开发者

delphi declaring size of ansi string

Its easy to define a string at the size of 3 (in old delphi code)

st:string[3];

now, we wish to move the code to ansi

st:ansiString[3];

won't work!

and for adcanced oem type

st:oemString[3]; 

same problem, where

type
  OemString = Type AnsiString(CP_OEMCP);

how could be declared a fixed length ansi string and the new oem type?

update: i know it will 开发者_如何学Gocreate a fixed length string. it is part of the design of the software to protect against mistakes, and is essential for the program.


You don't need to define the size of an AnsiString.

The notation

string[3] 

is for short strings used by Pascal (and Delphi 1) and it is mostly kept for legacy purposes.

Short strings can be 1 to 255 bytes long. The first ("hidden") byte contains the length.

AnsiString is a pointer to a character buffer (0 terminated). It has some internal magic like reference counting. And you can safely add characters to an existing string because the compiler will handle all the nasty details for you.

UnicodeStrings are like AnsiStrings, but with unicode chars (2 bytes in this case). The default string now (Delphi 2009) maps to UnicodeString.

the type AnsiString has a construct to add a codepage (used to define the characters above 127) hence the CP_OEMCP:

OemString = Type AnsiString(CP_OEMCP);


"Short Strings" are "Ansi" String, because there are only available for backward compatibility of pre-Delphi code.

       st: string[3];

will always create a fixed-length "short string" with the current Ansi Code Page / Char Set, since Delphi 2009.

But such short strings are NOT the same than so called AnsiString. There is not code page for short strings. As there is no reference-count for short strings.

The code page exists only for AnsiString type, which are not fixed-length, but variable-length, and reference counted, so a completely diverse type than a short string defined by string[...].

You can't just mix Short String and AnsiString type declaration, by design. Both are called 'strings' but are diverse types.

Here is the mapping of a Short String

  st[0] = length(st)
  st[1] = 1st char (if any) in st
  st[2] = 2nd char (if any) in st
  st[3] = 3rd (if any) in st

Here is the memory mapping of an AnsiString or UnicodeString type:

  st = nil   if st=''
  st = PAnsiChar if st<>''

and here is the PSt: PAnsiChar layout:

  PWord(PSt-12)^ = code page
  PWord(PSt-10)^ = reference count
  PInteger(PSt-8)^  = reference count
  PInteger(PSt-4)^  = length(st) in AnsiChar or UnicodeChar count
  PAnsiChar(PSt) / PWideChar(PSt) = Ansi or Unicode text stored in st, finished by a #0 char (AnsiChar or UnicodeChar)

So if there is some similarities between AnsiString and UnicodeString type, the short string type is totally diverse, and can't be mixed as you wished.


That would only be usefull when String[3] in unicode versions of Delphi defaults to 3 WideChars. That would supprise me, but in case it is, use:

st: array[1..3] of AnsiChar;


The size of an ansistring and unicodestring will grow dynamically. The compiler and runtime code handle all this stuff for you.
See: http://delphi.about.com/od/beginners/l/aa071800a.htm

For a more in depth explanation see: http://www.codexterity.com/delphistrings.htm

The length can be anything from 1 char to 2GB.


But the old ShortString type, the newer string types in Delphi are dynamic. They grow and shrink as needed. You can preallocate a string to a given length calling SetLength(), useful to avoid re-allocating memory if you have to add data piece by piece to a string you know the final length anyway, but even after that the string can still grow and shrink when data are added or deleted. If you need static strings you can use array[0..n] of chars, whose size won't change dynamically.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