unit testStringUnit;
interface
uses
SysUtils,
Types,
StringUnit,
ConstUnit,
XPtest,
end_uses;
//procedure testByteType;
procedure testCharToByteLen;
procedure testStringTextPartsCompare;
procedure testCompareStringWideAPI;
procedure testCopyIndex;
procedure testMapStringW;
procedure testStrCount1;
procedure testStringCount;
procedure testCheckStrInTable;
procedure testCheckCharInTable;
procedure testChangeLineBreakes;
procedure testStringsReplace;
procedure testEncodeEscapeSequence;
procedure testDecodeEscapeSequence;
procedure testEncodeStringToCodeCsv;
procedure testStringsWordReplace;
procedure testTrimFirstChar;
procedure testTrimLastChar;
procedure testTrimChar;
procedure testOneTrimChar;
procedure testGetTagInfo;
procedure testGetTagText;
procedure testInStr;
procedure testWideCharByteLength;
procedure testCharIndexToByteIndex;
procedure testByteIndexToCharIndex;
procedure testCharLengthToByteLength;
procedure testIncludeBothEndsStr;
procedure testExcludeBothEndsStr;
implementation
(*----------------------------------------
procedure testByteType;
begin
Check(True, IsDBCSLeadChar( ('る'[1]) ));
Check(True, IsDBCSLeadChar( ('る'[2]) ));
Check(True, IsDBCSLeadChar( ('あ'[1]) ));
Check(False, IsDBCSLeadChar( ('あ'[2]) ));
//↓SysUtilで使われているWinAPI関数
//Check(True, IsDBCSLeadByte( Byte('る'[1]) ));
//Check(True, IsDBCSLeadByte( Byte('る'[2]) ));
//Check(True, IsDBCSLeadByte( Byte('あ'[1]) ));
//Check(False, IsDBCSLeadByte( Byte('あ'[2]) ));
Check(mbSingleByte, ByteType('ABC', 1));
Check(mbLeadByte, ByteType('あ', 1));
Check(mbTrailByte, ByteType('あ', 2));
Check(mbLeadByte, ByteType('る', 1));
Check(mbTrailByte, ByteType('る', 2));
Check(mbLeadByte, ByteType('ああああ', 1));
Check(mbTrailByte, ByteType('ああああ', 2));
Check(mbLeadByte, ByteType('ああああ', 3));
Check(mbTrailByte, ByteType('ああああ', 4));
Check(mbLeadByte, ByteType('ああああ', 5));
Check(mbTrailByte, ByteType('ああああ', 6));
Check(mbLeadByte, ByteType('ああああ', 7));
Check(mbTrailByte, ByteType('ああああ', 8));
Check(mbLeadByte, ByteType('るるるる', 1));
Check(mbTrailByte, ByteType('るるるる', 2));
Check(mbLeadByte, ByteType('るるるる', 3));
Check(mbTrailByte, ByteType('るるるる', 4));
Check(mbLeadByte, ByteType('るるるる', 5));
Check(mbTrailByte, ByteType('るるるる', 6));
Check(mbLeadByte, ByteType('るるるる', 7));
Check(mbTrailByte, ByteType('るるるる', 8));
Check(mbLeadByte, ByteType('ABCああああ', 4));
Check(mbTrailByte, ByteType('ABCああああ', 5));
Check(mbLeadByte, ByteType('ABCああああ', 6));
Check(mbTrailByte, ByteType('ABCああああ', 7));
Check(mbLeadByte, ByteType('ABCああああ', 8));
Check(mbTrailByte, ByteType('ABCああああ', 9));
Check(mbLeadByte, ByteType('ABCああああ', 10));
Check(mbTrailByte, ByteType('ABCああああ', 11));
Check(mbLeadByte, ByteType('ABCるるるる', 4));
Check(mbTrailByte, ByteType('ABCるるるる', 5));
Check(mbLeadByte, ByteType('ABCるるるる', 6));
Check(mbTrailByte, ByteType('ABCるるるる', 7));
Check(mbLeadByte, ByteType('ABCるるるる', 8));
Check(mbTrailByte, ByteType('ABCるるるる', 9));
Check(mbLeadByte, ByteType('ABCるるるる', 10));
Check(mbTrailByte, ByteType('ABCるるるる', 11));
end;
//----------------------------------------*)
procedure testCharToByteLen;
begin
Check( 2, CharToByteLen('あいうえお', 1));
Check( 6, CharToByteLen('あいうえお', 3));
Check(10, CharToByteLen('あいうえお', 5));
Check(10, CharToByteLen('あいうえお', 6));
Check( 1, CharToByteLen('ABC123あいうえお', 1));
Check( 6, CharToByteLen('ABC123あいうえお', 6));
Check( 8, CharToByteLen('ABC123あいうえお', 7));
Check(16, CharToByteLen('ABC123あいうえお',11));
end;
procedure testStringTextPartsCompare;
procedure CheckFunctionWide(ReturnStrValue, ReturnTextValue: Boolean;
const Sub, S: WideString; StrIndex: Integer);
begin
Check( ReturnStrValue, StringPartsCompare(Sub, S, StrIndex) );
Check( ReturnTextValue, StringPartsCompare(Sub, S, StrIndex, ccIgnoreCase) );
end;
procedure CheckFunction(ReturnStrValue, ReturnTextValue: Boolean;
const Sub, S: WideString; AnsiStrIndex, WideStrIndex: Integer);
begin
CheckFunctionWide(ReturnStrValue, ReturnTextValue, Sub, S, WideStrIndex);
end;
begin
CheckFunction( True, True, 'あ', 'あいうえお', 1, 1);
CheckFunction( True, True, 'うえ', 'あいうえお', 5, 3);
CheckFunction( True, True, 'お', 'あいうえお', 9, 5);
CheckFunction(False,False, 'あ', 'あいうえお', 0, 0);
CheckFunction(False,False, 'あ', 'あいうえお', 2, 2);
CheckFunction(False,False, 'うえ', 'あいうえお', 4, 2);
CheckFunction(False,False, 'うえ', 'あいうえお', 6, 4);
CheckFunction(False,False, 'うえ', 'あいうえお', 8, 5);
CheckFunction(False,False, 'お', 'あいうえお', 8, 4);
CheckFunction(False,False, 'お', 'あいうえお', 11, 6);
CheckFunction( True, True, 'D', 'ABCDEF', 4, 4);
CheckFunction(False, True, 'D', 'abcdef', 4, 4);
CheckFunction( True, True, 'D', 'ABCDEF', 7, 4);
CheckFunction(False, True, 'D', 'abcdef', 7, 4);
CheckFunction(False,False, 'ABC', 'AB', 1, 1);
end;
procedure testCompareStringWideAPI;
begin
Check(crEqual, CompareStringWideAPI('123', '123', 1, 1, 3, 3, ccCaseSensitive));
Check(crGreaterThan, CompareStringWideAPI('123', '12', 1, 1, 3, 2, ccCaseSensitive));
Check(crLessThan, CompareStringWideAPI('12', '123', 1, 1, 2, 3, ccCaseSensitive));
Check(crLessThan, CompareStringWideAPI('123', '133', 1, 1, 3, 3, ccCaseSensitive));
Check(crGreaterThan, CompareStringWideAPI('133', '123', 1, 1, 3, 3, ccCaseSensitive));
end;
procedure testCopyIndex;
begin
Check('', CopyIndex('abcdefg', -1, 0));
Check('', CopyIndex('abcdefg', 0, -1));
Check('', CopyIndex('abcdefg', -3, 0));
Check('', CopyIndex('abcdefg', 0, -3));
Check('', CopyIndex('abcdefg', -10, -1));
Check('', CopyIndex('abcdefg', -1, -10));
Check('', CopyIndex('abcdefg', -10, -2));
Check('', CopyIndex('abcdefg', -2, -10));
Check('abc', CopyIndex('abcdefg', 1, 3));
Check('abc', CopyIndex('abcdefg', 0, 3));
Check('abc', CopyIndex('abcdefg', -10, 3));
Check('', CopyIndex('abcdefg', 3, 1));
Check('', CopyIndex('abcdefg', 3, 0));
Check('', CopyIndex('abcdefg', 3, -10));
Check('def', CopyIndex('abcdefg', 4, 6));
Check('', CopyIndex('abcdefg', 6, 4));
Check('efg', CopyIndex('abcdefg', 5, 7));
Check('efg', CopyIndex('abcdefg', 5, 8));
Check('efg', CopyIndex('abcdefg', 5, 10));
Check('', CopyIndex('abcdefg', 7, 5));
Check('', CopyIndex('abcdefg', 8, 5));
Check('', CopyIndex('abcdefg', 10, 5));
Check('g', CopyIndex('abcdefg', 7, 7));
Check('g', CopyIndex('abcdefg', 7, 8));
Check('', CopyIndex('abcdefg', 8, 8));
Check('', CopyIndex('abcdefg', 8, 9));
Check('', CopyIndex('abcdefg', 8, 15));
Check('', CopyIndex('abcdefg', 9, 8));
Check('', CopyIndex('abcdefg', 15, 8));
Check('', CopyIndex('abcdefg', 10, 15));
Check('', CopyIndex('abcdefg', 15, 10));
end;
procedure testMapStringW;
begin
Check('あああ', KataToHira('アあア'));
Check('あかさたなはまやらわをん', KataToHira('アカサタナハマヤラワヲン'));
Check('アアア', HiraToKata('あアあ'));
Check('アカサタナハマヤラワヲン', HiraToKata('あかさたなはまやらわをん'));
Check('アアア', KataToHira('アアア'));
Check('abc', ZenkakuToHankaku('abc'));
Check('アアア', ZenkakuToHankaku('アアア'));
Check('abc', HankakuToZenkaku('abc'));
Check('アアア', HankakuToZenkaku('アアア'));
end;
procedure testStrCount1;
begin
// Check(0, StrCount('A', '') );
// Check(0, StrCount('ABC', '') );
// Check(0, StrCount('', 'A') );
// Check(0, StrCount('', 'ABC') );
// Check(1, StrCount('A', 'A') );
// Check(3, StrCount('A', 'AAA') );
// Check(1, StrCount('A', 'ABC') );
// Check(2, StrCount('A', 'ABCABC') );
// Check(3, StrCount('ABC', 'ABCABCABC') );
// Check(2, StrCount('BCA', 'ABCABCABC') );
// Check(1, StrCount('AA', 'AAA') );
//
// Check(0, StrCount('A', '', ifByte) );
// Check(0, StrCount('ABC', '', ifByte) );
// Check(0, StrCount('', 'A', ifByte) );
// Check(0, StrCount('', 'ABC', ifByte) );
// Check(1, StrCount('A', 'A', ifByte) );
// Check(3, StrCount('A', 'AAA', ifByte) );
// Check(1, StrCount('A', 'ABC', ifByte) );
// Check(2, StrCount('A', 'ABCABC', ifByte) );
// Check(3, StrCount('ABC', 'ABCABCABC', ifByte) );
// Check(2, StrCount('BCA', 'ABCABCABC', ifByte) );
// Check(2, StrCount('AA', 'AAA', ifByte) );
//
// Check(0, StrCount('A', '') );
// Check(0, StrCount('ABC', '') );
// Check(0, StrCount('', 'A') );
// Check(0, StrCount('', 'ABC') );
// Check(1, StrCount('A', 'A') );
// Check(3, StrCount('A', 'AAA') );
// Check(1, StrCount('A', 'ABC') );
// Check(2, StrCount('A', 'ABCABC') );
// Check(3, StrCount('ABC', 'ABCABCABC') );
// Check(2, StrCount('BCA', 'ABCABCABC') );
// Check(1, StrCount('AA', 'AAA') );
//
// Check(0, StrCount('A', '', ifByte) );
// Check(0, StrCount('ABC', '', ifByte) );
// Check(0, StrCount('', 'A', ifByte) );
// Check(0, StrCount('', 'ABC', ifByte) );
// Check(1, StrCount('A', 'A', ifByte) );
// Check(3, StrCount('A', 'AAA', ifByte) );
// Check(1, StrCount('A', 'ABC', ifByte) );
// Check(2, StrCount('A', 'ABCABC', ifByte) );
// Check(3, StrCount('ABC', 'ABCABCABC', ifByte) );
// Check(2, StrCount('BCA', 'ABCABCABC', ifByte) );
// Check(2, StrCount('AA', 'AAA', ifByte) );
end;
procedure testStringCount;
begin
Check(0, StringCount('A', '') );
Check(0, StringCount('ABC', '') );
Check(0, StringCount('', 'A') );
Check(0, StringCount('', 'ABC') );
Check(1, StringCount('A', 'A') );
Check(3, StringCount('A', 'AAA') );
Check(1, StringCount('A', 'ABC') );
Check(2, StringCount('A', 'ABCABC') );
Check(3, StringCount('ABC', 'ABCABCABC') );
Check(2, StringCount('BCA', 'ABCABCABC') );
Check(1, StringCount('AA', 'AAA') );
Check(2, StringCount('AA', 'AAAAA') );
Check(0, StringCount('A', '', scfIncChar) );
Check(0, StringCount('ABC', '', scfIncChar) );
Check(0, StringCount('', 'A', scfIncChar) );
Check(0, StringCount('', 'ABC', scfIncChar) );
Check(1, StringCount('A', 'A', scfIncChar) );
Check(3, StringCount('A', 'AAA', scfIncChar) );
Check(1, StringCount('A', 'ABC', scfIncChar) );
Check(2, StringCount('A', 'ABCABC', scfIncChar) );
Check(3, StringCount('ABC', 'ABCABCABC', scfIncChar) );
Check(2, StringCount('BCA', 'ABCABCABC', scfIncChar) );
Check(2, StringCount('AA', 'AAA', scfIncChar) );
Check(4, StringCount('AA', 'AAAAA', scfIncChar) );
Check(0, StringCount('A', '') );
Check(0, StringCount('ABC', '') );
Check(0, StringCount('', 'A') );
Check(0, StringCount('', 'ABC') );
Check(1, StringCount('A', 'A') );
Check(3, StringCount('A', 'AAA') );
Check(1, StringCount('A', 'ABC') );
Check(2, StringCount('A', 'ABCABC') );
Check(3, StringCount('ABC', 'ABCABCABC') );
Check(2, StringCount('BCA', 'ABCABCABC') );
Check(1, StringCount('AA', 'AAA') );
Check(2, StringCount('AA', 'AAAAA') );
Check(0, StringCount('A', '', scfIncChar) );
Check(0, StringCount('ABC', '', scfIncChar) );
Check(0, StringCount('', 'A', scfIncChar) );
Check(0, StringCount('', 'ABC', scfIncChar) );
Check(1, StringCount('A', 'A', scfIncChar) );
Check(3, StringCount('A', 'AAA', scfIncChar) );
Check(1, StringCount('A', 'ABC', scfIncChar) );
Check(2, StringCount('A', 'ABCABC', scfIncChar) );
Check(3, StringCount('ABC', 'ABCABCABC', scfIncChar) );
Check(2, StringCount('BCA', 'ABCABCABC', scfIncChar) );
Check(2, StringCount('AA', 'AAA', scfIncChar) );
Check(4, StringCount('AA', 'AAAAA' , scfIncChar));
end;
procedure testCheckStrInTable;
begin
Check(itAllInclude, CheckStrInTable('123', '0123456789'));
Check(itAllExclude, CheckStrInTable('ABC', '0123456789'));
Check(itPartInclude, CheckStrInTable('ABC123', '0123456789'));
Check(itUnknown, CheckStrInTable('', '0123456789'));
Check(itUnknown, CheckStrInTable('123', ''));
Check(itAllInclude, CheckStrInTable('123123321123', '123'));
Check(itAllExclude, CheckStrInTable('ABCABCCBAABC', '123'));
Check(itPartInclude, CheckStrInTable('ABC123', '123'));
end;
procedure testCheckCharInTable;
begin
Check(True, CheckCharInTable('1', '0123456789'));
Check(False, CheckCharInTable('A', '0123456789'));
Check(False, CheckCharInTable('1', ''));
end;
procedure testInStr;
begin
Check(True, InStr('A', 'ABC'));
Check(True, InStr('B', 'ABC'));
Check(True, InStr('C', 'ABC'));
Check(True, InStr('あ', 'あいう'));
Check(True, InStr('い', 'あいう'));
Check(True, InStr('う', 'あいう'));
Check(False, InStr('a', 'ABC'));
Check(False, InStr('A', 'abc'));
Check(False, InStr('え', 'あいう'));
Check(True, InStr('99', '699'));
Check(True, InStr('99', '99'));
Check(True, InStr('99', '9900'));
Check(False, InStr('99', '698'));
Check(False, InStr('99', '98'));
Check(False, InStr('99', '9800'));
end;
procedure testChangeLineBreakes;
procedure CheckFunc(Source, CRLFResult, CRResult, LFResult: String);
begin
Check(CRLFResult, ChangeLineBreakes(Source, lbsCRLF));
Check(CRResult , ChangeLineBreakes(Source, lbsCR));
Check(LFResult , ChangeLineBreakes(Source, lbsLF));
Check(CRResult , ChangeLineBreakes(CRLFResult, lbsCR));
Check(LFResult , ChangeLineBreakes(CRLFResult, lbsLF));
Check(CRLFResult, ChangeLineBreakes(CRResult , lbsCRLF));
Check(LFResult , ChangeLineBreakes(CRResult , lbsLF));
Check(CRLFResult, ChangeLineBreakes(LFResult , lbsCRLF));
Check(CRResult , ChangeLineBreakes(LFResult , lbsCR));
end;
//2009/01/07
//※lbsNoLineBreaksについては未テスト
begin
//普通に変換
CheckFunc(#10#13+'あいう'+#10#13+'えお'+#10#13,
#13#10+'あいう'+#13#10+'えお'+#13#10,
#13 +'あいう'+#13 +'えお'+#13,
#10 +'あいう'+#10 +'えお'+#10);
//混在
CheckFunc(#13#10+'123'+#10#13+'ABC'+#13 +'あいう'+#10 ,
#13#10+'123'+#13#10+'ABC'+#13#10+'あいう'+#13#10,
#13 +'123'+#13 +'ABC'+#13 +'あいう'+#13 ,
#10 +'123'+#10 +'ABC'+#10 +'あいう'+#10 );
//連続
CheckFunc(#10#13#10#13+'ABC'+#10#13#10#13+'あいう'+#10#13#10#13,
#13#10#13#10+'ABC'+#13#10#13#10+'あいう'+#13#10#13#10,
#13#13 +'ABC'+#13#13 +'あいう'+#13#13 ,
#10#10 +'ABC'+#10#10 +'あいう'+#10#10 );
//混在して連続
CheckFunc(#10#13#13 +'123'+#13#13#10 +'ABC'+#10#10#13 ,
#13#10#13#10+'123'+#13#10#13#10+'ABC'+#13#10#13#10,
#13#13 +'123'+#13#13 +'ABC'+#13#13 ,
#10#10 +'123'+#10#10 +'ABC'+#10#10 );
CheckFunc(#10#13#13 +'123'+#13#10#13 +'あいう'+#10#13#10 ,
#13#10#13#10+'123'+#13#10#13#10+'あいう'+#13#10#13#10,
#13#13 +'123'+#13#13 +'あいう'+#13#13 ,
#10#10 +'123'+#10#10 +'あいう'+#10#10 );
//改行数のチェック
CheckFunc(#10#13#10#13+
'ABC'+#10#13#10#13#10#13+
'123'+#10#13#10#13#10#13#10#13+
'あいうえ'+#10#13#10#13#10#13#10#13#10#13,
#13#10#13#10+
'ABC'+#13#10#13#10#13#10+
'123'+#13#10#13#10#13#10#13#10+
'あいうえ'+#13#10#13#10#13#10#13#10#13#10,
#13#13 +
'ABC'+#13#13#13 +
'123'+#13#13#13#13 +
'あいうえ'+#13#13#13#13#13 ,
#10#10 +
'ABC'+#10#10#10 +
'123'+#10#10#10#10 +
'あいうえ'+#10#10#10#10#10 );
end;
procedure testStringsReplace;
var
OldPatterns, NewPatterns: TStringDynArray;
begin
SetLength(OldPatterns, 4);
SetLength(NewPatterns, 4);
OldPatterns[0] := '123'; NewPatterns[0] := 'ABC';
OldPatterns[1] := 'ABC'; NewPatterns[1] := '123';
OldPatterns[2] := '456'; NewPatterns[2] := 'DEF';
OldPatterns[3] := 'DEF'; NewPatterns[3] := '456';
Check('123ABC123ABC456DEF',
StringsReplace('ABC123ABC123DEF456', OldPatterns, NewPatterns));
Check('abcABCabcABCdefDEF',
StringsReplace('abc123abc123def456', OldPatterns, NewPatterns));
Check('123ABC123ABC456DEF',
StringsReplace('abc123abc123def456', OldPatterns, NewPatterns, ccIgnoreCase));
//オープン配列パラメータ指定
Check('123ABC123ABC456DEF',
StringsReplace('ABC123ABC123DEF456',
['123', 'ABC', '456', 'DEF'],
['ABC', '123', 'DEF', '456'], ccCaseSensitive));
Check('123ABC123ABC456DEF',
StringsReplace('abc123ABC123def456',
['123', 'ABC', '456', 'DEF'],
['ABC', '123', 'DEF', '456'], ccIgnoreCase, True));
Check('abcABC123ABCdefDEF',
StringsReplace('abc123ABC123def456',
['123', 'ABC', '456', 'DEF'],
['ABC', '123', 'DEF', '456'], ccCaseSensitive, True));
Check('abcABCABC123def456',
StringsReplace('abc123ABC123def456',
['123', 'ABC', '456', 'DEF'],
['ABC', '123', 'DEF', '456'], ccCaseSensitive, False));
Check('123123ABC123def456',
StringsReplace('abc123ABC123def456',
['123', 'ABC', '456', 'DEF'],
['ABC', '123', 'DEF', '456'], ccIgnoreCase, False));
SetLength(OldPatterns, 1);
SetLength(NewPatterns, 1);
OldPatterns[0] := '
'; NewPatterns[0] := #13#10;
Check(#13#10#13#10#13#10#13#10,
StringsReplace('
', OldPatterns, NewPatterns, ccIgnoreCase));
SetLength(OldPatterns, 2);
SetLength(NewPatterns, 2);
OldPatterns[0] := 'A'; NewPatterns[0] := 'B';
OldPatterns[1] := 'B'; NewPatterns[1] := 'A';
Check('BA', StringsReplace('AB', OldPatterns, NewPatterns, ccIgnoreCase));
SetLength(OldPatterns, 1);
SetLength(NewPatterns, 1);
OldPatterns[0] := 'function'; NewPatterns[0] := 'FUNCTION';
Check(' FUNCTION ',
StringsReplace(' function ', OldPatterns, NewPatterns, ccIgnoreCase));
end;
procedure testEncodeEscapeSequence;
begin
Check('ABC\r\nDEF\r\n', EncodeEscapeSequence('ABC'#13#10'DEF'#13#10));
Check('ABC\rDEF\n', EncodeEscapeSequence('ABC'#13'DEF'#10));
Check('\rDEF\n', EncodeEscapeSequence(#13'DEF'#10));
Check('\\rDEF\\n', EncodeEscapeSequence('\rDEF\n'));
Check('\\\\rDEF\\\\n', EncodeEscapeSequence('\\rDEF\\n'));
Check('ABC\r\nDEF\r\n', EncodeEscapeSequence('ABC'#13#10'DEF'#13#10));
end;
procedure testDecodeEscapeSequence;
begin
Check('ABC'#13#10'DEF'#13#10, DecodeEscapeSequence('ABC\r\nDEF\r\n'));
Check('ABC'#13'DEF'#10, DecodeEscapeSequence('ABC\rDEF\n'));
Check(#13'DEF'#10, DecodeEscapeSequence('\rDEF\n'));
Check(#13'DEF'#10, DecodeEscapeSequence('\RDEF\N'));
Check('\rDEF\n', DecodeEscapeSequence('\\rDEF\\n'));
Check('\\rDEF\\n', DecodeEscapeSequence('\\\\rDEF\\\\n'));
Check('ABC'#13#10'DEF'#13#10, DecodeEscapeSequence('ABC\R\NDEF\R\N'));
end;
procedure testEncodeStringToCodeCsv;
begin
Check('41,00,42,00,43,00,42,30,44,30,46,30',
EncodeStringToUTF16CodeCsv('ABCあいう'));
Check('41,00,42,00,43,00,42,30,44,30,46,30',
EncodeStringToUTF16CodeCsv('ABCあいう', eLittle));
Check('00,41,00,42,00,43,30,42,30,44,30,46',
EncodeStringToUTF16CodeCsv('ABCあいう', eBig));
Check('ABCあいう',
DecodeStringFromUTF16CodeCsv('41,00,42,00,43,00,42,30,44,30,46,30'));
Check('ABCあいう',
DecodeStringFromUTF16CodeCsv('41,00,42,00,43,00,42,30,44,30,46,30', eLittle));
Check('ABCあいう',
DecodeStringFromUTF16CodeCsv('00,41,00,42,00,43,30,42,30,44,30,46', eBig));
Check('ABCあいう',
DecodeStringFromUTF16CodeCsv(EncodeStringToUTF16CodeCsv('ABCあいう')));
Check('41,42,43,61,62,63,E3,81,82,E3,81,84,E3,81,86',
EncodeStringToUTF8CodeCsv('ABCabcあいう'));
Check('ABCabcあいう',
DecodeStringFromUTF8CodeCsv('41,42,43,61,62,63,E3,81,82,E3,81,84,E3,81,86'));
end;
procedure testStringsWordReplace;
var
OldPatterns, NewPatterns: TStringDynArray;
begin
SetLength(OldPatterns, 1);
SetLength(NewPatterns, 1);
OldPatterns[0] := 'select'; NewPatterns[0] := 'SELECT';
Check('SELECT',
StringsWordReplace('seleCT', OldPatterns, NewPatterns, ccIgnoreCase));
Check(' SELECT ',
StringsWordReplace(' seleCT ', OldPatterns, NewPatterns, ccIgnoreCase));
Check('SELECT1',
StringsWordReplace('seleCT1', OldPatterns, NewPatterns, ccIgnoreCase));
Check('_seleCT',
StringsWordReplace('_seleCT', OldPatterns, NewPatterns, ccIgnoreCase));
OldPatterns[0] := 'ttp://'; NewPatterns[0] := 'http://';
Check('http:// ',
StringsWordReplace('ttp:// ', OldPatterns, NewPatterns, ccIgnoreCase));
Check(' http:// ',
StringsWordReplace(' ttp:// ', OldPatterns, NewPatterns, ccIgnoreCase));
Check(' http:// ',
StringsWordReplace(' http:// ', OldPatterns, NewPatterns, ccIgnoreCase));
Check('http://www',
StringsWordReplace('ttp://www', OldPatterns, NewPatterns, ccIgnoreCase));
Check(' http://www',
StringsWordReplace(' ttp://www', OldPatterns, NewPatterns, ccIgnoreCase));
Check(' http://www',
StringsWordReplace(' http://www', OldPatterns, NewPatterns, ccIgnoreCase));
SetLength(OldPatterns, 2);
SetLength(NewPatterns, 2);
OldPatterns[0] := ''''; NewPatterns[0] := '"';
OldPatterns[1] := '"'; NewPatterns[1] := '''';
Check('''"''',
StringsWordReplace('"''"', OldPatterns, NewPatterns, ccIgnoreCase));
Check('s"',
StringsWordReplace('s''', OldPatterns, NewPatterns, ccIgnoreCase));
Check('''s',
StringsWordReplace('"s', OldPatterns, NewPatterns, ccIgnoreCase));
Check('se"l"ect',
StringsWordReplace('se''l''ect', OldPatterns, NewPatterns, ccIgnoreCase));
Check('''se"l"ect''',
StringsWordReplace('"se''l''ect"', OldPatterns, NewPatterns, ccIgnoreCase));
Check('"se''l''ect"',
StringsWordReplace('''se"l"ect''', OldPatterns, NewPatterns, ccIgnoreCase));
end;
procedure testTrimFirstChar;
begin
Check('あ', TrimFirstChar(' あ', ' '));
Check('う', TrimFirstChar(' う', ' '));
Check('か', TrimFirstChar('あいうえおああいいか', 'あいうえお'));
Check('X', TrimFirstChar(' YY X', ' Y'));
Check('789', TrimFirstChar('012346789', '6543210'));
Check('ABCXYZ', TrimFirstChar('XXYYZZABCXYZ', 'XYZ'));
Check('', TrimFirstChar('XXYYZZABCXYZ', 'XYZABC'));
Check('AAA'#9, TrimFirstChar(#9'AAA'#9, #9));
Check(#9, TrimFirstChar('AAA'#9, 'A'));
Check(#9'AAA', TrimFirstChar(#13#10#9'AAA', #13#10));
Check(#13#10, TrimFirstChar(#9'AAA'#13#10, #9'A'));
Check('123', TrimFirstChar('123', ''));
Check('', TrimFirstChar('', '123'));
end;
procedure testTrimLastChar;
begin
Check(' ', TrimLastChar(' あ', 'あ'));
Check('あいうえお', TrimLastChar('あいうえおああいいか', 'かいあ'));
Check('う', TrimLastChar('う ', ' '));
Check(' YY', TrimLastChar(' YY X', ' X'));
Check('012', TrimLastChar('012346789', '346789'));
Check('XXYYZZABC', TrimLastChar('XXYYZZABCXYZ', 'XYZ'));
Check('', TrimLastChar('XXYYZZABCXYZ', 'XYZABC'));
Check(#9'AAA', TrimLastChar(#9'AAA'#9, #9));
Check(#9, TrimLastChar(#9'AAA', 'A'));
Check(#9'AAA', TrimLastChar(#9'AAA'#13#10, #13#10));
Check(#13#10, TrimLastChar(#13#10#9'AAA', #9'A'));
Check('123', TrimLastChar('123', ''));
Check('', TrimLastChar('', '123'));
end;
procedure testTrimChar;
begin
Check('ABC', TrimChar('123467829ABC5212694192', '0123456789'));
Check('', TrimChar('123467829ABC5212694192', 'ABC0123456789'));
Check('かきくけこ', TrimChar('あいうえああいいかきくけこおおえういあい', 'あいうえお'));
Check('AAA', TrimChar(#9'AAA'#9, #9));
Check(#9, TrimChar(#9'AAA', 'A'));
Check(#9'AAA', TrimChar(#9'AAA'#13#10, #13#10));
Check(#13#10, TrimChar(#9'AAA'#13#10, #9'A'));
Check('123', TrimChar('123', ''));
Check('ABC DEF', TrimChar(' ABC DEF'#13#10' '#13#10, CtrlCharTbl+' '));
end;
procedure testOneTrimChar;
begin
Check('ABC', OneTrimChar('"ABC"', '"'));
Check('"ABC', OneTrimChar('"ABC', '"'));
Check('ABC"', OneTrimChar('ABC"', '"'));
Check('"ABC"',OneTrimChar('"ABC"', ''));
Check('ABC', OneTrimChar('ABC', ''));
Check('ABC', OneTrimChar('ABC', '"'));
Check('ABC', OneTrimChar('1ABC1', '123'));
Check('2ABC2', OneTrimChar('12ABC21', '123'));
Check('1ABC2',OneTrimChar('1ABC2', '123'));
Check('', OneTrimChar('', '"'));
end;
procedure testGetTagInfo;
procedure CheckTagInfo(StartValue, EndValue: Integer; TagInfo: TTagTextInfo);
begin
Check(StartValue, TagInfo.StartIndex);
Check(EndValue, TagInfo.EndIndex);
end;
var
TagInfo: TTagTextInfo;
begin
CheckTagInfo(1, 5, GetTagInfo('<', '>', ''));
CheckTagInfo(0, 4, GetTagInfo('<', '>', 'abc>'));
CheckTagInfo(1, 0, GetTagInfo('<', '>', '', 'abc<'));
CheckTagInfo(5, 0, GetTagInfo('<', '>', 'abc><'));
CheckTagInfo(5, 9, GetTagInfo('<', '>', 'abc>'));
CheckTagInfo(2, 4, GetTagInfo('a', 'c', ''));
CheckTagInfo(2, 4, GetTagInfo('a', 'a', ''));
CheckTagInfo(5, 0, GetTagInfo('>', '>', ''));
CheckTagInfo(5, 0, GetTagInfo('>', '', ''));
CheckTagInfo(0, 5, GetTagInfo('', '>', ''));
CheckTagInfo(0, 4, GetTagInfo('>', '<', 'abc<'));
CheckTagInfo(4, 5, GetTagInfo('>', '<', 'abc><'));
CheckTagInfo(4, 5, GetTagInfo('>', '<', 'abc>'));
CheckTagInfo(4, 8, GetTagInfo('>', '<', 'abc>123'));
end;
procedure testGetTagText;
begin
Check('', GetTagText('<', '>', ''));
Check('', GetTagText('<', '>', 'abc>'));
Check('', '', 'abc<'));
Check('<', GetTagText('<', '>', 'abc><'));
Check('', GetTagText('<', '>', 'abc>'));
Check('abc', GetTagText('a', 'c', ''));
Check('aba', GetTagText('a', 'a', ''));
Check('>', GetTagText('>', '>', ''));
Check('>', GetTagText('>', '', ''));
Check('', GetTagText('' , '>', ''));
Check('', GetTagText('>', '<', 'abc<'));
Check('><', GetTagText('>', '<', 'abc><'));
Check('><', GetTagText('>', '<', 'abc>'));
Check('>123<', GetTagText('>', '<', 'abc>123'));
// Check('aaa', GetTagText('<', '>', ''));
// Check('aaa', GetTagText('<', '>', 'abcdef'));
// Check('aaa', GetTagText('"', '"', '"aaa"'));
// Check('aaa', GetTagText('"', '"', 'abc"aaa"def'));
end;
type TWideCharByteLength = (wcblSingle, wcblMulti);
procedure testWideCharByteLength;
begin
Check(wcblSingle, WideCharByteLength(WideChar(WideString('a')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('g')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('z')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('A')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('G')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('Z')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('\')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('[')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString(']')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('^')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('`')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('_')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('~')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('{')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('|')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('}')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('。')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('「')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('」')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('、')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('・')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('ヲ')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('ァ')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('ィ')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('ゥ')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('ェ')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('ォ')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('ャ')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('ュ')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('ョ')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('ッ')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('ー')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('ア')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('イ')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('ウ')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('エ')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('オ')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('カ')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('キ')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('ク')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('ケ')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('コ')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('サ')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('シ')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('ス')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('セ')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('ソ')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('タ')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('チ')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('ツ')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('テ')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('ト')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('ナ')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('ニ')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('ヌ')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('ネ')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('ノ')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('ハ')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('ヒ')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('フ')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('ヘ')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('ホ')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('マ')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('ミ')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('ム')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('メ')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('モ')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('ヤ')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('ユ')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('ヨ')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('ラ')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('リ')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('ル')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('レ')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('ロ')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('ワ')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('ン')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('゙')[1])));
Check(wcblSingle, WideCharByteLength(WideChar(WideString('゚')[1])));
Check(wcblMulti, WideCharByteLength(WideChar(WideString('a')[1])));
Check(wcblMulti, WideCharByteLength(WideChar(WideString('A')[1])));
end;
procedure testCharIndexToByteIndex;
begin
Check(1, CharIndexToByteIndex('a', 1));
Check(2, CharIndexToByteIndex('a', 2));
Check(0, CharIndexToByteIndex('a', 3));
Check(2, CharIndexToByteIndex('abc', 2));
Check(4, CharIndexToByteIndex('abc', 4));
Check(1, CharIndexToByteIndex('あ', 1));
Check(3, CharIndexToByteIndex('あ', 2));
Check(0, CharIndexToByteIndex('あ', 3));
Check(3, CharIndexToByteIndex('あいう', 2));
Check(7, CharIndexToByteIndex('あいう', 4));
Check(8, CharIndexToByteIndex('123123abcABC', 6));
Check(11, CharIndexToByteIndex('123123abcABC', 8));
Check(15, CharIndexToByteIndex('123123abcABC', 11));
Check(19, CharIndexToByteIndex('123123abcABC', 13));
Check( 0, CharIndexToByteIndex('123123abcABC', 14));
end;
procedure testByteIndexToCharIndex;
begin
Check(1, ByteIndexToCharIndex('a', 1));
Check(0, ByteIndexToCharIndex('a', 2));
Check(0, ByteIndexToCharIndex('a', 3));
Check(2, ByteIndexToCharIndex('abc', 2));
Check(0, ByteIndexToCharIndex('abc', 4));
Check(1, ByteIndexToCharIndex('あ', 1));
Check(1, ByteIndexToCharIndex('あ', 2));
Check(0, ByteIndexToCharIndex('あ', 3));
Check(1, ByteIndexToCharIndex('あいう', 2));
Check(2, ByteIndexToCharIndex('あいう', 4));
Check(5, ByteIndexToCharIndex('123123abcABC', 6));
Check(6, ByteIndexToCharIndex('123123abcABC', 8));
Check(8, ByteIndexToCharIndex('123123abcABC', 11));
Check(10, ByteIndexToCharIndex('123123abcABC', 13));
Check(11, ByteIndexToCharIndex('123123abcABC', 15));
Check(12, ByteIndexToCharIndex('123123abcABC', 18));
Check( 0, ByteIndexToCharIndex('123123abcABC', 19));
end;
procedure testCharLengthToByteLength;
begin
Check(1, ByteLength('a'));
Check(1, ByteLength('A'));
Check(2, ByteLength('a'));
Check(2, ByteLength('A'));
Check(18, ByteLength('123123abcABC'));
Check(25, ByteLength('確認に使用したコード12345'));
end;
procedure testIncludeBothEndsStr;
begin
Check('"テスト"', IncludeBothEndsStr('テスト', '"'));
Check('"テスト"', IncludeBothEndsStr('"テスト', '"'));
Check('"テスト"', IncludeBothEndsStr('テスト"', '"'));
Check('"テスト"', IncludeBothEndsStr('"テスト"', '"'));
Check('テスト', IncludeBothEndsStr('テスト', ''));
Check('テスト', IncludeBothEndsStr('テスト', ''));
Check('テスト', IncludeBothEndsStr('テスト', ''));
Check('テスト', IncludeBothEndsStr('テスト', ''));
Check('""', IncludeBothEndsStr('', '"'));
end;
procedure testExcludeBothEndsStr;
begin
Check('テスト', ExcludeBothEndsStr('"テスト"', '"'));
Check('テスト', ExcludeBothEndsStr('"テスト', '"'));
Check('テスト', ExcludeBothEndsStr('テスト"', '"'));
Check('テスト', ExcludeBothEndsStr('テスト', '"'));
Check('テスト', ExcludeBothEndsStr('テスト', ''));
Check('テスト', ExcludeBothEndsStr('テスト', ''));
Check('テスト', ExcludeBothEndsStr('テスト', ''));
Check('テスト', ExcludeBothEndsStr('テスト', ''));
Check('', ExcludeBothEndsStr('""', '"'));
end;
end.