The Battle for Wesnoth  1.15.1+dev
unicode.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 by David White <dave@whitevine.net>
3  Copyright (C) 2005 - 2018 by Guillaume Melquiond <guillaume.melquiond@gmail.com>
4  Part of the Battle for Wesnoth Project https://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 #pragma once
17 
18 #include "ucs4_iterator_base.hpp"
19 #include "ucs4_convert_impl.hpp"
20 #include "unicode_cast.hpp"
21 
22 #include <string>
23 #include <vector>
24 
25 /**
26  * For Win32 API.
27  *
28  * On Windows, wchar_t is defined as uint16_t.
29  * Wide strings are expected to be UTF-16.
30  */
31 namespace utf16 {
33 }
34 
35 /**
36  * Functions for converting Unicode wide-char strings to UTF-8 encoded strings,
37  * back and forth.
38  */
39 namespace utf8 {
41 
42  /** Returns a lowercased version of the string. */
43  std::string lowercase(const std::string& s);
44 
45  /**
46  * Codepoint index corresponding to the nth character in a UTF-8 string.
47  *
48  * @return str.length() if there are less than @p index characters.
49  */
50  std::size_t index(const std::string& str, const std::size_t index);
51 
52  /** Length in characters of a UTF-8 string. */
53  std::size_t size(const std::string& str);
54 
55  /** Insert a UTF-8 string at the specified position. */
56  std::string& insert(std::string& str, const std::size_t pos, const std::string& insert);
57 
58  /**
59  * Erases a portion of a UTF-8 string.
60  *
61  * @param str UTF-8 encoded string.
62  * @param start Start position.
63  * @param len Number of characters to erase.
64  *
65  * @note This implementation does not check for valid UTF-8. Don't use it
66  * for user input.
67  */
68  std::string& erase(std::string& str, const std::size_t start, const std::size_t len = std::string::npos);
69 
70  /**
71  * Truncates a UTF-8 string to the specified number of characters.
72  *
73  * @param str UTF-8 encoded string.
74  * @param size Size to truncate to.
75  *
76  * @note This implementation does not check for valid UTF-8. Don't use it
77  * for user input.
78  */
79  std::string& truncate(std::string& str, const std::size_t size);
80 
81  /**
82  * Truncates a UTF-8 string to the specified number of characters.
83  *
84  * If the string has more than @p size UTF-8 characters it will be
85  * truncated to this size.
86  *
87  * The output is guaranteed to be valid UTF-8.
88  *
89  * @param[in,out] str [in] String encoded in UTF-8.
90  * [out] String encoded UTF-8 that contains at most @p size
91  * codepoints.
92  * @param size The size to truncate to.
93  */
94  void truncate_as_ucs4(std::string& str, const std::size_t size);
95 } // end namespace utf8
ucs4::iterator_base< std::u16string, ucs4_convert_impl::convert_impl< char16_t >::type > iterator
Definition: unicode.hpp:32
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:122
For Win32 API.
Definition: unicode.hpp:31
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:86
void erase(const std::string &key)
Definition: general.cpp:220
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:117
Functions for converting Unicode wide-char strings to UTF-8 encoded strings, back and forth...
Definition: unicode.cpp:34
static map_location::DIRECTION s
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:99
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:71
std::string lowercase(const std::string &s)
Returns a lowercased version of the string.
Definition: unicode.cpp:51
EXIT_STATUS start(const std::string &filename, bool take_screenshot, const std::string &screenshot_filename)
Main interface for launching the editor from the title screen.
Definition: editor_main.cpp:28