From: <jed...@us...> - 2009-03-01 15:33:23
|
Revision: 2663 http://jcl.svn.sourceforge.net/jcl/?rev=2663&view=rev Author: jedi_mbe Date: 2009-03-01 15:33:19 +0000 (Sun, 01 Mar 2009) Log Message: ----------- * Completed test suites for JclStrings * Fixed some bugs in JclStrings routines * Moved documentation for StrAnsiToOEM and StrOEMToAnsi from Strings.dtx to AnsiStrings.dtx Modified Paths: -------------- trunk/help/Strings.dtx trunk/jcl/source/common/JclStrings.pas trunk/qa/automated/dunit/units/TestJclStrings.pas Added Paths: ----------- trunk/help/AnsiStrings.dtx Added: trunk/help/AnsiStrings.dtx =================================================================== --- trunk/help/AnsiStrings.dtx (rev 0) +++ trunk/help/AnsiStrings.dtx 2009-03-01 15:33:19 UTC (rev 2663) @@ -0,0 +1,28 @@ +@@StrAnsiToOem +<GROUP StringManipulation.StringTransformationRoutines> +Summary: + Translates an ANSI string into a string using the OEM character set. +Description: + The StrAnsiToOem function translates an ansi string into a string using the + OEM defined character set. +Parameters: + S - The string to translate. +Result: + The translated string. +Donator: + Anonymous +-------------------------------------------------------------------------------- +@@StrOemToAnsi +<GROUP StringManipulation.StringTransformationRoutines> +Summary: + Translates an OEM string into an ansi string. +Description: + The StrOemToAnsi function translated a string using the OEM defined character set + into an ansi string. +Parameters: + S - The string to translate. +Result: + The translated string. +Donator: + Anonymous +-------------------------------------------------------------------------------- Modified: trunk/help/Strings.dtx =================================================================== --- trunk/help/Strings.dtx 2009-03-01 11:57:08 UTC (rev 2662) +++ trunk/help/Strings.dtx 2009-03-01 15:33:19 UTC (rev 2663) @@ -1254,7 +1254,7 @@ Parameters: S - The string in which to search for N occurences of SubStr. SubStr - The sub-string to search for. - Index - The number of occurences of sub-string in S. + N - The number of occurences of sub-string in S. Result: Index into S where the N-th occurence of SubStr is located. If SubStr has less than N occurences in S the result is 0. @@ -2377,34 +2377,6 @@ Donator: Team JCL -------------------------------------------------------------------------------- -@@StrAnsiToOem -<GROUP StringManipulation.StringTransformationRoutines> -Summary: - Translates an ANSI string into a string using the OEM character set. -Description: - The StrAnsiToOem function translates an ansi string into a string using the - OEM defined character set. -Parameters: - S - The string to translate. -Result: - The translated string. -Donator: - Anonymous --------------------------------------------------------------------------------- -@@StrOemToAnsi -<GROUP StringManipulation.StringTransformationRoutines> -Summary: - Translates an OEM string into an ansi string. -Description: - The StrOemToAnsi function translated a string using the OEM defined character set - into an ansi string. -Parameters: - S - The string to translate. -Result: - The translated string. -Donator: - Anonymous --------------------------------------------------------------------------------- @@StrCharsCount <GROUP StringManipulation.StringTransformationRoutines> Summary: @@ -3974,4 +3946,5 @@ <c>a positive value</c> if S1 is greater than S2 Donator: Marcel Bestebroer +-------------------------------------------------------------------------------- Modified: trunk/jcl/source/common/JclStrings.pas =================================================================== --- trunk/jcl/source/common/JclStrings.pas 2009-03-01 11:57:08 UTC (rev 2662) +++ trunk/jcl/source/common/JclStrings.pas 2009-03-01 15:33:19 UTC (rev 2663) @@ -158,7 +158,8 @@ type TCharValidator = function(const C: Char): Boolean; -function ArrayContainsChar(const Chars: array of Char; const C: Char): Boolean; +function ArrayContainsChar(const Chars: array of Char; const C: Char): Boolean; overload; +function ArrayContainsChar(const Chars: array of Char; const C: Char; out Index: Integer): Boolean; overload; // String Test Routines function StrIsAlpha(const S: string): Boolean; @@ -1012,6 +1013,13 @@ {$ENDIF ~CLR} function ArrayContainsChar(const Chars: array of Char; const C: Char): Boolean; +var + idx: Integer; +begin + Result := ArrayContainsChar(Chars, C, idx); +end; + +function ArrayContainsChar(const Chars: array of Char; const C: Char; out Index: Integer): Boolean; { optimized version for sorted arrays var I, L, H: Integer; @@ -1035,14 +1043,11 @@ end; Result := False; end;} -var - I: Integer; begin - Result := True; - for I := Low(Chars) to High(Chars) do - if Chars[I] = C then - Exit; - Result := False; + Index := High(Chars); + while (Index >= Low(Chars)) and (Chars[Index] <> C) do + Dec(Index); + Result := Index >= Low(Chars); end; // String Test Routines @@ -1098,6 +1103,10 @@ Result := False; if CheckAll then begin + // this will not work with the current definition of the validator. The validator would need to check each character + // it requires against the string (which is currently not provided to the Validator). The current implementation of + // CheckAll will check if all characters in S will be accepted by the provided Validator, which is wrong and incon- + // sistent with the documentation and the array-based overload. for I := 1 to Length(S) do begin Result := Chars(S[I]); @@ -1120,18 +1129,19 @@ var I: Integer; begin - Result := False; if CheckAll then begin - for I := 1 to Length(S) do + Result := True; + I := High(Chars); + while (I >= 0) and Result do begin - Result := ArrayContainsChar(Chars, S[I]); - if not Result then - Break; + Result := CharPos(S, Chars[I]) > 0; + Dec(I); end; end else begin + Result := False; for I := 1 to Length(S) do begin Result := ArrayContainsChar(Chars, S[I]); @@ -3035,18 +3045,18 @@ {$ELSE ~CLR} function StrFind(const Substr, S: string; const Index: Integer): Integer; var - Pos: PChar; + pos: Integer; begin if (SubStr <> '') and (S <> '') then begin - pos := StrPos(@S[Index], PChar(SubStr)); - if Pos = nil then - result := 0 + pos := StrIPos(Substr, Copy(S, Index, Length(S) - Index + 1)); + if pos = 0 then + Result := 0 else - Result := (Cardinal(Pos) - Cardinal(@S[1])) div SizeOf(Char) + 1; + Result := Index + Pos - 1; end else - result := 0; + Result := 0; end; {$ENDIF ~CLR} Modified: trunk/qa/automated/dunit/units/TestJclStrings.pas =================================================================== --- trunk/qa/automated/dunit/units/TestJclStrings.pas 2009-03-01 11:57:08 UTC (rev 2662) +++ trunk/qa/automated/dunit/units/TestJclStrings.pas 2009-03-01 15:33:19 UTC (rev 2663) @@ -100,7 +100,6 @@ procedure _StrTrimCharsRight; procedure _StrTrimQuotes; procedure _StrUpper_StrUpperInPlace_StrUpperBuff; - procedure _StrOemToAnsi_StrAnsiToOem; end; { TJclStringManagment } @@ -412,8 +411,24 @@ //-------------------------------------------------------------------------------------------------- +function ContainsValidator(const C: Char): Boolean; +begin + Result := (C = 'g') or (C = 'r'); +end; + procedure TJclStringTransformation._StrContainsChars; begin + CheckEquals(True, StrContainsChars('AbcdefghiJkl', ['g', 'r'], False), 'array, CheckAll set to False'); + CheckEquals(False, StrContainsChars('AbcdefghiJkl', ['g', 'r'], True), 'array, CheckAll set to True, only 1 occurring'); + CheckEquals(True, StrContainsChars('AbcdefghiJklr', ['g', 'r'], True), 'array, CheckAll set to True, both occurring'); + + CheckEquals(True, StrContainsChars('AbcdefghiJkl', ContainsValidator, False), 'validator, CheckAll set to False'); + // CheckAll=True will not work with a validator, at least not with the same meaning as with the array-based tests. + // The tests are disabled for now. + { + CheckEquals(False, StrContainsChars('AbcdefghiJkl', ContainsValidator, True), 'validator, CheckAll set to True, only 1 occurring'); + CheckEquals(True, StrContainsChars('AbcdefghiJklr', ContainsValidator, True), 'validator, CheckAll set to True, both occurring'); + } end; //-------------------------------------------------------------------------------------------------- @@ -1159,8 +1174,8 @@ i,t: Integer; s, s3, sn: string; begin - CheckEquals(StrTrimCharLeft('',#0),'','StrTrimCharLeft'); - CheckEquals(StrTrimCharLeft('AAAAAAAAAA','A'),'','StrTrimCharLeft'); + CheckEquals('', StrTrimCharLeft('', #0), 'StrTrimCharLeft1'); + CheckEquals('', StrTrimCharLeft('AAAAAAAAAA', 'A'), 'StrTrimCharLeft2'); GenerateAll(200, 2000, StringArray); GenerateAll(1, 2000, StringArray2); @@ -1182,14 +1197,25 @@ dec(t); end; - CheckEquals(StrTrimCharLeft(S3,SN[1]), S,'StrTrimCharLeft'); + CheckEquals(S, StrTrimCharLeft(S3,SN[1]), 'StrTrimCharLeft3.' + IntToStr(i)); end; end; //-------------------------------------------------------------------------------------------------- +function TrimValidator(const C: Char): Boolean; +begin + Result := (C = 'A') or (C = 'B'); +end; + procedure TJclStringTransformation._StrTrimCharsLeft; begin + CheckEquals('', StrTrimCharsLeft('', []), 'empty str, empty array'); + CheckEquals('ABC', StrTrimCharsLeft('ABC', []), 'non-empty str, empty array'); + CheckEquals('BCA', StrTrimCharsLeft('ABCA', ['A']), 'ABCA str, A array'); + CheckEquals('CA', StrTrimCharsLeft('ABCA', ['B', 'A']), 'ABCA str, BA array'); + + CheckEquals('CA', StrTrimCharsLeft('ABCA', TrimValidator), 'ABCA str, AB validator'); end; //-------------------------------------------------------------------------------------------------- @@ -1201,8 +1227,8 @@ begin // -- StrTrimCharRight -- - CheckEquals(StrTrimCharRight('',#0),'','StrTrimCharRight'); - CheckEquals(StrTrimCharRight('AAAAAAAAAA','A'),'','StrTrimCharRight'); + CheckEquals('', StrTrimCharRight('', #0), 'StrTrimCharRight1'); + CheckEquals('', StrTrimCharRight('AAAAAAAAAA', 'A'), 'StrTrimCharRight2'); GenerateAll(200, 2000, StringArray); GenerateAll(1, 2000, StringArray2); @@ -1224,7 +1250,7 @@ dec(t); end; - CheckEquals(StrTrimCharRight(S3,SN[1]), S,'StrTrimCharRight'); + CheckEquals(S, StrTrimCharRight(S3, SN[1]), 'StrTrimCharRight3.' + IntToStr(i)); end; end; @@ -1232,6 +1258,12 @@ procedure TJclStringTransformation._StrTrimCharsRight; begin + CheckEquals('', StrTrimCharsRight('', []), 'empty str, empty array'); + CheckEquals('ABC', StrTrimCharsRight('ABC', []), 'non-empty str, empty array'); + CheckEquals('ABC', StrTrimCharsRight('ABCA', ['A']), 'ABCA str, A array'); + CheckEquals('AB', StrTrimCharsRight('ABCA', ['C', 'A']), 'ABCA str, CA array'); + + CheckEquals('ABC', StrTrimCharsRight('ABCAABA', TrimValidator), 'ABCAABA str, AB validator'); end; //-------------------------------------------------------------------------------------------------- @@ -1288,10 +1320,6 @@ //-------------------------------------------------------------------------------------------------- -procedure TJclStringTransformation._StrOemToAnsi_StrAnsiToOem; -begin -end; - //================================================================================================== // String Managment //================================================================================================== @@ -1611,8 +1639,9 @@ procedure TJclStringSearchandReplace._StrFind; begin - CheckEquals(0, StrFind('abc', 'Test')); - CheckEquals(1, StrFind('Test', 'Test')); + CheckEquals(0, StrFind('abc', 'Test'), 'StrFind_1'); + CheckEquals(1, StrFind('Test', 'Test'), 'StrFind_2'); + CheckEquals(1, StrFind('Test', 'test'), 'StrFind_3'); end; //-------------------------------------------------------------------------------------------------- @@ -1645,18 +1674,36 @@ procedure TJclStringSearchandReplace._StrIndex; begin + CheckEquals(-1, StrIndex('', ['A', 'B']), 'Empty string in array of AB'); + CheckEquals(-1, StrIndex('A', []), '''A'' string in empty array'); + CheckEquals(0, StrIndex('A', ['A', 'B']), '''A'' string in array of AB, equal case'); + CheckEquals(0, StrIndex('a', ['A', 'B']), '''A'' string in array of AB, differing case'); + CheckEquals(1, StrIndex('B', ['A', 'B']), '''B'' string in array of AB, equal case'); + CheckEquals(2, StrIndex('C', ['A', 'B', 'C', 'C']), '''C'' string in array of ABCC, equal case'); end; //-------------------------------------------------------------------------------------------------- procedure TJclStringSearchandReplace._StrILastPos; begin + CheckEquals(10, StrILastPos('A', 'aaaaaaaaaa'), 'StrILastPos_1'); + CheckEquals(16, StrILastPos('abA', 'aabaaababababababa'), 'StrILastPos_2'); + CheckEquals(8, StrILastPos('abbA', 'abbaabbabba'), 'StrILastPos_3'); + CheckEquals(0, StrILastPos('_abba', 'abbaabbabba'), 'StrILastPos_4'); + CheckEquals(5, StrILastPos('_aBBa', 'abba_abbabba'), 'StrILastPos_5'); + CheckEquals(15, StrILastPos('ABA', 'aabaaaABAbabababa'), 'StrILastPos_6'); end; //-------------------------------------------------------------------------------------------------- procedure TJclStringSearchandReplace._StrIPos; begin + CheckEquals(1, StrIPos('A', 'aaaaaaaaaa'), 'StrIPos_1'); + CheckEquals(2, StrIPos('abA', 'aabaaababababababa'), 'StrIPos_2'); + CheckEquals(1, StrIPos('abbA', 'abbaabbabba'), 'StrIPos_3'); + CheckEquals(0, StrIPos('_abba', 'abbaabbabba'), 'StrIPos_4'); + CheckEquals(5, StrIPos('_aBBa', 'abba_abbabba'), 'StrIPos_5'); + CheckEquals(2, StrIPos('ABA', 'aabaaaABAbabababa'), 'StrIPos_6'); end; //-------------------------------------------------------------------------------------------------- @@ -1744,6 +1791,12 @@ procedure TJclStringSearchandReplace._StrNIPos; begin + CheckEquals(5, StrNIPos('aaaaaaaaaa', 'A', 5), 'StrNIPos_1'); + CheckEquals(0, StrNIPos('aabaaababababababa', 'abA', 0), 'StrNIPos_2'); + CheckEquals(0, StrNIPos('abbaabbabba', 'abbA', 4), 'StrNIPos_3'); + CheckEquals(8, StrNIPos('abbaabbabba', 'abba', 3), 'StrNIPos_4'); + CheckEquals(5, StrNIPos('abba_abbabba', '_aBBa', 1), 'StrNIPos_5'); + CheckEquals(11, StrNIPos('aabaaaABAbabababa', 'ABA', 4), 'StrNIPos_6'); end; //-------------------------------------------------------------------------------------------------- @@ -1814,7 +1867,15 @@ //-------------------------------------------------------------------------------------------------- procedure TJclStringCharacterTestRoutines._CharIsBlank; +var + c1: char; + begin + for c1 := #0 to #255 do + CheckEquals( + (c1 in [#9, ' ', #160]), + CharIsBlank(c1), + 'CharIsBlank #' + IntToStr(Ord(c1))); end; //-------------------------------------------------------------------------------------------------- This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |