OpenXcom  1.0
Open-source clone of the original X-Com
Unicode.h
1 #pragma once
2 /*
3  * Copyright 2010-2016 OpenXcom Developers.
4  *
5  * This file is part of OpenXcom.
6  *
7  * OpenXcom is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * OpenXcom is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *e
17  * You should have received a copy of the GNU General Public License
18  * along with OpenXcom. If not, see <http://www.gnu.org/licenses/>.
19  */
20 #include <string>
21 #include <stdint.h>
22 #include <SDL.h>
23 
24 namespace OpenXcom
25 {
26 
31 typedef Uint32 UCode;
36 typedef std::basic_string<UCode> UString;
37 
42 namespace Unicode
43 {
44  /* Special text tokens */
45  const char TOK_NL_SMALL = 2;
46  const char TOK_COLOR_FLIP = 1;
47  const unsigned char TOK_NBSP = 0xA0;
48 
50  inline bool isLinebreak(UCode c) { return (c == '\n' || c == TOK_NL_SMALL); }
52  inline bool isSpace(UCode c) { return (c == ' ' || c == TOK_NBSP); }
54  inline bool isSeparator(UCode c) { return (c == '-' || c == '/'); }
56  inline bool isPrintable(UCode c) { return (c > 32 && c != TOK_NBSP); }
57 
59  void getUtf8Locale();
61  UString convUtf8ToUtf32(const std::string &src);
63  std::string convUtf32ToUtf8(const UString &src);
65  std::string convPathToUtf8(const std::string &src);
67  std::string convUtf8ToPath(const std::string &src);
68 
70  bool naturalCompare(const std::string &a, const std::string &b);
72  bool caseCompare(const std::string &a, const std::string &b);
74  bool caseFind(const std::string &haystack, const std::string &needle);
76  void upperCase(std::string &s);
78  void lowerCase(std::string &s);
80  void replace(std::string &str, const std::string &find, const std::string &replace);
82  std::string formatNumber(int64_t value, const std::string &currency = "");
84  std::string formatFunding(int64_t funds);
86  std::string formatPercentage(int value);
87 }
88 
89 }
void replace(std::string &str, const std::string &find, const std::string &replace)
Replaces every instance of a substring.
Definition: Unicode.cpp:359
bool isSeparator(UCode c)
Checks if a character is a word separator.
Definition: Unicode.h:54
bool isPrintable(UCode c)
Checks if a character is visible to the user.
Definition: Unicode.h:56
std::string convUtf32ToUtf8(const UString &src)
Takes a Unicode 32-bit string and converts it to a 8-bit string encoded in UTF-8. ...
Definition: Unicode.cpp:132
std::string formatFunding(int64_t funds)
Takes an integer value and formats it as currency, spacing the thousands and adding a $ sign to the f...
Definition: Unicode.cpp:404
std::string convPathToUtf8(const std::string &src)
Takes a filesystem path and converts it to a UTF-8 string.
Definition: Unicode.cpp:225
void lowerCase(std::string &s)
Lowercases a UTF-8 string, modified in place.
Definition: Unicode.cpp:338
bool isLinebreak(UCode c)
non-breaking space
Definition: Unicode.h:50
const unsigned char TOK_NBSP
alternate between primary and secondary color
Definition: Unicode.h:47
std::string convUtf8ToPath(const std::string &src)
Takes a UTF-8 string and converts it to a filesystem path.
Definition: Unicode.cpp:241
void upperCase(std::string &s)
Uppercases a UTF-8 string, modified in place.
Definition: Unicode.cpp:318
UString convUtf8ToUtf32(const std::string &src)
Takes a Unicode 32-bit string and converts it to a 8-bit string encoded in UTF-8. ...
Definition: Unicode.cpp:95
bool isSpace(UCode c)
Checks if a character is a blank space (includes non-breaking spaces).
Definition: Unicode.h:52
std::string formatPercentage(int value)
Takes an integer value and formats it as percentage, adding a % sign.
Definition: Unicode.cpp:415
void getUtf8Locale()
Store a UTF-8 locale to use when dealing with character conversions.
Definition: Unicode.cpp:49
bool naturalCompare(const std::string &a, const std::string &b)
Compares two UTF-8 strings using natural human ordering.
Definition: Unicode.cpp:256
std::string formatNumber(int64_t value, const std::string &currency)
Takes an integer value and formats it as number with separators (spacing the thousands).
Definition: Unicode.cpp:373
bool caseFind(const std::string &haystack, const std::string &needle)
Searches for a substring in another string ignoring case.
Definition: Unicode.cpp:298
bool caseCompare(const std::string &a, const std::string &b)
Compares two UTF-8 strings ignoring case.
Definition: Unicode.cpp:281
const char TOK_COLOR_FLIP
line break and change to small font
Definition: Unicode.h:46
Definition: BaseInfoState.cpp:40