Symbian3/SDK/Source/GUID-5B1A7CBF-40DE-56F9-B145-948C6F72B647.dita
author Dominic Pinkman <dominic.pinkman@nokia.com>
Fri, 13 Aug 2010 16:47:46 +0100
changeset 14 578be2adaf3e
parent 7 51a74ef9ed63
permissions -rw-r--r--
Week 32 contribution of PDK documentation content. See release notes for details. Fixes bug Bug 3582

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies) All rights reserved. -->
<!-- This component and the accompanying materials are made available under the terms of the License 
"Eclipse Public License v1.0" which accompanies this distribution, 
and is available at the URL "http://www.eclipse.org/legal/epl-v10.html". -->
<!-- Initial Contributors:
    Nokia Corporation - initial contribution.
Contributors: 
-->
<!DOCTYPE concept
  PUBLIC "-//OASIS//DTD DITA Concept//EN" "concept.dtd">
<concept id="GUID-5B1A7CBF-40DE-56F9-B145-948C6F72B647" xml:lang="en"><title>Using
TDesC16 Class</title><shortdesc>Use <codeph>TDesC16</codeph> for interfaces which access Unicode
text, regardless of the build variant, but which does not change the data.</shortdesc><prolog><metadata><keywords/></metadata></prolog><conbody>
<section id="GUID-E63C406D-E480-59B0-B829-3838DDE6B24F"><title>Using in a
function interface</title> <p>Interfaces which take wide (Unicode) text, use
descriptors in the specification of that interface. All 16 bit concrete descriptors
are derived from <codeph>TDesC16</codeph> which means that the interface can
accept any 16 bit descriptor.</p> <p>The following code fragment shows the
most common function prototype pattern.</p> <codeblock id="GUID-22E802FF-CFDD-52EB-87FD-90BE56022243" xml:space="preserve">void ClassX::foo(const TDesC16&amp; anArg);</codeblock> <p>The use of <xref href="GUID-440FF2B4-353B-3097-A2BA-5887D10B8B23.dita"><apiname>TDesC16</apiname></xref> ensures that the data cannot be
modified through the descriptor; <codeph>const</codeph> is an extra guarantee
that the data cannot be changed.</p> <p>In practice, nearly all code uses
the build independent form, <codeph>TDesC</codeph>, unless an explicit 8 bit
or 16 bit build variant is required.</p> </section>
<section id="GUID-149EA2C7-E0A6-5AF6-BF63-9F07AAE1F0C2"><title>Extract leftmost
part of data</title> <p>The code fragment shows how the leftmost part of data
in a descriptor can be accessed, using the <xref href="GUID-440FF2B4-353B-3097-A2BA-5887D10B8B23.dita#GUID-440FF2B4-353B-3097-A2BA-5887D10B8B23/GUID-FC14F917-66BE-352A-84ED-27478C3D3962"><apiname>TDesC16::Left()</apiname></xref> member
function.</p> <p>The behaviour is the same for the build independent variant, <xref href="GUID-52D07F46-2162-380C-A775-C3BB335C42F5.dita"><apiname>TDesC</apiname></xref>,
replacing <xref href="GUID-B7343D82-C251-3C27-A1E4-79B628F2D593.dita"><apiname>_LIT16</apiname></xref> with <xref href="GUID-B47B3E37-4FAC-327C-B626-49E09DB147FC.dita"><apiname>_LIT</apiname></xref>, <xref href="GUID-44622E86-313E-36B3-9989-3016F8E6E252.dita"><apiname>TBufC16</apiname></xref> with <xref href="GUID-4D64E4B7-6BEE-3900-A115-460FE5B2D79E.dita"><apiname>TBufC</apiname></xref> and <xref href="GUID-8FE95467-D48B-3E61-9028-29C0F15F567E.dita"><apiname>TPtrC16</apiname></xref> with <xref href="GUID-5CD07A27-E3ED-3273-A560-680501468C91.dita"><apiname>TPtrC</apiname></xref>.</p> <codeblock id="GUID-92E42C6C-47A6-5216-88BC-689869F6D5C9" xml:space="preserve">_LIT16(KData,"abcdefg");
TBufC16&lt;8&gt; str(KData);
...
str.Left(4);</codeblock> <p>The call to <codeph>Left()</codeph> returns a
non-modifiable pointer descriptor representing the data string "<codeph>abcd</codeph>";
this has length 4. The original data contained in, and represented by, the
non-modifiable buffer descriptor <codeph>str</codeph>, is not changed in any
way.</p> <codeblock id="GUID-9BF55CB5-45D1-5B90-8E3E-3BF704B0CB55" xml:space="preserve">_LIT16(KData,"abcdefg");
TBufC16&lt;8&gt; str(KData);
...
str.Left(256);</codeblock> <p>This call to <codeph>Left()</codeph> returns
a non-modifiable pointer descriptor representing the data string "<codeph>abcdefg</codeph>",
i.e. the whole content of the descriptor <codeph>str</codeph>; this has length
7.</p> <p>Note that the following call to <codeph>Left()</codeph> results
in a panic.</p> <codeblock id="GUID-E74C6BA7-5676-55DA-8EDF-21D0C3F44CED" xml:space="preserve">_LIT16(KData,"abcdefg");
TBufC16&lt;8&gt; str(KData);
...
str.Left(-1);           // Panic !</codeblock> </section>
<section id="GUID-EE79E69C-3F7A-5AF8-9781-434A496255CA"><title>Extract rightmost
part of data</title> <p>The code fragment shows how the right most part of
data in a descriptor can be accessed, using the <xref href="GUID-440FF2B4-353B-3097-A2BA-5887D10B8B23.dita#GUID-440FF2B4-353B-3097-A2BA-5887D10B8B23/GUID-97097CD2-B179-3D51-874F-3CE7A9D761F0"><apiname>TDesC16::Right()</apiname></xref> member
function.</p> <p>The behaviour is the same for the build independent variant, <xref href="GUID-52D07F46-2162-380C-A775-C3BB335C42F5.dita"><apiname>TDesC</apiname></xref>,
replacing <xref href="GUID-B7343D82-C251-3C27-A1E4-79B628F2D593.dita"><apiname>_LIT16</apiname></xref> with <xref href="GUID-B47B3E37-4FAC-327C-B626-49E09DB147FC.dita"><apiname>_LIT</apiname></xref>, <xref href="GUID-44622E86-313E-36B3-9989-3016F8E6E252.dita"><apiname>TBufC16</apiname></xref> with <xref href="GUID-4D64E4B7-6BEE-3900-A115-460FE5B2D79E.dita"><apiname>TBufC</apiname></xref> and <xref href="GUID-8FE95467-D48B-3E61-9028-29C0F15F567E.dita"><apiname>TPtrC16</apiname></xref> with <xref href="GUID-5CD07A27-E3ED-3273-A560-680501468C91.dita"><apiname>TPtrC</apiname></xref>.</p> <codeblock id="GUID-41C7F212-E677-585E-80B1-3DED21E48537" xml:space="preserve">_LIT16(KData,"abcdefg");
TBufC16&lt;8&gt; str(KData);
...
str.Right(4);</codeblock> <p>The call to <codeph>Right()</codeph> returns
a non-modifiable pointer descriptor representing the data string "<codeph>defg</codeph>";
this has length 4. The original data contained in, and represented by, the
non-modifiable buffer descriptor <codeph>str</codeph>, is not changed in any
way.</p> <codeblock id="GUID-6663CDF1-93F6-50DE-8A1A-E1F30F4528F2" xml:space="preserve">_LIT16(KData,"abcdefg");
TBufC16&lt;8&gt; str(KData);
...
str.Right(256);</codeblock> <p>This call to <codeph>Right()</codeph> returns
a non-modifiable pointer descriptor representing the data string "<codeph>abcdefg</codeph>".
The whole content of the descriptor <codeph>str</codeph> has the length 7.</p> <p>Note
that the following call to <codeph>Right()</codeph> results in a panic.</p> <codeblock id="GUID-2F9ADE78-9CA5-5032-95E0-5497687FC8FE" xml:space="preserve">_LIT16(KData,"abcdefg");
TBufC16&lt;8&gt; str(KData);
...
str.Right(-1);          // Panic !</codeblock> </section>
<section id="GUID-A7393084-990B-5C51-A5FF-B9C4182ECAE0"><title>Extract middle
portion of the data</title> <p>The code fragment shows how a portion of data
within a descriptor can be accessed, using the <xref href="GUID-440FF2B4-353B-3097-A2BA-5887D10B8B23.dita#GUID-440FF2B4-353B-3097-A2BA-5887D10B8B23/GUID-76B4CCFB-F592-3A80-87FE-ACD868172F3C"><apiname>TDesC16::Mid()</apiname></xref> member
function. Each call to<codeph>Mid()</codeph> returns a non-modifiable pointer
descriptor representing the selected portions of data.</p> <p>The behaviour
is the same for the build independent variant,<xref href="GUID-52D07F46-2162-380C-A775-C3BB335C42F5.dita"><apiname>TDesC</apiname></xref>, replacing <xref href="GUID-B7343D82-C251-3C27-A1E4-79B628F2D593.dita"><apiname>_LIT16</apiname></xref> with <xref href="GUID-B47B3E37-4FAC-327C-B626-49E09DB147FC.dita"><apiname>_LIT</apiname></xref>,<xref href="GUID-44622E86-313E-36B3-9989-3016F8E6E252.dita"><apiname>TBufC16</apiname></xref> with<xref href="GUID-4D64E4B7-6BEE-3900-A115-460FE5B2D79E.dita"><apiname>TBufC</apiname></xref> and<xref href="GUID-8FE95467-D48B-3E61-9028-29C0F15F567E.dita"><apiname>TPtrC16</apiname></xref> with <xref href="GUID-5CD07A27-E3ED-3273-A560-680501468C91.dita"><apiname>TPtrC</apiname></xref>.</p> <codeblock id="GUID-7A669FDC-DE36-54C0-878F-0E8C4D9A3246" xml:space="preserve">_LIT16(KData,"abcdefg");
TBufC16 str(KData);
...
str.Mid(0);   //returns TPtrC16 representing "abcdefg"; length is 7
str.Mid(1);   //returns TPtrC16 representing "bcdefg";  length is 6
str.Mid(6);   //returns TPtrC16 representing "g";       length is 1
str.Mid(3,3); //returns TPtrC16 representing "def";     length is 3
str.Mid(0,7); //returns TPtrC16 representing "abcdefg"; length is 7
...
str.Mid(8); // Panics !
str.Mid(3,5); // Panics !</codeblock> </section>
<section id="GUID-5506FF14-B12F-5CB8-9613-DFCF678BDB57"><title>Comparing data</title> <p>This
code fragment shows the<xref href="GUID-440FF2B4-353B-3097-A2BA-5887D10B8B23.dita#GUID-440FF2B4-353B-3097-A2BA-5887D10B8B23/GUID-AD5D7B9B-527C-324F-B399-4AEDA0432D4B"><apiname>TDesC16::Compare()</apiname></xref> function.</p> <p>The
behaviour is the same for the build independent variant,<xref href="GUID-52D07F46-2162-380C-A775-C3BB335C42F5.dita"><apiname>TDesC</apiname></xref>,
replacing <xref href="GUID-B7343D82-C251-3C27-A1E4-79B628F2D593.dita"><apiname>_LIT16</apiname></xref> with <xref href="GUID-B47B3E37-4FAC-327C-B626-49E09DB147FC.dita"><apiname>_LIT</apiname></xref>, <xref href="GUID-44622E86-313E-36B3-9989-3016F8E6E252.dita"><apiname>TBufC16</apiname></xref> with<xref href="GUID-4D64E4B7-6BEE-3900-A115-460FE5B2D79E.dita"><apiname>TBufC</apiname></xref>.</p> <codeblock id="GUID-D3F03C22-0D4C-5C76-A10E-90B4B330F04B" xml:space="preserve">_LIT16(Kabcd,  "abcd");
_LIT16(Kabcde, "abcde");
_LIT16(Kabc,   "abc");
_LIT16(Kabcx,  "abcx");
...
TBufC16&lt;8&gt; str(Kabcd);
...
str.Compare(Kabcde);    // returns -ve
str.Compare(Kabc);      // returns +ve
str.Compare(Kabcd);     // returns zero
str.Compare(Kabcx);     // returns -ve</codeblock> <p>This result of the comparison
means that:</p> <ul>
<li id="GUID-902AA635-E5DD-5806-B1C3-E6D4CD6531C8"><p>"abcd" is less than
"abcde".</p> </li>
<li id="GUID-BD9A0EE3-F924-5C1F-88AF-A5A56B22B0B5"><p>"abcd" is greater than
"abc".</p> </li>
<li id="GUID-140D551D-F465-53AD-9FB8-0DBA1CB7FB29"><p>"abcd" is equal to "abcd".</p> </li>
<li id="GUID-48853EF5-D2A1-55C6-92A3-1308BEE49FD0"><p>"abcd" is less than
"abcx".</p> </li>
</ul> </section>
<section id="GUID-8E3F058A-4E53-5608-BEE0-69531AB28A5E"><title>Locating a
character</title> <p>This code fragment shows the<xref href="GUID-440FF2B4-353B-3097-A2BA-5887D10B8B23.dita#GUID-440FF2B4-353B-3097-A2BA-5887D10B8B23/GUID-76F6497A-6353-3CF6-AB30-41E02EDC916A"><apiname>TDesC16::Locate()</apiname></xref> function.</p> <p>The
behaviour is the same for the build independent variant,<xref href="GUID-52D07F46-2162-380C-A775-C3BB335C42F5.dita"><apiname>TDesC</apiname></xref>,
replacing <xref href="GUID-B7343D82-C251-3C27-A1E4-79B628F2D593.dita"><apiname>_LIT16</apiname></xref> with <xref href="GUID-B47B3E37-4FAC-327C-B626-49E09DB147FC.dita"><apiname>_LIT</apiname></xref>, <xref href="GUID-44622E86-313E-36B3-9989-3016F8E6E252.dita"><apiname>TBufC16</apiname></xref> with<xref href="GUID-4D64E4B7-6BEE-3900-A115-460FE5B2D79E.dita"><apiname>TBufC</apiname></xref>.</p> <codeblock id="GUID-212A32BC-BBA0-5A1D-92C2-D9CF9609379F" xml:space="preserve">_LIT16(Kabcd,"abcd");
TBufC16&lt;8&gt; str(Kabcd);
...
str.Locate('d');   // returns 3
str.Locate('a');   // returns 0
str.Locate('b');   // returns 1
str.Locate('x');   // returns KErrNotFound</codeblock> </section>
<section id="GUID-942CD420-E5A1-5A11-BC21-E895294CDDEB"><title>Finding data</title> <p>This
code fragment shows the <xref href="GUID-440FF2B4-353B-3097-A2BA-5887D10B8B23.dita#GUID-440FF2B4-353B-3097-A2BA-5887D10B8B23/GUID-3C5D15A1-E09F-34A5-A4E9-FEE36CED32B5"><apiname>TDesC16::Find()</apiname></xref> function.</p> <p>The
behaviour is the same for the build independent variant,<xref href="GUID-52D07F46-2162-380C-A775-C3BB335C42F5.dita"><apiname>TDesC</apiname></xref>,
replacing <xref href="GUID-B7343D82-C251-3C27-A1E4-79B628F2D593.dita"><apiname>_LIT16</apiname></xref> with <xref href="GUID-B47B3E37-4FAC-327C-B626-49E09DB147FC.dita"><apiname>_LIT</apiname></xref>, <xref href="GUID-44622E86-313E-36B3-9989-3016F8E6E252.dita"><apiname>TBufC16</apiname></xref> with<xref href="GUID-4D64E4B7-6BEE-3900-A115-460FE5B2D79E.dita"><apiname>TBufC</apiname></xref>.</p> <codeblock id="GUID-764360D8-EA0F-5B7D-939B-235F249C169E" xml:space="preserve">_LIT16(KAtoZ,"abcdefghijklmnopqrstuvwxyz");
TBufC16&lt;32&gt; str(KAtoZ);
...
_LIT16(KFind1,"abc");
str.Find(KFind1);            // returns 0

