Snippets

Stefan Glienke Make SameText and alike unicode aware without breaking change

Updated by Stefan Glienke

File snippet.txt Modified

  • Ignore whitespace
  • Hide word diff
   Writeln(AnsiSameText(a, b));
 end;
 
-// what if we would get rid of the 2 parameter overload and use the 3 param overload but with
-
-type
-  TLocaleOptions = (loInvariantLocale, loUserLocale, loUseGlobalBehavior);
+// what if we would introduce a global switch to control the behavior to
+// keep the old behavior or have those methods handle unicode automatically
 
 var
   DoLegacyStringStuff: Boolean = True;
 
-// just hacking this up without copying code from SysUtils
-function SameText(const S1, S2: string; LocaleOptions: TLocaleOptions = loUseGlobalBehavior): Boolean;
+// just hacking this up and naming it differently for this demo to not collide with the existing functions
+function SameTextNew(const S1, S2: string): Boolean;
 begin
-  if LocaleOptions = loUseGlobalBehavior then
-    if DoLegacyStringStuff then
-      LocaleOptions := loInvariantLocale
-    else
-      LocaleOptions := loUserLocale;
-
-  Result := System.SysUtils.SameText(S1, S2, System.SysUtils.TLocaleOptions(LocaleOptions));
+  if DoLegacyStringStuff then
+    Result := SameText(S1, S2)
+  else
+    Result := AnsiSameText(S1, S2);
 end;
 
 
   // without changing the DoLegacyStringStuff variable we have the same as before
 
   Writeln('after change without touching global variable:');
-  Writeln(SameText(a, b));
+  Writeln(SameTextNew(a, b)); // this would be SameText in real code
   Writeln(SameText(a, b, loUserLocale));
   Writeln(AnsiSameText(a, b));
 
   DoLegacyStringStuff := False;
 
   Writeln('changing global variable:');
-  Writeln(SameText(a, b));
+  Writeln(SameTextNew(a, b)); // this would be SameText in real code
   Writeln(SameText(a, b, loUserLocale));
   Writeln(AnsiSameText(a, b));
 end;
Created by Stefan Glienke

File snippet.txt Added

  • Ignore whitespace
  • Hide word diff
+program Project1;
+
+{$APPTYPE CONSOLE}
+
+uses
+  System.SysUtils;
+
+var
+  a: string = 'äöü';
+  b: string = 'ÄÖÜ';
+
+procedure CurrentSitation;
+begin
+  // as we know the first call will return false as SameText only considers a..z
+
+  Writeln('currently:');
+  Writeln(SameText(a, b));
+  Writeln(SameText(a, b, loUserLocale));
+  Writeln(AnsiSameText(a, b));
+end;
+
+// what if we would get rid of the 2 parameter overload and use the 3 param overload but with
+
+type
+  TLocaleOptions = (loInvariantLocale, loUserLocale, loUseGlobalBehavior);
+
+var
+  DoLegacyStringStuff: Boolean = True;
+
+// just hacking this up without copying code from SysUtils
+function SameText(const S1, S2: string; LocaleOptions: TLocaleOptions = loUseGlobalBehavior): Boolean;
+begin
+  if LocaleOptions = loUseGlobalBehavior then
+    if DoLegacyStringStuff then
+      LocaleOptions := loInvariantLocale
+    else
+      LocaleOptions := loUserLocale;
+
+  Result := System.SysUtils.SameText(S1, S2, System.SysUtils.TLocaleOptions(LocaleOptions));
+end;
+
+
+procedure Proposal;
+begin
+  // without changing the DoLegacyStringStuff variable we have the same as before
+
+  Writeln('after change without touching global variable:');
+  Writeln(SameText(a, b));
+  Writeln(SameText(a, b, loUserLocale));
+  Writeln(AnsiSameText(a, b));
+
+  // when we change it we get unicode handling by default and everywhere
+  DoLegacyStringStuff := False;
+
+  Writeln('changing global variable:');
+  Writeln(SameText(a, b));
+  Writeln(SameText(a, b, loUserLocale));
+  Writeln(AnsiSameText(a, b));
+end;
+
+begin
+  CurrentSitation;
+  Proposal;
+end.
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.