The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
unicode.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 by David White <[email protected]>
3  Copyright (C) 2005 - 2016 by Guillaume Melquiond <[email protected]>
4  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #ifndef SERIALIZATION_UNICODE_HPP_INCLUDED
17 #define SERIALIZATION_UNICODE_HPP_INCLUDED
18 
19 #include "ucs4_iterator_base.hpp"
20 #include "unicode_types.hpp"
21 #include "ucs4_convert_impl.hpp"
22 #include "unicode_cast.hpp"
23 
24 #include <string>
25 #include <vector>
26 
27 /**
28  * For Win32 API.
29  *
30  * On Windows, wchar_t is defined as Uint16.
31  * Wide strings are expected to be UTF-16.
32  */
33 namespace utf16 {
35 }
36 
37 /**
38  * Functions for converting Unicode wide-char strings to UTF-8 encoded strings,
39  * back and forth.
40  */
41 namespace utf8 {
43 
44  /** Returns a lowercased version of the string. */
46 
47  /**
48  * Codepoint index corresponding to the nth character in a UTF-8 string.
49  *
50  * @return str.length() if there are less than @p index characters.
51  */
52  size_t index(const utf8::string& str, const size_t index);
53 
54  /** Length in characters of a UTF-8 string. */
55  size_t size(const utf8::string& str);
56 
57  /** Insert a UTF-8 string at the specified position. */
58  utf8::string& insert(utf8::string& str, const size_t pos, const utf8::string& insert);
59 
60  /**
61  * Erases a portion of a UTF-8 string.
62  *
63  * @param str UTF-8 encoded string.
64  * @param start Start position.
65  * @param len Number of characters to erase.
66  *
67  * @note This implementation does not check for valid UTF-8. Don't use it
68  * for user input.
69  */
70  utf8::string& erase(utf8::string& str, const size_t start, const size_t len = std::string::npos);
71 
72  /**
73  * Truncates a UTF-8 string to the specified number of characters.
74  *
75  * @param str UTF-8 encoded string.
76  * @param size Size to truncate to.
77  *
78  * @note This implementation does not check for valid UTF-8. Don't use it
79  * for user input.
80  */
81  utf8::string& truncate(utf8::string& str, const size_t size);
82 
83  /**
84  * Truncates a UTF-8 string to the specified number of characters.
85  *
86  * If the string has more than @p size UTF-8 characters it will be
87  * truncated to this size.
88  *
89  * The output is guaranteed to be valid UTF-8.
90  *
91  * @param[in] str String encoded in UTF-8.
92  * @param[out] str String encoded UTF-8 that contains at most @p size
93  * codepoints.
94  * @param size The size to truncate to.
95  */
96  void truncate_as_ucs4(utf8::string& str, const size_t size);
97 } // end namespace utf8
98 
99 #endif
size_t index(const utf8::string &str, const size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
Definition: unicode.cpp:73
int pos
Definition: formula.cpp:800
void truncate_as_ucs4(utf8::string &str, const size_t size)
Truncates a UTF-8 string to the specified number of characters.
Definition: unicode.cpp:124
utf8::string lowercase(const utf8::string &s)
Returns a lowercased version of the string.
Definition: unicode.cpp:53
ucs4::iterator_base< std::string, ucs4_convert_impl::convert_impl< char_t >::type > iterator
Definition: unicode.hpp:42
For Win32 API.
Definition: unicode.hpp:33
GLenum GLsizei len
Definition: glew.h:5662
GLuint start
Definition: glew.h:1221
utf8::string & truncate(utf8::string &str, const size_t size)
Truncates a UTF-8 string to the specified number of characters.
Definition: unicode.cpp:119
size_t size(const utf8::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:88
Functions for converting Unicode wide-char strings to UTF-8 encoded strings, back and forth...
Definition: unicode.cpp:36
GLuint index
Definition: glew.h:1782
GLsizeiptr size
Definition: glew.h:1649
ucs4::iterator_base< utf16::string, ucs4_convert_impl::convert_impl< char_t >::type > iterator
Definition: unicode.hpp:34
utf8::string & insert(utf8::string &str, const size_t pos, const utf8::string &insert)
Insert a UTF-8 string at the specified position.
Definition: unicode.cpp:101
GLdouble s
Definition: glew.h:1358
std::string string
utf8::string & erase(utf8::string &str, const size_t start, const size_t len)
Erases a portion of a UTF-8 string.
Definition: unicode.cpp:106