Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Non-ASCII characters display incorrectly in FOR loops, but only if the parens are not on the same line as each other

Jul
532
10
I’m moving towards changing my quotes and apostrophes to smart quotes and smart apostrophes, which aren’t drawn correctly in web browsers but are drawn very correctly in the console and easier for me to use because I can tell which quotes are which more easily.

I noticed .... they don’t work inside for loops.
Kinda like how iff-then often doesn’t work inside for loops.

Basically, in these 2 snippet examples, the quotes display correctly in the 1ˢᵗ, but not in the 2ⁿᵈ. It baffles me.

I’m sure there’s a technical reason why. I’m just sad that i have to go back and change single character into environment variables, or change for loops into subroutines, if i want to use them.


Does not display smart quotes/apostrophes correctly:
Code:
                        for %%tmpLetterForMeaning in (%1$) do (
                                echo tmpLetterForMeaning  = %tmpLetterForMeaning%
                                if "1" ne "%@RegEx[:,%tmpLetterForMeaning%]" (
                                        call fatal_error "%left_quote%%tmpLetterForMeaning%%right_quote% makes no sense. This parameter should have been in the forumat of %left_quote%{%italics_on%letter%italics_off%}:{%italics_on%meaning%italics_off%}%right_quote%, where %left_quote%letter%right_quote% is a letter found in our additional allowable keys %faint_on%(currently set to %left_quote%additional_keys%%right_quote%)%faint_off%"
                                )
                                set tmp_meaning_letter=%@LEFT[1,%tmpLetterForMeaning%]
                                set tmp_meaning_expand=%@RIGHT[%@EVAL[%@LEN[%tmpLetterForMeaning]-2],%tmpLetterForMeaning%]
                                echo ‘%tmp_meaning_letter%’ means ‘tmp_meaning_expand’
                        )

Does display smart quotes/apostrophes correctly:
Code:
                        for %%tmpLetterForMeaning in (%1$) do ( gosub processLetterMeaning "%tmpLetterForMeaning%")
                        goto :EOF
                                :processLetterMeaning                       
                                        echo tmpLetterForMeaning  = %tmpLetterForMeaning%
                                        if "1" ne "%@RegEx[:,%tmpLetterForMeaning%]" (
                                                call fatal_error "%left_quote%%tmpLetterForMeaning%%right_quote% makes no sense. This parameter should have been in the forumat of %left_quote%{%italics_on%letter%italics_off%}:{%italics_on%meaning%italics_off%}%right_quote%, where %left_quote%letter%right_quote% is a letter found in our additional allowable keys %faint_on%(currently set to %left_quote%additional_keys%%right_quote%)%faint_off%"
                                        )
                                        set tmp_meaning_letter=%@LEFT[1,%tmpLetterForMeaning%]
                                        set tmp_meaning_expand=%@RIGHT[%@EVAL[%@LEN[%tmpLetterForMeaning]-2],%tmpLetterForMeaning%]
                                        echo ‘%tmp_meaning_letter%’ means ‘tmp_meaning_expand’
                                return

It’s hard for me to fathom end-users who want to use the same characters that “phone folk” use being able to remember where “phone” characters are allowed and where they aren’t. I can barely remember and keep running into surprises. It would be nice if the character was treated the same in all TCC situations.
 
Last edited:
I’ve actually gone as far as to use AutoHotKey to remap my apostrophe key to the smart apostrophe (and use alt-’ for the original) and mapped ctrl-shift-" as the left smart quote and alt-shift-" as the right smart quote, and ctrl-alt-shift-" as both.

Much better. I also have ctrl-hyphen = endash and alt-hyphen = emdash and wonder why that’s not stock default. It’s like they decided PC folk are stuck in the past and only phone folk can do correct punctuation. But that’s just not true.
 
I don't have any problem using Unicode characters in a FOR loop:

Unicode-FOR.webp


Can you boil your issue down to a simple, one- or two-line demo?
 
The boil down is that i moved the exact same code that was between ( ) in the for into a gosub

No other changes

So what’s inside really shouldn’t matter. This is a pattern i’ve run into a lot, this was simply the time i had the activation energy to speak up about it.

I’ve had to redo a lot of stuff that way once i started using emojis

There’s definitely some kind of inconsistency. Echo should work the same everywhere, IMNSHO.
It doesn’t. It’s killing meeeeeeee
 
I should also add... Using %@CHAR[] instead of the actual characters has fixed this in 100% of the times i’ve encounted it.

But it’s much easier to turn a for loop into a gosub than to use an env-var, obviously.....
 
And yes, your example works fine at my command line. This only happens in Bat files it seems. Multi-line situations in ( ) which i know get collapsed internally but still may have something to do with it (?)
 
A small, simple example is almost always helpful for troubleshooting. Two thoughts come to my mind: (1) How is your batch file encoded? I suggest UTF-8 with a BOM. And (2), for a multiline loop, DO is probably a lot better than FOR.
 
Did it!
It’s the parenthesis processing.

1734063487118.webp

Something’s amok:

Code:
@Echo off

for %%foo in (bar) do ( gosub sub )
for %%foo in (bar) do ( echo “Hello, world!” )      
for %%foo in (bar) do ( 
        echo “Hello, world!”
)      

cancel

:sub                              
        echo “Hello, world!”
return
 
A small, simple example is almost always helpful for troubleshooting. Two thoughts come to my mind: (1) How is your batch file encoded? I suggest UTF-8 with a BOM. And (2), for a multiline loop, DO is probably a lot better than FOR.

The BAT is UTF-8. Pretty much all mine are.
 
I can reproduce this. Yeah, it doesn't look right to me either.

That said, for multiline loops, you should probably be using DO instead of FOR.
 
You little batch does work as expected when saved as UTF-16. So something to do with UTF-8 decoding when multiple lines are assembled.
 
It seems the closing paranthesis can be on a next line but not the text behind the opening ...

So the following should work in UTF-8 ...

Code:
@Echo off

for %%foo in (bar) do ( gosub sub )
for %%foo in (bar) do ( echo “Hello, world!” ) 
for %%foo in (bar) do ( echo “Hello, world!”
)

cancel

:sub                         
        echo “Hello, world!”
return
 
FOR doesn't support UTF-8 for line continuations -- because FOR exists solely for CMD compatibility, and CMD doesn't support UTF-8 anywhere.

FOR is a klunky disaster; if you're not striving for CMD compatibility, you should be using DO.

I’ve been so used to living with FOR that i didn’t even realize there was anything else until this year haha
 
Back
Top