Changeset 117


Ignore:
Timestamp:
Apr 26, 2007, 1:53:33 PM (18 years ago)
Author:
RBRi
Message:

Pchar utils added

Location:
trunk/NewView
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/NewView/CharUtilsUnit.pas

    r112 r117  
    2727  Function CharIsAlpha(const aChar : char) : boolean;
    2828
     29
     30  // ---------------
     31  // ---- PChar ----
     32  // ---------------
     33
     34 
     35  // Converts a PChar into a String like StrPas
     36  // but conversts at least the first aLength chars
     37  Function StrPasWithLength(const aPChar: PChar; const aLength: integer) : String;
     38
     39  // Returns the difference of the pointers
     40  Function PCharPointerDiff(const aMinuend: PChar; const aSubtrahend : PChar ) : Longword;
     41
    2942Implementation
    3043
     
    4356
    4457
     58  // ---------------
     59  // ---- PChar ----
     60  // ---------------
     61
     62
     63  // Converts a PChar into a String like StrPas
     64  // but conversts at least the first aLength chars
     65  Function StrPasWithLength(const aPChar: PChar; const aLength: integer) : String;
     66  var
     67    i: integer;
     68  begin
     69    Result := '';
     70    i := 0;
     71    while (aPChar[i] <> #0) and (i < aLength) do
     72    begin
     73      Result := Result + aPChar[i];
     74      inc( i );
     75    end;
     76  end;
     77
     78
     79  Function PCharPointerDiff(const aMinuend: PChar; const aSubtrahend : PChar ) : Longword;
     80  begin
     81    Result := Longword(aMinuend) - Longword(aSubtrahend);
     82  end;
     83
     84
    4585END.
  • trunk/NewView/unittests/CharUtilsUnitTests.pas

    r112 r117  
    55uses
    66  Classes,
     7  SysUtils,
    78  TestAssert,
    89  CharUtilsUnit;
     
    136137
    137138
     139  // ---------------
     140  // ---- PChar ----
     141  // ---------------
     142
     143
     144  PROCEDURE testStrPasWithLength_Empty;
     145  VAR
     146    tmpPChar : PChar;
     147    tmpResult : String;
     148  BEGIN
     149    tmpPChar := '';
     150    tmpResult := StrPasWithLength(tmpPChar, 0);
     151
     152    assertEqualsAnsiString('testStrPasWithLength_Empty', '', tmpResult);
     153  END;
     154
     155
     156  PROCEDURE testStrPasWithLength_EmptyLengt11;
     157  VAR
     158    tmpPChar : PChar;
     159    tmpResult : String;
     160  BEGIN
     161    tmpPChar := '';
     162    tmpResult := StrPasWithLength(tmpPChar, 11);
     163
     164    assertEqualsAnsiString('testStrPasWithLength_EmptyLengt11', '', tmpResult);
     165  END;
     166
     167
     168  PROCEDURE testStrPasWithLength_Lengt0;
     169  VAR
     170    tmpPChar : PChar;
     171    tmpResult : String;
     172  BEGIN
     173    tmpPChar := 'abc';
     174    tmpResult := StrPasWithLength(tmpPChar, 0);
     175
     176    assertEqualsAnsiString('testStrPasWithLength_Lengt0', '', tmpResult);
     177  END;
     178
     179
     180  PROCEDURE testStrPasWithLength_FirstChar;
     181  VAR
     182    tmpPChar : PChar;
     183    tmpResult : String;
     184  BEGIN
     185    tmpPChar := 'abc';
     186    tmpResult := StrPasWithLength(tmpPChar, 1);
     187
     188    assertEqualsAnsiString('testStrPasWithLength_FirstChar', 'a', tmpResult);
     189  END;
     190
     191
     192  PROCEDURE testStrPasWithLength_ExactLength;
     193  VAR
     194    tmpPChar : PChar;
     195    tmpResult : String;
     196  BEGIN
     197    tmpPChar := 'abc';
     198    tmpResult := StrPasWithLength(tmpPChar, 3);
     199
     200    assertEqualsAnsiString('testStrPasWithLength_ExactLength', 'abc', tmpResult);
     201  END;
     202
     203
     204  PROCEDURE testStrPasWithLength_LengthOneToBig;
     205  VAR
     206    tmpPChar : PChar;
     207    tmpResult : String;
     208  BEGIN
     209    tmpPChar := 'abc';
     210    tmpResult := StrPasWithLength(tmpPChar, 4);
     211
     212    assertEqualsAnsiString('testStrPasWithLength_LengthOneToBig', 'abc', tmpResult);
     213  END;
     214
     215
     216  PROCEDURE testStrPasWithLength_LengthNegative;
     217  VAR
     218    tmpPChar : PChar;
     219    tmpResult : String;
     220  BEGIN
     221    tmpPChar := 'abc';
     222    tmpResult := StrPasWithLength(tmpPChar, -4);
     223
     224    assertEqualsAnsiString('testStrPasWithLength_LengthNegative', '', tmpResult);
     225  END;
     226
     227
     228  // ----------------------------------------------------------
     229
     230  PROCEDURE testPCharPointerDiff_Same;
     231  VAR
     232    tmpMinuend : PChar;
     233    tmpSubtrahend : PChar;
     234    tmpResult : Longword;
     235  BEGIN
     236    tmpMinuend := 'abc';
     237    tmpSubtrahend := tmpMinuend;
     238    tmpResult := PCharPointerDiff(tmpMinuend, tmpSubtrahend);
     239
     240    assertEqualsLongWord('testPCharPointerDiff_Same', 0, tmpResult);
     241  END;
     242
     243
     244  PROCEDURE testPCharPointerDiff;
     245  VAR
     246    tmpMinuend : PChar;
     247    tmpSubtrahend : PChar;
     248    tmpResult : Longword;
     249  BEGIN
     250    tmpSubtrahend := 'abcdef';
     251    tmpMinuend := tmpSubtrahend + 2;
     252    assertEqualsString('testPCharPointerDiff', 'cdef', StrPas(tmpMinuend));
     253
     254    tmpResult := PCharPointerDiff(tmpMinuend, tmpSubtrahend);
     255
     256    assertEqualsLongWord('testPCharPointerDiff_Same', 2, tmpResult);
     257  END;
     258
     259
    138260  // ----------------------------------------------------------
    139261
     
    149271    result.add(@testCharIsAlpha_False);
    150272
     273
     274    result.add(@testStrPasWithLength_Empty);
     275    result.add(@testStrPasWithLength_EmptyLengt11);
     276    result.add(@testStrPasWithLength_Lengt0);
     277    result.add(@testStrPasWithLength_FirstChar);
     278    result.add(@testStrPasWithLength_ExactLength);
     279    result.add(@testStrPasWithLength_LengthOneToBig);
     280    result.add(@testStrPasWithLength_LengthNegative);
     281
     282    result.add(@testPCharPointerDiff_Same);
     283    result.add(@testPCharPointerDiff);
     284
    151285  END;
    152286
Note: See TracChangeset for help on using the changeset viewer.