summaryrefslogtreecommitdiff
path: root/devdocs/c/string%2Fbyte%2Fstrxfrm.html
blob: 15aa44a8f0615f6801f2e0f50607fca9ca15f311 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
    <h1 id="firstHeading" class="firstHeading">strxfrm</h1>            <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code>&lt;string.h&gt;</code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl t-until-c99"> <td> <pre data-language="c">size_t strxfrm( char          *dest, const char          *src, size_t count );</pre>
</td> <td class="t-dcl-nopad"> </td> <td> <span class="t-mark-rev t-until-c99">(until C99)</span> </td> </tr> <tr class="t-dcl t-since-c99"> <td> <pre data-language="c">size_t strxfrm( char *restrict dest, const char *restrict src, size_t count );</pre>
</td> <td class="t-dcl-nopad"> </td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td> </tr>  </table> <p>Transforms the null-terminated byte string pointed to by <code>src</code> into the implementation-defined form such that comparing two transformed strings with <code><a href="strcmp" title="c/string/byte/strcmp">strcmp</a></code> gives the same result as comparing the original strings with <code><a href="strcoll" title="c/string/byte/strcoll">strcoll</a></code>, in the current C locale.</p>
<p>The first <code>count</code> characters of the transformed string are written to destination, including the terminating null character, and the length of the full transformed string is returned, excluding the terminating null character.</p>
<p>The behavior is undefined if the <code>dest</code> array is not large enough. The behavior is undefined if <code>dest</code> and <code>src</code> overlap.</p>
<p>If <code>count</code> is <code>​0​</code>, then <code>dest</code> is allowed to be a null pointer.</p>
<h3 id="Notes"> Notes</h3> <p>The correct length of the buffer that can receive the entire transformed string is <code><span class="nu0">1</span><span class="sy2">+</span>strxfrm<span class="br0">(</span><a href="http://en.cppreference.com/w/c/types/NULL"><span class="kw103">NULL</span></a>, src, <span class="nu0">0</span><span class="br0">)</span></code></p>
<p>This function is used when making multiple locale-dependent comparisons using the same string or set of strings, because it is more efficient to use <code>strxfrm</code> to transform all the strings just once, and subsequently compare the transformed strings with <code><a href="strcmp" title="c/string/byte/strcmp">strcmp</a></code>.</p>
<h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> dest </td> <td> - </td> <td> pointer to the first element of the array where the transformed string will be written </td>
</tr> <tr class="t-par"> <td> src </td> <td> - </td> <td> pointer to the first character of a null-terminated byte string to transform </td>
</tr> <tr class="t-par"> <td> count </td> <td> - </td> <td> maximum number of characters to be written </td>
</tr>
</table> <h3 id="Return_value"> Return value</h3> <p>The length of the transformed string, not including the terminating null-character.</p>
<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;locale.h&gt;
 
int main(void)
{
    setlocale(LC_COLLATE, "cs_CZ.iso88592");
 
    const char *in1 = "hrnec";
    char out1[1+strxfrm(NULL, in1, 0)];
    strxfrm(out1, in1, sizeof out1);
 
    const char *in2 = "chrt";
    char out2[1+strxfrm(NULL, in2, 0)];
    strxfrm(out2, in2, sizeof out2);
 
    printf("In the Czech locale: ");
    if(strcmp(out1, out2) &lt; 0)
         printf("%s before %s\n",in1, in2);
    else
         printf("%s before %s\n",in2, in1);
 
    printf("In lexicographical comparison: ");
    if(strcmp(in1, in2)&lt;0)
         printf("%s before %s\n",in1, in2);
    else
         printf("%s before %s\n",in2, in1);
 
}</pre></div> <p>Possible output:</p>
<div class="text source-text"><pre data-language="c">In the Czech locale: hrnec before chrt
In lexicographical comparison: chrt before hrnec</pre></div> </div> <h3 id="References"> References</h3>  <ul>
<li> C17 standard (ISO/IEC 9899:2018): </li>
<ul><li> 7.24.4.5 The strxfrm function (p: 267) </li></ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul><li> 7.24.4.5 The strxfrm function (p: 366-367) </li></ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul><li> 7.21.4.5 The strxfrm function (p: 329-330) </li></ul>
<li> C89/C90 standard (ISO/IEC 9899:1990): </li>
<ul><li> 4.11.4.5 The strxfrm function </li></ul>
</ul>               <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="strcoll" title="c/string/byte/strcoll"> <span class="t-lines"><span>strcoll</span></span></a></div> </td> <td> compares two strings in accordance to the current locale <br> <span class="t-mark">(function)</span>  </td>
</tr> <tr class="t-dsc"> <td> <div><a href="../wide/wcscoll" title="c/string/wide/wcscoll"> <span class="t-lines"><span>wcscoll</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-c95">(C95)</span></span></span></div> </td> <td> compares two wide strings in accordance to the current locale <br> <span class="t-mark">(function)</span>  </td>
</tr> <tr class="t-dsc"> <td> <div><a href="strcmp" title="c/string/byte/strcmp"> <span class="t-lines"><span>strcmp</span></span></a></div> </td> <td> compares two strings <br> <span class="t-mark">(function)</span>  </td>
</tr> <tr class="t-dsc"> <td> <div><a href="../wide/wcsxfrm" title="c/string/wide/wcsxfrm"> <span class="t-lines"><span>wcsxfrm</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-c95">(C95)</span></span></span></div> </td> <td> transform a wide string so that wcscmp would produce the same result as wcscoll <br> <span class="t-mark">(function)</span>  </td>
</tr> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/string/byte/strxfrm" title="cpp/string/byte/strxfrm">C++ documentation</a></span> for <code>strxfrm</code> </td>
</tr> </table>           <div class="_attribution">
  <p class="_attribution-p">
    &copy; cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br>
    <a href="https://en.cppreference.com/w/c/string/byte/strxfrm" class="_attribution-link">https://en.cppreference.com/w/c/string/byte/strxfrm</a>
  </p>
</div>