Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/string/byte/strxfrm"

From cppreference.com
< cpp‎ | string‎ | byte
m (langlinks; p=true.)
m ({{c}}, ., headers sorted, fmt)
 
Line 2: Line 2:
 
{{cpp/string/byte/navbar}}
 
{{cpp/string/byte/navbar}}
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl header | cstring}}
+
{{dcl header|cstring}}
{{dcl |
+
{{dcl|
 
std::size_t strxfrm( char* dest, const char* src, std::size_t count );
 
std::size_t strxfrm( char* dest, const char* src, std::size_t count );
 
}}
 
}}
 
{{dcl end}}
 
{{dcl end}}
  
Transforms the null-terminated byte string pointed to by {{tt|src}} into the implementation-defined form such that comparing two transformed strings with {{lc|std::strcmp}} gives the same result as comparing the original strings with {{lc|std::strcoll}}, in the current C locale.
+
Transforms the null-terminated byte string pointed to by {{c|src}} into the implementation-defined form such that comparing two transformed strings with {{lc|std::strcmp}} gives the same result as comparing the original strings with {{lc|std::strcoll}}, in the current C locale.
  
The first {{tt|count}} 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.
+
The first {{c|count}} 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.
  
The behavior is undefined if the {{tt|dest}} array is not large enough. The behavior is undefined if {{tt|dest}} and {{tt|src}} overlap.
+
The behavior is undefined if the {{c|dest}} array is not large enough. The behavior is undefined if {{c|dest}} and {{c|src}} overlap.
  
If {{tt|count}} is {{c|0}}, then {{tt|dest}} is allowed to be a null pointer.
+
If {{c|count}} is {{c|0}}, then {{c|dest}} is allowed to be a null pointer.
  
 
===Notes===
 
===Notes===
The correct length of the buffer that can receive the entire transformed string is {{c|1+std::strxfrm(nullptr, src, 0)}}
+
The correct length of the buffer that can receive the entire transformed string is {{c|1 + std::strxfrm(nullptr, src, 0)}}.
  
 
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 {{lc|std::strxfrm}} to transform all the strings just once, and subsequently compare the transformed strings with {{lc|std::strcmp}}.
 
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 {{lc|std::strxfrm}} to transform all the strings just once, and subsequently compare the transformed strings with {{lc|std::strcmp}}.
Line 23: Line 23:
 
===Parameters===
 
===Parameters===
 
{{par begin}}
 
{{par begin}}
{{par| dest | pointer to the first element of the array where the transformed string will be written }}
+
{{par|dest|pointer to the first element of the array where the transformed string will be written}}
{{par| src | pointer to the first character of a null-terminated byte string to transform}}
+
{{par|src|pointer to the first character of a null-terminated byte string to transform}}
{{par| count | maximum number of characters to be written }}
+
{{par|count|maximum number of characters to be written}}
 
{{par end}}
 
{{par end}}
  
Line 33: Line 33:
 
===Example===
 
===Example===
 
