Главная страница
Top.Mail.Ru    Яндекс.Метрика
Текущий архив: 2006.02.19;
Скачать: CL | DM;

Вниз

Тип String   Найти похожие ветки 

 
ArtemESC ©   (2006-01-28 08:59) [0]

Доброго времени суток...
Как в Delphi тип String устроен? В смысле
как с ним работать на асме...


 
YurikGL ©   (2006-01-28 09:02) [1]


> Как в Delphi тип String устроен?

String types

A string represents a sequence of characters. Delphi supports the following predefined string types.

String types
Type Maximum length Memory required Used for
ShortString 255 characters 2 to 256 bytes backward compatibility
AnsiString ~2^31 characters 4 bytes to 2GB 8-bit (ANSI) characters, DBCS ANSI, MBCS ANSI, etc.
WideString ~2^30 characters 4 bytes to 2GB Unicode characters; multi-user servers and multi-language applications
AnsiString, sometimes called the long string, is the preferred type for most purposes.
String types can be mixed in assignments and expressions; the compiler automatically performs required conversions. But strings passed by reference to a function or procedure (as var and out parameters) must be of the appropriate type. Strings can be explicitly cast to a different string type (see Typecasts).
The reserved word string functions like a generic type identifier. For example,

var S: string;

creates a variable S that holds a string. In the default {$H+} state, the compiler interprets string (when it appears without a bracketed number after it) as AnsiString. Use the {$H-} directive to turn string into ShortString.

The standard function Length returns the number of characters in a string. The SetLength procedure adjusts the length of a string.

Comparison of strings is defined by the ordering of the characters in corresponding positions. Between strings of unequal length, each character in the longer string without a corresponding character in the shorter string takes on a greater-than value. For example, "AB" is greater than "A"; that is, "AB" > "A" returns True. Zero-length strings hold the lowest values.

You can index a string variable just as you would an array. If S is a string variable and i an integer expression, S[i] represents the ith character--or, strictly speaking, the ith byte--in S. For a ShortString or AnsiString, S[i] is of type AnsiChar; for a WideString, S[i] is of type WideChar. For single-byte (Western) locales, MyString[2] := "A"; assigns the value A to the second character of MyString. The following code uses the standard AnsiUpperCase function to convert MyString to uppercase.

var I: Integer;
begin
 I := Length(MyString);
 while I > 0 do
 begin
   MyString[I] := AnsiUpperCase(MyString[I]);
   I := I - 1;
 end;
end;

Be careful indexing strings in this way, since overwriting the end of a string can cause access violations. Also, avoid passing long-string indexes as var parameters, because this results in inefficient code.

You can assign the value of a string constant--or any other expression that returns a string--to a variable. The length of the string changes dynamically when the assignment is made. Examples:

MyString := "Hello world!";
MyString := "Hello " + "world";
MyString := MyString + "!";
MyString := " ";               { space }
MyString := "";                { empty string }


 
begin...end ©   (2006-01-28 09:09) [2]

> ArtemESC ©   (28.01.06 08:59)

> В смысле
> как с ним работать на асме...

А что конкретно нужно?

> YurikGL ©   (28.01.06 09:02) [1]

> The following code uses the standard AnsiUpperCase function
> to convert MyString to uppercase.
>
> var I: Integer;
> begin
> I := Length(MyString);
> while I > 0 do
> begin
>   MyString[I] := AnsiUpperCase(MyString[I]);
>   I := I - 1;
> end;
> end;

Боже мой... Вот уж не ожидал такое увидеть в справке...


 
TUser ©   (2006-01-28 09:14) [3]

ansistring - указатель на область памяти, где дальше идет строка + 1 нулевой символ. А перед этой точкой - 4 байта на длину + еще дальше 4 байта на счетчик ссылок. Вроде так. При включенной опции $H (по умолчанию) - string == ansiastring.


 
ArtemESC ©   (2006-01-28 10:14) [4]

>>А что конкретно нужно?
Получить длину строки, n-символ...


 
begin...end ©   (2006-01-28 10:26) [5]

> ArtemESC ©   (28.01.06 10:14) [4]

> Получить длину строки

См. код System._LStrLen.

> n-символ

var
 S: string;
begin
 S := "abc";
 asm
   MOV   EAX, S
   MOV   AL, [EAX + ...]
   ...
 end
end


 
ArtemESC ©   (2006-01-28 10:37) [6]

>>begin...end
Спасибо!

MOV   AL, [EAX + ...]

... - это и есть смещение (от начала строки)?


 
begin...end ©   (2006-01-28 10:40) [7]

> ArtemESC ©   (28.01.06 10:37) [6]

> ... - это и есть смещение (от начала строки)?

Да. Оно на единицу меньше индекса символа.


 
ArtemESC ©   (2006-01-28 10:48) [8]

>>begin...end ©   (28.01.06 10:40) [7]
>>Оно на единицу меньше индекса символа.
Могу предположить что в первом "индексе" хранится длина?


 
begin...end ©   (2006-01-28 11:01) [9]

> ArtemESC ©   (28.01.06 10:48) [8]

См. [3]. Длина находится по смещению -4 от начала тела строки. Следовательно, получить её можно так:

var
 S: string;
begin
 S := "abc";
 asm
   MOV   EAX, S
   TEST  EAX, EAX
   JZ    @@null
   MOV   EAX, [EAX - 4] // Теперь в EAX загружена длина строки S
   ...
 @@null:
   // Строка S не инициализирована
   ...
 end
end

Говоря о том, что смещение от начала строки меньше индекса символа на единицу, я имел в виду, что символ S[1] находится по адресу Pointer(S) + 0, символ S[2] -- по адресу Pointer(S) + 1, и т.д.

P.S. Посты [5], [7] и этот относятся к случаю, когда string = AnsiString (когда {$H+}). Если же {$H-}, то string = ShortString, и получать символы и длину нужно совсем по-другому.



Страницы: 1 вся ветка

Текущий архив: 2006.02.19;
Скачать: CL | DM;

Наверх




Память: 0.49 MB
Время: 0.033 c
2-1138887535
Apl
2006-02-02 16:38
2006.02.19
TADOQuery


15-1138776748
Ega23
2006-02-01 09:52
2006.02.19
С Днём рождения! 1 февраля


1-1137595538
Maverick
2006-01-18 17:45
2006.02.19
MainMenu + Icon + MDI


15-1138686642
syte_ser78
2006-01-31 08:50
2006.02.19
защита картинки от распечатывания


15-1138446098
Хинт
2006-01-28 14:01
2006.02.19
Перевести с Visa илл Master card на WebMoney