Download objects
Transcript
180 - 06: All About Strings MyStr1 [1] := Show ( 'Change Show ( 'MyStr1 Show ( 'MyStr2 'a'; 2nd char' ); - ' + StringStatus (MyStr1)); - ' + StringStatus (MyStr2)); Initially, you should get two strings with the same content, the same memory location, and a reference count of 2. MyStr1 - Addr: 51837036, Len: 5, Ref: 2, Val: Hello MyStr2 - Addr: 51837036, Len: 5, Ref: 2, Val: Hello As the application changes the value of one of the two strings (it doesn't matter which one), the memory location of the updated string will change. This is the effect of the copy-on-write technique. This is the second part of the output: Change 2nd char MyStr1 - Addr: 51848300, Len: 5, Ref: 1, Val: Hallo MyStr2 - Addr: 51837036, Len: 5, Ref: 1, Val: Hello You can freely extend this example and use the StringStatus function to explore the behavior of long strings in many other circumstances, with multiple reference, when they are passed as parameters, assigned to local variables, and more. Strings and Encodings As we have seen the string type in Object Pascal is mapped to the Unicode UTF16 format, with 2-bytes per element and management of surrogate pairs for code points outside of the BMP (Basic Multi-language Plane). There are many cases, though, in which you need to save to file, load from file, transmit over a socket connection, or receive form a connection textual data that uses a different representation, like ANSI or UTF8. To convert files and in memory data among different formats (or encodings), the Object Pascal RTL has a handy TEncoding class, defined in the System.SysUtils unit along with several inherited classes. note There are several other handy classes in the Object Pascal RTL that you can use for reading and writing data in text formats. For example, the TStreamReader and TStreamWriter classes offer support for text files with any encoding. These classes will be introduced in Chapter 18. Although I still haven't introduced classes and inheritance, this set of encoding classes is very easy to use, as there is already a global object for each encoding, automatically created for you. In other words, an object of each of these encoding classes is available within the TEncoding class, as a class property: Marco Cantù, Object Pascal Handbook