It is currently August 18th, 2019, 12:06 pm

Unescape to Unicode character

Help with creating, editing & fixing problems with skins
Yincognito
Posts: 652
Joined: February 27th, 2015, 2:38 pm

Unescape to Unicode character

Yincognito » March 16th, 2019, 8:42 pm

As you probably know, Rainmeter references Unicode characters this way. In this sample skin I'm trying to convert &, & and & (all referencing the same thing) to the ampersand & character, but while ResultString1 turns out as desired, ResultString2 doesn't make the necessary replacements, because, for some reason, \1 is taken literally, instead of the intended [\38] or [\x26]:

Code: Select all

[Variables]
amp=38

[Rainmeter]
AccurateText=1
Update=1000
DynamicWindowSize=1

[MS_SourceString]
Measure=String
String="Me & you & him & everybody went for a walk."
DynamicVariables=1

[MS_ResultString1]
Measure=String
String=[MS_SourceString]
RegExpSubstitute=1
Substitute="(?si)(&)(\w+)(;)":"\1##\2#\3","(?si)&#([#\w]+);":"[\\1]","(?si)(\[\\[#\w]+\])":"[\38]"
DynamicVariables=1

[MS_ResultString2]
Measure=String
String=[MS_SourceString]
RegExpSubstitute=1
Substitute="(?si)(&)(\w+)(;)":"\1##\2#\3","(?si)&#([#\w]+);":"[\\1]","(?si)(\[\\[#\w]+\])":"\1"
DynamicVariables=1

[MT_Test]
Meter=STRING
SolidColor=64,64,64,255
FontColor=255,255,255,255
MeasureName=MS_SourceString
MeasureName2=MS_ResultString1
MeasureName3=MS_ResultString2
Text="Source:	%1#CRLF#Result1:	%2#CRLF#Result2:	%3"
DynamicVariables=1
So, how can I unescape \1 to the actual [\38] or [\x26]? Did I miss something here?
Yincognito
Posts: 652
Joined: February 27th, 2015, 2:38 pm

Re: Unescape to Unicode character

Yincognito » March 17th, 2019, 5:18 pm

Found the solution: make the necessary replacements (so that the string has the desired [\#amp#], [\38] and [\26] in it after the substitution), then use the substitute result in a bang. Only when used in a bang (and probably in a measure or a new variable as well) the above replacements are "unescaped" and the end result becomes the expected Me & you & him & everybody went for a walk.

Finally I can make a DecodeCharacterReference-like Substitute statement to handle single or multiple HTML/XML character reference encoding at the moment I choose and in whichever measure I choose (not just a WebParser measure) in just a few steps and without parsing the string thousands of times replacing each codepoint separately. Of course, a few hundred variables like amp or comma below are needed, but this should be both fast and effective:

Code: Select all

[Variables]
amp=38
comma=44

[Rainmeter]
AccurateText=1
Update=1000
DynamicWindowSize=1

[MS_SourceString]
Measure=String
String="Me, & you, & him, & everybody went for a walk."
DynamicVariables=1

[MS_ResultString1]
Measure=String
String=[MS_SourceString]
UpdateDivider=-1
RegExpSubstitute=1
Substitute="(?s)&(?:amp|AMP|#0*38|(?i)#x0*26);":"&"
DynamicVariables=1

[MS_ResultString2]
Measure=String
String=[MS_SourceString]
UpdateDivider=-1
RegExpSubstitute=1
Substitute="&(\w+);":"[\#\1#]","&#?(\w+);":"[\\1]"
OnUpdateAction=[!SetOption MS_ResultString3 String [MS_ResultString2]]
DynamicVariables=1

[MS_ResultString3]
Measure=String
String=
UpdateDivider=-1
DynamicVariables=1

[MT_Test]
Meter=STRING
SolidColor=64,64,64,255
FontColor=255,255,255,255
MeasureName=MS_SourceString
MeasureName2=MS_ResultString1
MeasureName3=MS_ResultString2
MeasureName4=MS_ResultString3
Text="Source:	%1#CRLF#Result1:	%2#CRLF#Result2:	%3#CRLF#Result3:	%4"
DynamicVariables=1