The Battle for Wesnoth  1.19.5+dev
unicode.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2024
3  by Philippe Plantier <ayin@anathas.org>
4  Copyright (C) 2005 by Guillaume Melquiond <guillaume.melquiond@gmail.com>
5  Copyright (C) 2003 by David White <dave@whitevine.net>
6  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
7 
8  This program is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 2 of the License, or
11  (at your option) any later version.
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY.
14 
15  See the COPYING file for more details.
16 */
17 
18 #pragma once
19 
20 #include "ucs4_iterator_base.hpp"
21 #include "ucs4_convert_impl.hpp"
22 #include "unicode_cast.hpp"
23 
24 #include <string>
25 
26 /**
27  * For Win32 API.
28  *
29  * On Windows, wchar_t is defined as uint16_t.
30  * Wide strings are expected to be UTF-16.
31  */
32 namespace utf16 {
34 }
35 
36 /**
37  * Functions for converting Unicode wide-char strings to UTF-8 encoded strings,
38  * back and forth.
39  */
40 namespace utf8 {
42 
43  /** Returns a lowercased version of the string. */
44  std::string lowercase(const std::string& s);
45 
46  /**
47  * Codepoint index corresponding to the nth character in a UTF-8 string.
48  *
49  * @return str.length() if there are less than @p index characters.
50  */
51  std::size_t index(const std::string& str, const std::size_t index);
52 
53  /** Length in characters of a UTF-8 string. */
54  std::size_t size(const std::string& str);
55 
56  /** Insert a UTF-8 string at the specified position. */
57  std::string& insert(std::string& str, const std::size_t pos, const std::string& insert);
58 
59  /**
60  * Erases a portion of a UTF-8 string.
61  *
62  * @param str UTF-8 encoded string.
63  * @param start Start position.
64  * @param len Number of characters to erase.
65  *
66  * @note This implementation does not check for valid UTF-8. Don't use it
67  * for user input.
68  */
69  std::string& erase(std::string& str, const std::size_t start, const std::size_t len = std::string::npos);
70 
71  /**
72  * Truncates a UTF-8 string to the specified number of characters.
73  *
74  * @param str UTF-8 encoded string.
75  * @param size Size to truncate to.
76  *
77  * @note This implementation does not check for valid UTF-8. Don't use it
78  * for user input.
79  */
80  std::string& truncate(std::string& str, const std::size_t size);
81 
82  /**
83  * Truncates a UTF-8 string to the specified number of characters.
84  *
85  * If the string has more than @p size UTF-8 characters it will be
86  * truncated to this size.
87  *
88  * The output is guaranteed to be valid UTF-8.
89  *
90  * @param[in,out] str [in] String encoded in UTF-8.
91  * [out] String encoded UTF-8 that contains at most @p size
92  * codepoints.
93  * @param size The size to truncate to.
94  */
95  void truncate_as_ucs4(std::string& str, const std::size_t size);
96 } // end namespace utf8
EXIT_STATUS start(bool clear_id, const std::string &filename, bool take_screenshot, const std::string &screenshot_filename)
Main interface for launching the editor from the title screen.
For Win32 API.
Definition: unicode.hpp:32
ucs4::iterator_base< std::u16string, ucs4_convert_impl::convert_impl< char16_t >::type > iterator
Definition: unicode.hpp:33
Functions for converting Unicode wide-char strings to UTF-8 encoded strings, back and forth.
Definition: unicode.cpp:33
std::size_t index(const std::string &str, const std::size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
Definition: unicode.cpp:70
std::string & insert(std::string &str, const std::size_t pos, const std::string &insert)
Insert a UTF-8 string at the specified position.
Definition: unicode.cpp:98
std::string lowercase(const std::string &s)
Returns a lowercased version of the string.
Definition: unicode.cpp:50
std::string & erase(std::string &str, const std::size_t start, const std::size_t len)
Erases a portion of a UTF-8 string.
Definition: unicode.cpp:103
ucs4::iterator_base< std::string, ucs4_convert_impl::convert_impl< char >::type > iterator
Definition: unicode.hpp:41
void truncate_as_ucs4(std::string &str, const std::size_t size)
Truncates a UTF-8 string to the specified number of characters.
Definition: unicode.cpp:121
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:85
std::string & truncate(std::string &str, const std::size_t size)
Truncates a UTF-8 string to the specified number of characters.
Definition: unicode.cpp:116
static map_location::direction s