Delphi. Pointers. PChar

Source: Internet
Author: User

This article is a Delphi. Pointer. The application of the sister article, want to refine the Pchar application, so with this article.

Attention:

1: This article is about Pchar and string-related operations, other methods are not much to say.

2: Because D separates ansi/unicode two kinds of completely different Yi, namely: Ansi.char=ansichar; Unicode.char=widechar

Therefore, in this article, Pchar for the Pansichar, for Pwidechar, need to do other processing, please note

Pchar is a pointer to a pointer to a string content that has a data type (CHAR) compared to pointer.

So, some people also like to take it as a memory block storage, to do a buffer package, because it compared with pointer, mobile, convenient conversion, home necessities AH (see usage II)

Usage One:

1 var 2   P:pchar; 3   string ; 4 begin 5   ' ABC ' ; 6   P: = PChar (s);   

The most commonly used code, string and Pchar data type conversion, in various types of APIs, often used.

Other related usage is: When assigning, move: +-

P: = PChar (s) + 1; P point to s[2]

P: = PChar (s)-sizeof (Integer); P Point to String.len

Attention:

The string must be char = #0 in its content. i.e. s: = ' abc '; In s application (3 + 1) * sizeof (char), of which 1 is prepared for #0.

ShowMessage (IntToStr (Ord (PChar (s) + Length (s))));

usage Two: cache processing with Pchar

1 type2Pmystring =^tmystring;3Tmystring =Record4 Buf:pansichar;5 Len:integer;6 Buf_len:integer;7   End;8 9Initialize +Allocate SpaceTen procedure string_init (vars:tmystring; buf_len:integer); One begin AFillchar (s), SizeOf (s),0); -S.buf: =AllocMem (Buf_len); -S.buf_len: =Buf_len; the End; -  -Anti-initialization +Free Space - procedure string_uninit (vars:tmystring); + begin -   ifS.buf_len >0  Then + Freemem (S.BUF); AFillchar (s), sizeof (s),0); at End; -  -//Write data (arbitrary data) - procedure string_Write(vars:tmystring; Buf:pointer; Len:integer);Overload; - begin -   ifLen + S.len > S.buf_len Then in   begin -INC (S.buf_len, Len *2); to Reallocmem (S.buf, S.buf_len); +   End; -   ifLen >0  Then the   begin *Move (buf^, (S.buf +s.len) ^, Len); $ Inc (S.len, Len);Panax Notoginseng   End; - End; the  +//Write data (string data) A procedure string_Write(vars:tmystring;ConstAData:string);Overload; the begin +   string_Write(S, Pansichar (AData), Length (AData) *sizeof (Char)); - End; $  $//read data (for strings only), erase data after reading - function string_Read(vars:tmystring):string; - begin the   ifS.len >0  Then -   beginWuyi SetString (Result, S.buf, S.len); theS.len: =0; -   End Else WuResult: ="'; - End;

The above is a simple function of buffer cache with Pchar, in the process of writing (String_write), is actually a simple pchar +-processing, just an extension method.

    Read operation, only write a string, other data, such as Integer, double, is actually a pointer conversion problem, such as:        

1 Result: = pbyte (s.buf) ^; 2 1 )^; 3 1 4 )^; 4 1 4 4 )^; 5 1 4 4 8) ^.wparam;

There is another operation is string_delete, with the interest of the person to handle themselves:)

 

Usage Three:

I don't know if I've read the code: Classes.pas::tparser.nexttoken, the code inside, parsing string writing is very interesting.

The general rule is: when encountering a desired character, then find the Terminator, then get a string, according to the rules, let the string into an integer, string ...

Then, I learned to use Pchar to parse all kinds of strings, I have to say that code to praise one, the idea is very interesting, suggest a look.

The following example, probably a simplified version of Nexttoken, writes some logic out, well, using splitter as the simplest example.

1 type2Tstr =Record3 Ptr:pchar;4 Len:integer;5   End;6 7//separating the SRC data, separating the data, and putting it into S8//successful, indicates the separation is successful, the failure means the end9 functionSplittervarsrc, s:tstr): Boolean;Ten var One Start:pchar; A begin -Result: =false; -   ifSrc.len <=0  Thenexit; the  -//1: Save original address -Start: =src.ptr; -//2: Move to the separator character prompt +    while(Src.len >0) and  not(src.ptr^inch[',',';']) Do -   begin + Inc (SRC.PTR); A Dec (src.len); at   End; -//3: Check whether the separation was successful -Result: = Src.ptr-start >0; -   ifResult Then -   begin -//4: Successful, S assignment inS.ptr: =start; -S.len: = src.ptr-start; to//5: Skips delimited characters, waits for next separation +      while(Src.len >0) and(src.ptr^inch[',',';']) Do -     begin the Inc (SRC.PTR); * Dec (src.len); $     End;Panax Notoginseng   End; - End; the  + functionSplitter_string(varSRC:TSTR;varSstring): Boolean; A var the Sub:tstr; + begin -Result: =Splitter (src, sub); $   ifResult Then $ SetString (S, sub.ptr, Sub.len); - End; -  the procedureTform1.button1click (sender:tobject); - varWuyi Src:tstr; theData, S:string; - begin WuData: ='A,b,c'; -Src.ptr: =PChar (data); AboutSrc.len: =Length (data); $  -    whileSplitter_string(SRC, s) Do -MEMO1.LINES.ADD ('Splitter:'+s); - End;

Forgive me, always do not know this method of parsing called What name (Do you know, please tell me), or just a character parsing logic? But it doesn't feel like.
Perhaps someone asked, why so toss, simple separation, with tstring and add a property set, you can get the results.

Above, just an example, just about logic. It is not intended to be delimited, and in some cases it is useful to use as few strings as possible.

This method of application, in string processing: syntax parsing, really drawbacks, such as: expression, xml,json,http ...

This processing method: Pchar scans from beginning to end, then ends, and the middle is interspersed with data processing. So the speed is not fast.

And in the middle, if needed can not produce any processing with memory allocation/deallocation (string operation requires Getmem+freemem), only the address + length is recorded.

The example above is just a simple process, and the case is used to match each character, such as the characters in the XML "<", "/", ">", and then data processing.

This part of the content, it is estimated that interested in the analysis will be looked at, so no longer fine writing. :D

Summarize:

Some things are very refined, always say pchar operations are various, because the pointer itself is a relatively high degree of freedom of things, coupled with some methods, not to mention the combination.

So, also can not say too clear, feel like the word is in circles, or say those things, so, first write here. :)

 

The level is limited, if has the similarity, is piracy!

2014.10.21 by QSL

Delphi. Pointers. PChar

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.