{{example
 
{{example
| code=
+
|code=
 +
#include <cassert>
 +
#include <cstring>
 +
#include <iomanip>
 
#include <iostream>
 
#include <iostream>
#include <iomanip>
 
#include <cstring>
 
 
#include <string>
 
#include <string>
#include <cassert>
 
  
 
int main()
 
int main()
Line 46: Line 46:
  
 
     std::string in1 = "hrnec";
 
     std::string in1 = "hrnec";
     std::string out1(1+std::strxfrm(nullptr, in1.c_str(), 0), ' ');
+
     std::string out1(1 + std::strxfrm(nullptr, in1.c_str(), 0), ' ');
 
     std::string in2 = "chrt";
 
     std::string in2 = "chrt";
     std::string out2(1+std::strxfrm(nullptr, in2.c_str(), 0), ' ');
+
     std::string out2(1 + std::strxfrm(nullptr, in2.c_str(), 0), ' ');
  
 
     std::strxfrm(&out1[0], in1.c_str(), out1.size());
 
     std::strxfrm(&out1[0], in1.c_str(), out1.size());
Line 54: Line 54:
  
 
     std::cout << "In the Czech locale: ";
 
     std::cout << "In the Czech locale: ";
     if(out1 < out2)
+
     if (out1 < out2)
        std::cout << in1 << " before " << in2 << '\n';
+
        std::cout << in1 << " before " << in2 << '\n';
 
     else
 
     else
        std::cout << in2 << " before " << in1 << '\n';
+
        std::cout << in2 << " before " << in1 << '\n';
  
 
     std::cout << "In lexicographical comparison: ";
 
     std::cout << "In lexicographical comparison: ";
     if(in1 < in2)
+
     if (in1 < in2)
        std::cout << in1 << " before " << in2 << '\n';
+
        std::cout << in1 << " before " << in2 << '\n';
 
     else
 
     else
        std::cout << in2 << " before " << in1 << '\n';
+
        std::cout << in2 << " before " << in1 << '\n';
 
+
 
}
 
}
| p=true
+
|p=true
| output=
+
|output=
 
In the Czech locale: hrnec before chrt
 
In the Czech locale: hrnec before chrt
 
In lexicographical comparison: chrt before hrnec
 
In lexicographical comparison: chrt before hrnec
Line 74: Line 73:
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/string/wide/dsc wcsxfrm}}
+
{{dsc inc|cpp/string/wide/dsc wcsxfrm}}
{{dsc inc | cpp/locale/collate/dsc do_transform}}
+
{{dsc inc|cpp/locale/collate/dsc do_transform}}
{{dsc inc | cpp/string/byte/dsc strcoll}}
+
{{dsc inc|cpp/string/byte/dsc strcoll}}
{{dsc see c | c/string/byte/strxfrm}}
+
{{dsc see c|c/string/byte/strxfrm}}
 
{{dsc end}}
 
{{dsc end}}
  
 
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
 
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}

Latest revision as of 09:06, 6 June 2023

Defined in header <cstring>
std::size_t strxfrm( char* dest, const char* src, std::size_t count );

Transforms the null-terminated byte string pointed to by src into the implementation-defined form such that comparing two transformed strings with std::strcmp gives the same result as comparing the original strings with std::strcoll, in the current C locale.

The first count 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.

The behavior is undefined if the dest array is not large enough. The behavior is undefined if dest and src overlap.

If count is 0, then dest is allowed to be a null pointer.

Contents

[edit] Notes

The correct length of the buffer that can receive the entire transformed string is 1 + std::strxfrm(nullptr, src, 0).

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 std::strxfrm to transform all the strings just once, and subsequently compare the transformed strings with std::strcmp.

[edit] Parameters

dest - pointer to the first element of the array where the transformed string will be written
src - pointer to the first character of a null-terminated byte string to transform
count - maximum number of characters to be written

[edit] Return value

The length of the transformed string, not including the terminating null-character.

[edit] Example

#include <cassert>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <string>
 
int main()
{
    char* loc = std::setlocale(LC_COLLATE, "cs_CZ.iso88592");
    assert(loc);
 
    std::string in1 = "hrnec";
    std::string out1(1 + std::strxfrm(nullptr, in1.c_str(), 0), ' ');
    std::string in2 = "chrt";
    std::string out2(1 + std::strxfrm(nullptr, in2.c_str(), 0), ' ');
 
    std::strxfrm(&out1[0], in1.c_str(), out1.size());
    std::strxfrm(&out2[0], in2.c_str(), out2.size());
 
    std::cout << "In the Czech locale: ";
    if (out1 < out2)
        std::cout << in1 << " before " << in2 << '\n';
    else
        std::cout << in2 << " before " << in1 << '\n';
 
    std::cout << "In lexicographical comparison: ";
    if (in1 < in2)
        std::cout << in1 << " before " << in2 << '\n';
    else
        std::cout << in2 << " before " << in1 << '\n';
}

Possible output:

In the Czech locale: hrnec before chrt
In lexicographical comparison: chrt before hrnec

[edit] See also

transform a wide string so that wcscmp would produce the same result as wcscoll
(function) [edit]
[virtual]
transforms a string so that collation can be replaced by comparison
(virtual protected member function of std::collate<CharT>) [edit]
compares two strings in accordance to the current locale
(function) [edit]
C documentation for strxfrm