[fpc-pascal] ansistrings access by character
Florian Klaempfl
F.Klaempfl at gmx.de
Sun Jul 10 10:52:34 CEST 2005
L505 wrote:
> |
> | Or if you wanted to prepend exceptions rather than catch them, the following
> | function could also work have worked (note you have to do a shortString cast,
> | AnsiStrings don't like access by character)
> |
>
> I was under the same impression for a long time, but some of the FPC RTL sources
> use ansistrings and access by character. The findpart function for example uses
> ansistrings but accesses by string[n]
>
>
> For example this compiles and runs.
>
> var
> test:ansistring;
> function IsANumber (InStr : AnsiString) : Boolean;
> var
> TempBool: Boolean;
> Str: ansiString;
> I: Integer;
> begin
> Str := InStr;
> TempBool := True;
> I := 1;
>
> while (I <= Length(Str)) and (TempBool = True) do
> begin
> TempBool := Str[I] in ['0'..'9'];
> Inc(I);
> end;
> IsANumber := TempBool
> end;
>
> begin
> test:='testing';
> test[1]:='p';
> writeln(test);
>
> test:='5453tyy';
> if IsANumber(test) then
> writeln('yes '+test+' is a number')
> else
> writeln('no, '+test+' is not a number');
>
> test:='545365';
> if IsANumber(test) then
> writeln('yes '+test+' is a number')
> else
> writeln('no, '+test+' is not a number');
>
> readln;
>
> end.
>
>
> So can someone shed the light? Is it just a rumor that ansistrings shouldn't be
> accessed by character? Only specific situations?
Disadvantage:
- it's slow because it calls unique,i.e. ensures that the reference
counter is 1
More information about the fpc-pascal
mailing list