_LIT16(KFInd2,"bcde");
str.Find(KFInd2);            // returns 1

_LIT16(KFind3,"uvwxyz");
str.Find(KFind3);            // returns 20

_LIT16(KFind4,"0123");
str.Find(KFind4);            // returns KErrNotFound

_LIT16(KFind5,"abcdefghijklmnopqrstuvwxyz01");
str.Find(KFind5);            // returns KErrNotFound

str.Find(KNullDesC16);       // returns 0</codeblock> </section>
<section id="GUID-35A35FA8-F8CF-59F7-AEF6-23F340E5E9EE"><title>Pattern matching</title> <p>This
code fragment shows the <xref href="GUID-440FF2B4-353B-3097-A2BA-5887D10B8B23.dita#GUID-440FF2B4-353B-3097-A2BA-5887D10B8B23/GUID-D104D885-DB49-30CD-A78D-2E10A783CA93"><apiname>TDesC16::Match()</apiname></xref> function.</p> <p>The
behaviour is the same for the build independent variant,<xref href="GUID-52D07F46-2162-380C-A775-C3BB335C42F5.dita"><apiname>TDesC</apiname></xref>,
replacing <xref href="GUID-B7343D82-C251-3C27-A1E4-79B628F2D593.dita"><apiname>_LIT16</apiname></xref> with <xref href="GUID-B47B3E37-4FAC-327C-B626-49E09DB147FC.dita"><apiname>_LIT</apiname></xref>, <xref href="GUID-44622E86-313E-36B3-9989-3016F8E6E252.dita"><apiname>TBufC16</apiname></xref> with<xref href="GUID-4D64E4B7-6BEE-3900-A115-460FE5B2D79E.dita"><apiname>TBufC</apiname></xref>.</p> <codeblock id="GUID-465DAB75-DFA3-5BE9-AC38-4548C9395334" xml:space="preserve">_LIT16(KAtoZ,"abcdefghijklmnopqrstuvwxyz");
TBufC16&lt;32&gt; str(KAtoZ);
    ...
