# HG changeset patch # User sheepluva # Date 1463349564 -7200 # Node ID 3f1f8f79fcdb8b1da50b17ed7805954ce9f5dfb8 # Parent 0fba6cb098a1e4d0b67184d48a4814958d0a6ba7 implement Trim() in uUtils diff -r 0fba6cb098a1 -r 3f1f8f79fcdb hedgewars/pas2cRedo.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; diff -r 0fba6cb098a1 -r 3f1f8f79fcdb hedgewars/uLandObjects.pas --- 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; diff -r 0fba6cb098a1 -r 3f1f8f79fcdb hedgewars/uRender.pas --- 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, [' ']) diff -r 0fba6cb098a1 -r 3f1f8f79fcdb hedgewars/uUtils.pas --- 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,' ');