DEADSOFTWARE

escape strings in stat files
[d2df-sdl.git] / src / shared / utils.pas
index f5f762c6e983c96a5dd7c54ad3fb68c82110d058..946e4f5a07c7a31a5422fad1f2f479fb83ffce6a 100644 (file)
@@ -247,6 +247,9 @@ function digitInBase (ch: AnsiChar; base: Integer): Integer;
 // double quotes supports c-style escapes
 // function will select quote mode automatically
 function quoteStr (const s: AnsiString): AnsiString;
+// separate single-quote and double-quote escape functions
+function squoteStr (const s: AnsiString): AnsiString;
+function dquoteStr (const s: AnsiString): AnsiString;
 
 
 type
@@ -720,53 +723,52 @@ end;
 
 
 // ////////////////////////////////////////////////////////////////////////// //
-function quoteStr (const s: AnsiString): AnsiString;
-
-  function squote (const s: AnsiString): AnsiString;
-  var
-    f: Integer;
+function squoteStr (const s: AnsiString): AnsiString;
+var
+  f: Integer;
+begin
+  result := '''';
+  for f := 1 to Length(s) do
   begin
-    result := '''';
-    for f := 1 to Length(s) do
-    begin
-      if (s[f] = '''') then result += '''';
-      result += s[f];
-    end;
-    result += '''';
+    if (s[f] = '''') then result += '''';
+    result += s[f];
   end;
+  result += '''';
+end;
 
-  function dquote (const s: AnsiString): AnsiString;
-  var
-    f: Integer;
-    ch: AnsiChar;
+function dquoteStr (const s: AnsiString): AnsiString;
+var
+  f: Integer;
+  ch: AnsiChar;
+begin
+  result := '"';
+  for f := 1 to Length(s) do
   begin
-    result := '"';
-    for f := 1 to Length(s) do
+    ch := s[f];
+         if (ch = #0) then result += '\z'
+    else if (ch = #9) then result += '\t'
+    else if (ch = #10) then result += '\n'
+    else if (ch = #13) then result += '\r'
+    else if (ch = #27) then result += '\e'
+    else if (ch < ' ') or (ch = #127) then
     begin
-      ch := s[f];
-           if (ch = #0) then result += '\z'
-      else if (ch = #9) then result += '\t'
-      else if (ch = #10) then result += '\n'
-      else if (ch = #13) then result += '\r'
-      else if (ch = #27) then result += '\e'
-      else if (ch < ' ') or (ch = #127) then
-      begin
-        result += '\x';
-        result += LowerCase(IntToHex(Integer(ch), 2));
-      end
-      else if (ch = '"') or (ch = '\') then
-      begin
-        result += '\';
-        result += ch;
-      end
-      else
-      begin
-        result += ch;
-      end;
+      result += '\x';
+      result += LowerCase(IntToHex(Integer(ch), 2));
+    end
+    else if (ch = '"') or (ch = '\') then
+    begin
+      result += '\';
+      result += ch;
+    end
+    else
+    begin
+      result += ch;
     end;
-    result += '"';
   end;
+  result += '"';
+end;
 
+function quoteStr (const s: AnsiString): AnsiString;
 var
   needSingle: Boolean = false;
   f: Integer;
@@ -774,9 +776,9 @@ begin
   for f := 1 to Length(s) do
   begin
     if (s[f] = '''') then begin needSingle := true; continue; end;
-    if (s[f] < ' ') or (s[f] = #127) then begin result := dquote(s); exit; end;
+    if (s[f] < ' ') or (s[f] = #127) then begin result := dquoteStr(s); exit; end;
   end;
-  if needSingle then result := squote(s) else result := ''''+s+'''';
+  if needSingle then result := squoteStr(s) else result := ''''+s+'''';
 end;