New question re: A problem with the SafeChars Plugin...
Charles, unfortunately the major problem that started this whole thing kind of got lost in the shuffle, so to speak, and I still don't know quite how to handle it. (I've found two things that "seem" to work, but I really don't trust them.) And I will warn you in advance that unfortunately this is kind of long.
So here's the problem. I'm getting data from an external "source" via the "@ExecArray" function. Said data may contain "dangerous" characters, which, of course, will be "remapped" to their "safe" equivalents using "%@SafeExp". But the issue is that I often want to search individual elements of the array for (constant!) string(s) that may, themselves, contain "unsafe" characters.
So an example of what I was trying to do is to look for an "&" character in a particular element of that array.
So, some code fragments that I think will "illustrate" the problems I'm having.
First, the following code is contained in a .btm file that begins with:
Code:
@Echo Off
SetLocal
UnSafe /E:,
and ends with:
Code:
UnSafe /Z >NUL:
EndLocal
Quit 0
This is, of course, all pretty much the standard prologue and epilogue code of a "safe" batch file.
Now the first actual line of "useful" code is, for this example:
Code:
Set ABC="Ampersand: & Caret: ^ OR bar: | Comma: , "
Note that this is a quoted string, so the ampersand, caret, OR bar, and comma are all "valid".
So, executing:
Code:
@Echo String as entered: %ABC
produces:
Code:
String as entered: "Ampersand: & Caret: ^ OR bar: | Comma: , "
which is what you would expect, and is OK other than the fact that it is enclosed in double quotes, which is generally not acceptable in the situations where I am trying to process this data.
So executing:
Code:
@Echo Unquoted string: %@UnQuote[%ABC]
produces:
Code:
Unquoted string: Ampersand:
TCC: Z:\SafeCharTests.btm [6] Unknown command "Caret:"
TCC: Z:\SafeCharTests.btm [6] Unknown command "Comma:"
which is also, somewhat unfortunately, what you would expect.
So, executing:
Code:
@Echo Index of the ampersand in the "unsafe" environment variable with the ampersand "escaped":
@Echo %@Index[%ABC,^&]
produces:
Code:
Index of the ampersand in the "unsafe" environment variable with the ampersand "escaped":
12
which is "almost" acceptable if you remember that the opening quote of the environment variable is included in the index.
So, if you execute:
Code:
@Echo "Safe" environment Variable: %@SafeEnv[ABC]
produces pretty much what you would expect given that the "unsafe" characters have been replaced by close equivalents that are visually similar but not identical to the "original" characters.
However, executing:
Code:
@Echo Unquoted "safe" environment variable:
@Echo %@UnQuote[%SafeEnv[ABC]]
produces:
Code:
Unquoted "safe" environment variable:
"Ampersand: & Caret: ^ OR bar: | Comma: , "
Note that the "quotes" are still present because the "remapped" quotes are not really ASCII quotes, of course.
And executing:
Code:
@Echo Index of the ampersand in the "safe" environment variable:
%@Index[%@SafeEnv[ABC],^&]
produces:
because the "remapped" ampersand is no longer an ASCII ampersand, of course.
And, rather strangely in my opinion, executing:
Code:
@Echo Index of the caret in the "Unsafe" environment variable:
@Echo %@Index[%ABC,^^]
produces:
for reasons I don't really understand.
However, in researching this, the command sequence:
Code:
Set ABC="^"
Echo %@Ascii[%ABC]
produces:
Only the double quotes come up, no sign of the caret. And, just for verification:
produces,
meaning that the caret is still there, somewhere. Evidently some parsing that is taking place is "discarding" it in both this and other possibly similar situations.
Well, to search for a string containing an ampersand, for example, I was creating and then using a search term that was defined similar to:
Code:
Set SearchTerm1=%@SafeExp[This is an ampersand: &]
Now, in the "real" world situation the environment variable being searched would not be set by a "Set" command, but rather would be one of the results of the "@ExecArray" function. However, if you then:
you get:
What happened to the first word is kind of a mystery to me other than to say I was not supplying an internal variable, function, or array element to the "@SafeExp" function, which means, I suppose, that the the "results" of this statement are essentially undefined.
And I also don't have a clue as to a "supported" way to set an environment variable the "safe" equivalent of an "unsafe" character.
Now, there are two things that both appear to work, the first for completely mysterious to me reasons meaning I don't trust them at all, and the second one where, for whatever reason, the "undefined" behavior appears to have a "useful" result, but is it reliable?
So, the first, "mysterious", technique is to do something like:
Code:
Set SearchTerm1=%@SafeExp[Dropped This is an ampersand: &]
followed by:
Code:
Set SearchTerm1=%@LTrim[" ",%SearchTerm1]
which, when executing
produces:
Which is what I want. However, I think that you will agree with me when I say that the above is kind of a klutzy kludge (or maybe it's kluge, looking up both words online would seem that they are very similar if not in fact identical)
Another, slightly less odd, "solution" is:
Code:
Set SearchTerm1=This is an ampersand %@SafeExp[&] isn't it.
which produces, when echoed:
Code:
This is an ampersand & isn't it.
which is what I want but also depends on, at best, "undocumented" behavior.
So the question is, of course, how should this be handled?