implement Trim() in uUtils
authorsheepluva
Sun, 15 May 2016 23:59:24 +0200
changeset 11829 3f1f8f79fcdb
parent 11828 0fba6cb098a1
child 11830 bb6503b9847e
implement Trim() in uUtils
hedgewars/pas2cRedo.pas
hedgewars/uLandObjects.pas
hedgewars/uRender.pas
hedgewars/uUtils.pas
--- a/hedgewars/pas2cRedo.pas	Sun May 15 23:11:14 2016 +0200
+++ b/hedgewars/pas2cRedo.pas	Sun May 15 23:59:24 2016 +0200
@@ -71,7 +71,7 @@
     trunc, round, ceil : function : integer;
     abs, sqr : function : integer;
 
-    StrPas, FormatDateTime, copy, str, PosS, trim, LowerCase : function : shortstring;
+    StrPas, FormatDateTime, copy, str, PosS, LowerCase : function : shortstring;
     pos : function : integer;
     StrToInt : function : integer;
     SetLength, SetLengthA, val, StrDispose, StrCopy : procedure;
--- a/hedgewars/uLandObjects.pas	Sun May 15 23:11:14 2016 +0200
+++ b/hedgewars/uLandObjects.pas	Sun May 15 23:59:24 2016 +0200
@@ -34,7 +34,7 @@
 
 implementation
 uses uStore, uConsts, uConsole, uRandom, uSound
-     , uTypes, uVariables, uDebug, SysUtils, uUtils
+     , uTypes, uVariables, uDebug, uUtils
      , uPhysFSLayer;
 
 const MaxRects = 512;
--- a/hedgewars/uRender.pas	Sun May 15 23:11:14 2016 +0200
+++ b/hedgewars/uRender.pas	Sun May 15 23:59:24 2016 +0200
@@ -463,7 +463,7 @@
         // print up to 3 extentions per row
         // ExtractWord will return empty string if index out of range
         //AddFileLog(TrimRight(
-        AddFileLog((
+        AddFileLog(Trim(
             ExtractWord(tmpint, tmpstr, [' ']) + ' ' +
             ExtractWord(tmpint+1, tmpstr, [' ']) + ' ' +
             ExtractWord(tmpint+2, tmpstr, [' '])
--- a/hedgewars/uUtils.pas	Sun May 15 23:11:14 2016 +0200
+++ b/hedgewars/uUtils.pas	Sun May 15 23:59:24 2016 +0200
@@ -23,6 +23,9 @@
 interface
 uses uTypes, uFloat;
 
+// returns s with whitespaces (chars <= #32) removed form both ends
+function Trim(s: shortstring) : shortstring;
+
 procedure SplitBySpace(var a, b: shortstring);
 procedure SplitByChar(var a, b: shortstring; c: char);
 procedure SplitByCharA(var a, b: ansistring; c: char);
@@ -98,7 +101,7 @@
 
 
 implementation
-uses {$IFNDEF PAS2C}typinfo, {$ENDIF}Math, uConsts, uVariables, SysUtils, uPhysFSLayer, uDebug;
+uses {$IFNDEF PAS2C}typinfo, {$ENDIF}Math, uConsts, uVariables, uPhysFSLayer, uDebug;
 
 {$IFDEF DEBUGFILE}
 var logFile: PFSFile;
@@ -108,6 +111,45 @@
 {$ENDIF}
 var CharArray: array[0..255] of Char;
 
+// All leading/tailing characters with ordinal values less than or equal to 32 (a space) are stripped.
+function Trim(s: shortstring) : shortstring;
+var len, left, right: integer;
+begin
+
+len:= Length(s);
+
+if len = 0 then
+    exit(s);
+
+// find first non-whitespace
+left:= 1;
+while left <= len do
+    begin
+    if s[left] > #32 then
+        break;
+    inc(left);
+    end;
+
+// find last non-whitespace
+right:= len;
+while right >= 1 do
+    begin
+    if s[right] > #32 then
+        break;
+    dec(right);
+    end;
+
+// string is whitespace only
+if left > right then
+    exit('');
+
+// get string without surrounding whitespace
+len:= right - left + 1;
+
+Trim:= copy(s, left, len);
+
+end;
+
 procedure SplitBySpace(var a,b: shortstring);
 begin
 SplitByChar(a,b,' ');