_LIT16(KMatch1,"*ijk*");
str.Match(KMatch1);          //returns -&gt; 8

_LIT16(KMatch2,"*i?k*");
str.Match(KMatch2);          //        -&gt; 8

_LIT16(KMatch3,"ijk*");
str.Match(KMatch3);          //        -&gt; KErrNotFound

_LIT16(KMatch4,"abcd");
str.Match(KMatch4);          //        -&gt; KErrNotFound

_LIT16(KMatch5,"*i*mn*");
str.Match(KMatch5);          //        -&gt; 8

_LIT16(KMatch6,"abcdef*");
str.Match(KMatch6);          //        -&gt; 0

_LIT16(KMatch7,"*");
str.Match(KMatch7);          //        -&gt; 0

_LIT16(KMatch8,"*y*");
str.Match(KMatch8);          //        -&gt; 24

_LIT16(KMatch9,"*i??k*");
str.Match(KMatch9);          //        -&gt; KErrNotFound</codeblock> <p>To test
for the existence of a pattern within a text string, the pattern must start
and end with an ‘*’.</p> </section>
<section id="GUID-0C41371C-F933-55A5-A56A-46C14D8E0072"><title>Referencing
a data item</title> <p>The code fragment shows how a data item can be referenced
using<xref href="GUID-440FF2B4-353B-3097-A2BA-5887D10B8B23.dita#GUID-440FF2B4-353B-3097-A2BA-5887D10B8B23/GUID-1AA9098F-02ED-3305-B7B3-02A495A245A2"><apiname>TDesC16::operator[]()</apiname></xref>.</p> <p>The behaviour is the
same for the build independent variant,<xref href="GUID-52D07F46-2162-380C-A775-C3BB335C42F5.dita"><apiname>TDesC</apiname></xref>, replacing <xref href="GUID-B7343D82-C251-3C27-A1E4-79B628F2D593.dita"><apiname>_LIT16</apiname></xref> with <xref href="GUID-B47B3E37-4FAC-327C-B626-49E09DB147FC.dita"><apiname>_LIT</apiname></xref>, <xref href="GUID-44622E86-313E-36B3-9989-3016F8E6E252.dita"><apiname>TBufC16</apiname></xref> with<xref href="GUID-4D64E4B7-6BEE-3900-A115-460FE5B2D79E.dita"><apiname>TBufC</apiname></xref>.</p> <codeblock id="GUID-36DC1901-DE68-539C-8B9C-018D8C4DDBA5" xml:space="preserve">_LIT16(KData,"abcdefg");
TBufC16&lt;8&gt; str(KData);
...
str[0];                 // returns reference to 'a'
str[3];                 // returns reference to 'd'
str[7];                 // Panics !!</codeblock> </section>
<section id="GUID-1885CDFC-D55F-596E-9BB4-D4BCB509FEB6"><title>Creating a
heap descriptor</title> <p>The code fragments show how a heap descriptor is
created from an existing descriptor using the <xref href="GUID-440FF2B4-353B-3097-A2BA-5887D10B8B23.dita#GUID-440FF2B4-353B-3097-A2BA-5887D10B8B23/GUID-9F44916A-1E89-350C-B831-79AF4D07CB15"><apiname>TDesC16::AllocL()</apiname></xref> member
function.</p> <p>The behaviour is the same for the build independent variant,<xref href="GUID-52D07F46-2162-380C-A775-C3BB335C42F5.dita"><apiname>TDesC</apiname></xref>,
replacing <xref href="GUID-B7343D82-C251-3C27-A1E4-79B628F2D593.dita"><apiname>_LIT16</apiname></xref> with <xref href="GUID-B47B3E37-4FAC-327C-B626-49E09DB147FC.dita"><apiname>_LIT</apiname></xref>,<xref href="GUID-44622E86-313E-36B3-9989-3016F8E6E252.dita"><apiname>TBufC16</apiname></xref> with<xref href="GUID-4D64E4B7-6BEE-3900-A115-460FE5B2D79E.dita"><apiname>TBufC</apiname></xref> and<xref href="GUID-3D3D9CD7-C8FD-3F81-9CC5-1A71D4F9751E.dita"><apiname>HBufC16</apiname></xref> with <xref href="GUID-A103FB19-60B3-3E45-97A5-1F295934ACA1.dita"><apiname>HBufC</apiname></xref>.</p> <codeblock id="GUID-DDC25AE2-0F9C-54E7-BDF5-0BCD0AA0BE41" xml:space="preserve">_LIT16(KData,"abcdefg");
TBufC16&lt;16&gt; str(KData);
...
HBufC16* ptr;
...
ptr = str.AllocL();     //Creates and returns address of
...                     //heap descriptor. The new heap descriptor
...                     //contains a copy of the original data.
ptr-&gt;Length();          //Returns 7; the length of "abcdfeg"</codeblock> </section>
</conbody><related-links>
<link href="GUID-7830BAAB-40DD-5E55-84B5-8DCA888E68E7.dita#GUID-7830BAAB-40DD-5E55-84B5-8DCA888E68E7/GUID-235E272D-6F5A-5740-8150-9698C8C8D55D">
<linktext>Using TDesC in a function interface</linktext></link>
</related-links></concept>