1
0

mptString.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  1. /*
  2. * mptString.cpp
  3. * -------------
  4. * Purpose: Small string-related utilities, number and message formatting.
  5. * Notes : Currently none.
  6. * Authors: OpenMPT Devs
  7. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  8. */
  9. #include "stdafx.h"
  10. #include "mptString.h"
  11. #include "mpt/string/types.hpp"
  12. #include "mpt/string/utility.hpp"
  13. #include "mpt/string_transcode/transcode.hpp"
  14. #include <locale>
  15. #include <string>
  16. #include <vector>
  17. #include <cstdlib>
  18. #if defined(MODPLUG_TRACKER)
  19. #include <cwctype>
  20. #endif // MODPLUG_TRACKER
  21. #if defined(MODPLUG_TRACKER)
  22. #include <wctype.h>
  23. #endif // MODPLUG_TRACKER
  24. #if MPT_OS_WINDOWS
  25. #include <windows.h>
  26. #endif // MPT_OS_WINDOWS
  27. OPENMPT_NAMESPACE_BEGIN
  28. /*
  29. Quick guide to the OpenMPT string type jungle
  30. =============================================
  31. This quick guide is only meant as a hint. There may be valid reasons to not
  32. honor the recommendations found here. Staying consistent with surrounding and/or
  33. related code sections may also be important.
  34. List of string types
  35. --------------------
  36. * std::string (OpenMPT, libopenmpt)
  37. C++ string of unspecifed 8bit encoding. Try to always document the
  38. encoding if not clear from context. Do not use unless there is an obvious
  39. reason to do so.
  40. * std::wstring (OpenMPT)
  41. UTF16 (on windows) or UTF32 (otherwise). Do not use unless there is an
  42. obvious reason to do so.
  43. * mpt::lstring (OpenMPT)
  44. OpenMPT locale string type. The encoding is always CP_ACP. Do not use unless
  45. there is an obvious reason to do so.
  46. * char* (OpenMPT, libopenmpt)
  47. C string of unspecified encoding. Use only for static literals or in
  48. performance critical inner loops where full control and avoidance of memory
  49. allocations is required.
  50. * wchar_t* (OpenMPT)
  51. C wide string. Use only if Unicode is required for static literals or in
  52. performance critical inner loops where full control and avoidance of memory
  53. allocation is required.
  54. * mpt::winstring (OpenMPT)
  55. OpenMPT type-safe string to interface with native WinAPI, either encoded in
  56. locale/CP_ACP (if !UNICODE) or UTF16 (if UNICODE).
  57. * CString (OpenMPT)
  58. MFC string type, either encoded in locale/CP_ACP (if !UNICODE) or UTF16 (if
  59. UNICODE). Specify literals with _T(""). Use in MFC GUI code.
  60. * CStringA (OpenMPT)
  61. MFC ANSI string type. The encoding is always CP_ACP. Do not use.
  62. * CStringW (OpenMPT)
  63. MFC Unicode string type. Do not use.
  64. * mpt::PathString (OpenMPT, libopenmpt)
  65. String type representing paths and filenames. Always use for these in order
  66. to avoid potentially lossy conversions. Use P_("") macro for
  67. literals.
  68. * mpt::ustring (OpenMPT, libopenmpt)
  69. The default unicode string type. Can be encoded in UTF8 or UTF16 or UTF32,
  70. depending on MPT_USTRING_MODE_* and sizeof(wchar_t). Literals can written as
  71. U_(""). Use as your default string type if no other string type is
  72. a measurably better fit.
  73. * MPT_UTF8 (OpenMPT, libopenmpt)
  74. Macro that generates a mpt::ustring from string literals containing
  75. non-ascii characters. In order to keep the source code in ascii encoding,
  76. always express non-ascii characters using explicit \x23 escaping. Note that
  77. depending on the underlying type of mpt::ustring, MPT_UTF8 *requires* a
  78. runtime conversion. Only use for string literals containing non-ascii
  79. characters (use MPT_USTRING otherwise).
  80. * MPT_ULITERAL / MPT_UCHAR / mpt::uchar (OpenMPT, libopenmpt)
  81. Macros which generate string literals, char literals and the char literal
  82. type respectively. These are especially useful in constexpr contexts or
  83. global data where MPT_USTRING is either unusable or requires a global
  84. contructor to run. Do NOT use as a performance optimization in place of
  85. MPT_USTRING however, because MPT_USTRING can be converted to C++11/14 user
  86. defined literals eventually, while MPT_ULITERAL cannot because of constexpr
  87. requirements.
  88. * mpt::RawPathString (OpenMPT, libopenmpt)
  89. Internal representation of mpt::PathString. Only use for parsing path
  90. fragments.
  91. * mpt::u8string (OpenMPT, libopenmpt)
  92. Internal representation of mpt::ustring. Do not use directly. Ever.
  93. * std::basic_string<char> (OpenMPT)
  94. Same as std::string. Do not use std::basic_string in the templated form.
  95. * std::basic_string<wchar_t> (OpenMPT)
  96. Same as std::wstring. Do not use std::basic_string in the templated form.
  97. The following string types are available in order to avoid the need to overload
  98. functions on a huge variety of string types. Use only ever as function argument
  99. types.
  100. Note that the locale charset is not available on all libopenmpt builds (in which
  101. case the option is ignored or a sensible fallback is used; these types are
  102. always available).
  103. All these types publicly inherit from mpt::ustring and do not contain any
  104. additional state. This means that they work the same way as mpt::ustring does
  105. and do support type-slicing for both, read and write accesses.
  106. These types only add conversion constructors for all string types that have a
  107. defined encoding and for all 8bit string types using the specified encoding
  108. heuristic.
  109. * AnyUnicodeString (OpenMPT, libopenmpt)
  110. Is constructible from any Unicode string.
  111. * AnyString (OpenMPT, libopenmpt)
  112. Tries to do the smartest auto-magic we can do.
  113. * AnyStringLocale (OpenMPT, libopenmpt)
  114. char-based strings are assumed to be in locale encoding.
  115. * AnyStringUTF8orLocale (OpenMPT, libopenmpt)
  116. char-based strings are tried in UTF8 first, if this fails, locale is used.
  117. * AnyStringUTF8 (OpenMPT, libopenmpt)
  118. char-based strings are assumed to be in UTF8.
  119. Encoding of 8bit strings
  120. ------------------------
  121. 8bit strings have an unspecified encoding. When the string is contained within a
  122. CSoundFile object, the encoding is most likely CSoundFile::GetCharsetInternal(),
  123. otherwise, try to gather the most probable encoding from surrounding or related
  124. code sections.
  125. Decision tree to help deciding which string type to use
  126. -------------------------------------------------------
  127. if in libopenmpt
  128. if in libopenmpt c++ interface
  129. T = std::string, the encoding is utf8
  130. elif in libopenmpt c interface
  131. T = char*, the encoding is utf8
  132. elif performance critical inner loop
  133. T = char*, document the encoding if not clear from context
  134. elif string literal containing non-ascii characters
  135. T = MPT_UTF8
  136. elif path or file
  137. if parsing path fragments
  138. T = mpt::RawPathString
  139. template your function on the concrete underlying string type
  140. (std::string and std::wstring) or use preprocessor MPT_OS_WINDOWS
  141. else
  142. T = mpt::PathString
  143. fi
  144. else
  145. T = mpt::ustring
  146. fi
  147. else
  148. if performance critical inner loop
  149. if needs unicode support
  150. T = mpt::uchar* / MPT_ULITERAL
  151. else
  152. T = char*, document the encoding if not clear from context
  153. fi
  154. elif string literal containing non-ascii characters
  155. T = MPT_UTF8
  156. elif path or file
  157. if parsing path fragments
  158. T = mpt::RawPathString
  159. template your function on the concrete underlying string type
  160. (std::string and std::wstring) or use preprocessor MPT_OS_WINDOWS
  161. else
  162. T = mpt::PathString
  163. fi
  164. elif winapi interfacing code
  165. T = mpt::winstring
  166. elif mfc/gui code
  167. T = CString
  168. else
  169. if constexpr context or global data
  170. T = mpt::uchar* / MPT_ULITERAL
  171. else
  172. T = mpt::ustring
  173. fi
  174. fi
  175. fi
  176. This boils down to: Prefer mpt::PathString and mpt::ustring, and only use any
  177. other string type if there is an obvious reason to do so.
  178. Character set conversions
  179. -------------------------
  180. Character set conversions in OpenMPT are always fuzzy.
  181. Behaviour in case of an invalid source encoding and behaviour in case of an
  182. unrepresentable destination encoding can be any of the following:
  183. * The character is replaced by some replacement character ('?' or L'\ufffd' in
  184. most cases).
  185. * The character is replaced by a similar character (either semantically
  186. similiar or visually similar).
  187. * The character is transcribed with some ASCII text.
  188. * The character is discarded.
  189. * Conversion stops at this very character.
  190. Additionally. conversion may stop or continue on \0 characters in the middle of
  191. the string.
  192. Behaviour can vary from one conversion tuple to any other.
  193. If you need to ensure lossless conversion, do a roundtrip conversion and check
  194. for equality.
  195. Unicode handling
  196. ----------------
  197. OpenMPT is generally not aware of and does not handle different Unicode
  198. normalization forms.
  199. You should be aware of the following possibilities:
  200. * Conversion between UTF8, UTF16, UTF32 may or may not change between NFC and
  201. NFD.
  202. * Conversion from any non-Unicode 8bit encoding can result in both, NFC or NFD
  203. forms.
  204. * Conversion to any non-Unicode 8bit encoding may or may not involve
  205. conversion to NFC, NFD, NFKC or NFKD during the conversion. This in
  206. particular means that conversion of decomposed german umlauts to ISO8859-1
  207. may fail.
  208. * Changing the normalization form of path strings may render the file
  209. inaccessible.
  210. Unicode BOM may or may not be preserved and/or discarded during conversion.
  211. Invalid Unicode code points may be treated as invalid or as valid characters
  212. when converting between different Unicode encodings.
  213. Interfacing with WinAPI
  214. -----------------------
  215. When in MFC code, use CString.
  216. When in non MFC code, either use std::wstring when directly interfacing with
  217. APIs only available in WCHAR variants, or use mpt::winstring and
  218. mpt::WinStringBuf helpers otherwise.
  219. Specify TCHAR string literals with _T("foo") in mptrack/, and with TEXT("foo")
  220. in common/ or sounddev/. _T() requires <tchar.h> which is specific to the MSVC
  221. runtime and not portable across compilers. TEXT() is from <windows.h>. We use
  222. _T() in mptrack/ only because it is shorter.
  223. */
  224. namespace mpt { namespace String {
  225. #define C(x) (mpt::char_value((x)))
  226. // AMS1 actually only supports ASCII plus the modified control characters and no high chars at all.
  227. // Just default to CP437 for those to keep things simple.
  228. static constexpr char32_t CharsetTableCP437AMS[256] = {
  229. C(' '),0x0001,0x0002,0x0003,0x00e4,0x0005,0x00e5,0x0007,0x0008,0x0009,0x000a,0x000b,0x000c,0x000d,0x00c4,0x00c5, // differs from CP437
  230. 0x0010,0x0011,0x0012,0x0013,0x00f6,0x0015,0x0016,0x0017,0x0018,0x00d6,0x001a,0x001b,0x001c,0x001d,0x001e,0x001f, // differs from CP437
  231. 0x0020,0x0021,0x0022,0x0023,0x0024,0x0025,0x0026,0x0027,0x0028,0x0029,0x002a,0x002b,0x002c,0x002d,0x002e,0x002f,
  232. 0x0030,0x0031,0x0032,0x0033,0x0034,0x0035,0x0036,0x0037,0x0038,0x0039,0x003a,0x003b,0x003c,0x003d,0x003e,0x003f,
  233. 0x0040,0x0041,0x0042,0x0043,0x0044,0x0045,0x0046,0x0047,0x0048,0x0049,0x004a,0x004b,0x004c,0x004d,0x004e,0x004f,
  234. 0x0050,0x0051,0x0052,0x0053,0x0054,0x0055,0x0056,0x0057,0x0058,0x0059,0x005a,0x005b,0x005c,0x005d,0x005e,0x005f,
  235. 0x0060,0x0061,0x0062,0x0063,0x0064,0x0065,0x0066,0x0067,0x0068,0x0069,0x006a,0x006b,0x006c,0x006d,0x006e,0x006f,
  236. 0x0070,0x0071,0x0072,0x0073,0x0074,0x0075,0x0076,0x0077,0x0078,0x0079,0x007a,0x007b,0x007c,0x007d,0x007e,0x2302,
  237. 0x00c7,0x00fc,0x00e9,0x00e2,0x00e4,0x00e0,0x00e5,0x00e7,0x00ea,0x00eb,0x00e8,0x00ef,0x00ee,0x00ec,0x00c4,0x00c5,
  238. 0x00c9,0x00e6,0x00c6,0x00f4,0x00f6,0x00f2,0x00fb,0x00f9,0x00ff,0x00d6,0x00dc,0x00a2,0x00a3,0x00a5,0x20a7,0x0192,
  239. 0x00e1,0x00ed,0x00f3,0x00fa,0x00f1,0x00d1,0x00aa,0x00ba,0x00bf,0x2310,0x00ac,0x00bd,0x00bc,0x00a1,0x00ab,0x00bb,
  240. 0x2591,0x2592,0x2593,0x2502,0x2524,0x2561,0x2562,0x2556,0x2555,0x2563,0x2551,0x2557,0x255d,0x255c,0x255b,0x2510,
  241. 0x2514,0x2534,0x252c,0x251c,0x2500,0x253c,0x255e,0x255f,0x255a,0x2554,0x2569,0x2566,0x2560,0x2550,0x256c,0x2567,
  242. 0x2568,0x2564,0x2565,0x2559,0x2558,0x2552,0x2553,0x256b,0x256a,0x2518,0x250c,0x2588,0x2584,0x258c,0x2590,0x2580,
  243. 0x03b1,0x00df,0x0393,0x03c0,0x03a3,0x03c3,0x00b5,0x03c4,0x03a6,0x0398,0x03a9,0x03b4,0x221e,0x03c6,0x03b5,0x2229,
  244. 0x2261,0x00b1,0x2265,0x2264,0x2320,0x2321,0x00f7,0x2248,0x00b0,0x2219,0x00b7,0x221a,0x207f,0x00b2,0x25a0,0x00a0
  245. };
  246. // AMS2: Looking at Velvet Studio's bitmap font (TPIC32.PCX), these appear to be the only supported non-ASCII chars.
  247. static constexpr char32_t CharsetTableCP437AMS2[256] = {
  248. C(' '),0x00a9,0x221a,0x00b7,C('0'),C('1'),C('2'),C('3'),C('4'),C('5'),C('6'),C('7'),C('8'),C('9'),C('A'),C('B'), // differs from CP437
  249. C('C'),C('D'),C('E'),C('F'),C(' '),0x00a7,C(' '),C(' '),C(' '),C(' '),C(' '),C(' '),C(' '),C(' '),C(' '),C(' '), // differs from CP437
  250. 0x0020,0x0021,0x0022,0x0023,0x0024,0x0025,0x0026,0x0027,0x0028,0x0029,0x002a,0x002b,0x002c,0x002d,0x002e,0x002f,
  251. 0x0030,0x0031,0x0032,0x0033,0x0034,0x0035,0x0036,0x0037,0x0038,0x0039,0x003a,0x003b,0x003c,0x003d,0x003e,0x003f,
  252. 0x0040,0x0041,0x0042,0x0043,0x0044,0x0045,0x0046,0x0047,0x0048,0x0049,0x004a,0x004b,0x004c,0x004d,0x004e,0x004f,
  253. 0x0050,0x0051,0x0052,0x0053,0x0054,0x0055,0x0056,0x0057,0x0058,0x0059,0x005a,0x005b,0x005c,0x005d,0x005e,0x005f,
  254. 0x0060,0x0061,0x0062,0x0063,0x0064,0x0065,0x0066,0x0067,0x0068,0x0069,0x006a,0x006b,0x006c,0x006d,0x006e,0x006f,
  255. 0x0070,0x0071,0x0072,0x0073,0x0074,0x0075,0x0076,0x0077,0x0078,0x0079,0x007a,0x007b,0x007c,0x007d,0x007e,0x2302,
  256. 0x00c7,0x00fc,0x00e9,0x00e2,0x00e4,0x00e0,0x00e5,0x00e7,0x00ea,0x00eb,0x00e8,0x00ef,0x00ee,0x00ec,0x00c4,0x00c5,
  257. 0x00c9,0x00e6,0x00c6,0x00f4,0x00f6,0x00f2,0x00fb,0x00f9,0x00ff,0x00d6,0x00dc,0x00a2,0x00a3,0x00a5,0x20a7,0x0192,
  258. 0x00e1,0x00ed,0x00f3,0x00fa,0x00f1,0x00d1,0x00aa,0x00ba,0x00bf,0x2310,0x00ac,0x00bd,0x00bc,0x00a1,0x00ab,0x00bb,
  259. 0x2591,0x2592,0x2593,0x2502,0x2524,0x2561,0x2562,0x2556,0x2555,0x2563,0x2551,0x2557,0x255d,0x255c,0x255b,0x2510,
  260. 0x2514,0x2534,0x252c,0x251c,0x2500,0x253c,0x255e,0x255f,0x255a,0x2554,0x2569,0x2566,0x2560,0x2550,0x256c,0x2567,
  261. 0x2568,0x2564,0x2565,0x2559,0x2558,0x2552,0x2553,0x256b,0x256a,0x2518,0x250c,0x2588,0x2584,0x258c,0x2590,0x2580,
  262. 0x03b1,0x00df,0x0393,0x03c0,0x03a3,0x03c3,0x00b5,0x03c4,0x03a6,0x0398,0x03a9,0x03b4,0x221e,0x03c6,0x03b5,0x2229,
  263. 0x2261,0x00b1,0x2265,0x2264,0x2320,0x2321,0x00f7,0x2248,0x00b0,0x2219,0x00b7,0x221a,0x207f,0x00b2,0x25a0,0x00a0
  264. };
  265. #undef C
  266. // templated on 8bit strings because of type-safe variants
  267. template<typename Tdststring>
  268. static Tdststring EncodeImpl(Charset charset, const mpt::widestring &src)
  269. {
  270. static_assert(sizeof(typename Tdststring::value_type) == sizeof(char));
  271. static_assert(mpt::is_character<typename Tdststring::value_type>::value);
  272. switch(charset)
  273. {
  274. #if defined(MPT_ENABLE_CHARSET_LOCALE)
  275. case Charset::Locale: return mpt::encode<Tdststring>(mpt::logical_encoding::locale, src); break;
  276. #endif
  277. case Charset::UTF8: return mpt::encode<Tdststring>(mpt::common_encoding::utf8, src); break;
  278. case Charset::ASCII: return mpt::encode<Tdststring>(mpt::common_encoding::ascii, src); break;
  279. case Charset::ISO8859_1: return mpt::encode<Tdststring>(mpt::common_encoding::iso8859_1, src); break;
  280. case Charset::ISO8859_15: return mpt::encode<Tdststring>(mpt::common_encoding::iso8859_15, src); break;
  281. case Charset::CP850: return mpt::encode<Tdststring>(mpt::common_encoding::cp850, src); break;
  282. case Charset::CP437: return mpt::encode<Tdststring>(mpt::common_encoding::cp437, src); break;
  283. case Charset::CP437AMS: return mpt::encode<Tdststring>(CharsetTableCP437AMS, src); break;
  284. case Charset::CP437AMS2: return mpt::encode<Tdststring>(CharsetTableCP437AMS2, src); break;
  285. case Charset::Windows1252: return mpt::encode<Tdststring>(mpt::common_encoding::windows1252, src); break;
  286. case Charset::Amiga: return mpt::encode<Tdststring>(mpt::common_encoding::amiga, src); break;
  287. case Charset::RISC_OS: return mpt::encode<Tdststring>(mpt::common_encoding::riscos, src); break;
  288. case Charset::ISO8859_1_no_C1: return mpt::encode<Tdststring>(mpt::common_encoding::iso8859_1_no_c1, src); break;
  289. case Charset::ISO8859_15_no_C1: return mpt::encode<Tdststring>(mpt::common_encoding::iso8859_15_no_c1, src); break;
  290. case Charset::Amiga_no_C1: return mpt::encode<Tdststring>(mpt::common_encoding::amiga_no_c1, src); break;
  291. }
  292. return Tdststring();
  293. }
  294. // templated on 8bit strings because of type-safe variants
  295. template<typename Tsrcstring>
  296. static mpt::widestring DecodeImpl(Charset charset, const Tsrcstring &src)
  297. {
  298. static_assert(sizeof(typename Tsrcstring::value_type) == sizeof(char));
  299. static_assert(mpt::is_character<typename Tsrcstring::value_type>::value);
  300. switch(charset)
  301. {
  302. #if defined(MPT_ENABLE_CHARSET_LOCALE)
  303. case Charset::Locale: return mpt::decode<Tsrcstring>(mpt::logical_encoding::locale, src); break;
  304. #endif
  305. case Charset::UTF8: return mpt::decode<Tsrcstring>(mpt::common_encoding::utf8, src); break;
  306. case Charset::ASCII: return mpt::decode<Tsrcstring>(mpt::common_encoding::ascii, src); break;
  307. case Charset::ISO8859_1: return mpt::decode<Tsrcstring>(mpt::common_encoding::iso8859_1, src); break;
  308. case Charset::ISO8859_15: return mpt::decode<Tsrcstring>(mpt::common_encoding::iso8859_15, src); break;
  309. case Charset::CP850: return mpt::decode<Tsrcstring>(mpt::common_encoding::cp850, src); break;
  310. case Charset::CP437: return mpt::decode<Tsrcstring>(mpt::common_encoding::cp437, src); break;
  311. case Charset::CP437AMS: return mpt::decode<Tsrcstring>(CharsetTableCP437AMS, src); break;
  312. case Charset::CP437AMS2: return mpt::decode<Tsrcstring>(CharsetTableCP437AMS2, src); break;
  313. case Charset::Windows1252: return mpt::decode<Tsrcstring>(mpt::common_encoding::windows1252, src); break;
  314. case Charset::Amiga: return mpt::decode<Tsrcstring>(mpt::common_encoding::amiga, src); break;
  315. case Charset::RISC_OS: return mpt::decode<Tsrcstring>(mpt::common_encoding::riscos, src); break;
  316. case Charset::ISO8859_1_no_C1: return mpt::decode<Tsrcstring>(mpt::common_encoding::iso8859_1_no_c1, src); break;
  317. case Charset::ISO8859_15_no_C1: return mpt::decode<Tsrcstring>(mpt::common_encoding::iso8859_15_no_c1, src); break;
  318. case Charset::Amiga_no_C1: return mpt::decode<Tsrcstring>(mpt::common_encoding::amiga_no_c1, src); break;
  319. }
  320. return mpt::widestring();
  321. }
  322. // templated on 8bit strings because of type-safe variants
  323. template<typename Tdststring, typename Tsrcstring>
  324. static Tdststring ConvertImpl(Charset to, Charset from, const Tsrcstring &src)
  325. {
  326. static_assert(sizeof(typename Tdststring::value_type) == sizeof(char));
  327. static_assert(sizeof(typename Tsrcstring::value_type) == sizeof(char));
  328. if(to == from)
  329. {
  330. const typename Tsrcstring::value_type * src_beg = src.data();
  331. const typename Tsrcstring::value_type * src_end = src_beg + src.size();
  332. return Tdststring(reinterpret_cast<const typename Tdststring::value_type *>(src_beg), reinterpret_cast<const typename Tdststring::value_type *>(src_end));
  333. }
  334. return EncodeImpl<Tdststring>(to, DecodeImpl(from, src));
  335. }
  336. } // namespace String
  337. bool IsUTF8(const std::string &str)
  338. {
  339. return mpt::is_utf8(str);
  340. }
  341. #if MPT_WSTRING_CONVERT
  342. std::wstring ToWide(Charset from, const std::string &str)
  343. {
  344. return String::DecodeImpl(from, str);
  345. }
  346. #if defined(MPT_ENABLE_CHARSET_LOCALE)
  347. std::wstring ToWide(const mpt::lstring &str)
  348. {
  349. return String::DecodeImpl(Charset::Locale, str);
  350. }
  351. #endif // MPT_ENABLE_CHARSET_LOCALE
  352. #endif
  353. #if MPT_WSTRING_CONVERT
  354. std::string ToCharset(Charset to, const std::wstring &str)
  355. {
  356. return String::EncodeImpl<std::string>(to, str);
  357. }
  358. #endif
  359. std::string ToCharset(Charset to, Charset from, const std::string &str)
  360. {
  361. return String::ConvertImpl<std::string>(to, from, str);
  362. }
  363. #if defined(MPT_ENABLE_CHARSET_LOCALE)
  364. std::string ToCharset(Charset to, const mpt::lstring &str)
  365. {
  366. return String::ConvertImpl<std::string>(to, Charset::Locale, str);
  367. }
  368. #endif // MPT_ENABLE_CHARSET_LOCALE
  369. #if defined(MPT_ENABLE_CHARSET_LOCALE)
  370. #if MPT_WSTRING_CONVERT
  371. mpt::lstring ToLocale(const std::wstring &str)
  372. {
  373. return String::EncodeImpl<mpt::lstring>(Charset::Locale, str);
  374. }
  375. #endif
  376. mpt::lstring ToLocale(Charset from, const std::string &str)
  377. {
  378. return String::ConvertImpl<mpt::lstring>(Charset::Locale, from, str);
  379. }
  380. #endif // MPT_ENABLE_CHARSET_LOCALE
  381. #if MPT_OS_WINDOWS
  382. #if MPT_WSTRING_CONVERT
  383. mpt::winstring ToWin(const std::wstring &str)
  384. {
  385. #ifdef UNICODE
  386. return str;
  387. #else
  388. return ToLocale(str);
  389. #endif
  390. }
  391. #endif
  392. mpt::winstring ToWin(Charset from, const std::string &str)
  393. {
  394. #ifdef UNICODE
  395. return ToWide(from, str);
  396. #else
  397. return ToLocale(from, str);
  398. #endif
  399. }
  400. #if defined(MPT_ENABLE_CHARSET_LOCALE)
  401. mpt::winstring ToWin(const mpt::lstring &str)
  402. {
  403. #ifdef UNICODE
  404. return ToWide(str);
  405. #else
  406. return str;
  407. #endif
  408. }
  409. #endif // MPT_ENABLE_CHARSET_LOCALE
  410. #endif // MPT_OS_WINDOWS
  411. #if defined(MPT_WITH_MFC)
  412. CString ToCString(const std::wstring &str)
  413. {
  414. #ifdef UNICODE
  415. return str.c_str();
  416. #else
  417. return ToCharset(Charset::Locale, str).c_str();
  418. #endif
  419. }
  420. CString ToCString(Charset from, const std::string &str)
  421. {
  422. #ifdef UNICODE
  423. return ToWide(from, str).c_str();
  424. #else
  425. return ToCharset(Charset::Locale, from, str).c_str();
  426. #endif
  427. }
  428. std::wstring ToWide(const CString &str)
  429. {
  430. #ifdef UNICODE
  431. return str.GetString();
  432. #else
  433. return ToWide(Charset::Locale, str.GetString());
  434. #endif
  435. }
  436. std::string ToCharset(Charset to, const CString &str)
  437. {
  438. #ifdef UNICODE
  439. return ToCharset(to, str.GetString());
  440. #else
  441. return ToCharset(to, Charset::Locale, str.GetString());
  442. #endif
  443. }
  444. #if defined(MPT_ENABLE_CHARSET_LOCALE)
  445. CString ToCString(const mpt::lstring &str)
  446. {
  447. #ifdef UNICODE
  448. return ToWide(str).c_str();
  449. #else
  450. return str.c_str();
  451. #endif
  452. }
  453. mpt::lstring ToLocale(const CString &str)
  454. {
  455. #ifdef UNICODE
  456. return String::EncodeImpl<mpt::lstring>(Charset::Locale, str.GetString());
  457. #else
  458. return str.GetString();
  459. #endif
  460. }
  461. #endif // MPT_ENABLE_CHARSET_LOCALE
  462. #if MPT_OS_WINDOWS
  463. mpt::winstring ToWin(const CString &str)
  464. {
  465. return str.GetString();
  466. }
  467. #endif // MPT_OS_WINDOWS
  468. #endif // MPT_WITH_MFC
  469. #if MPT_USTRING_MODE_WIDE
  470. // inline
  471. #else // !MPT_USTRING_MODE_WIDE
  472. #if MPT_WSTRING_CONVERT
  473. mpt::ustring ToUnicode(const std::wstring &str)
  474. {
  475. return String::EncodeImpl<mpt::ustring>(mpt::Charset::UTF8, str);
  476. }
  477. #endif
  478. mpt::ustring ToUnicode(Charset from, const std::string &str)
  479. {
  480. return String::ConvertImpl<mpt::ustring>(mpt::Charset::UTF8, from, str);
  481. }
  482. #if defined(MPT_ENABLE_CHARSET_LOCALE)
  483. mpt::ustring ToUnicode(const mpt::lstring &str)
  484. {
  485. return String::ConvertImpl<mpt::ustring>(mpt::Charset::UTF8, mpt::Charset::Locale, str);
  486. }
  487. #endif // MPT_ENABLE_CHARSET_LOCALE
  488. #if defined(MPT_WITH_MFC)
  489. mpt::ustring ToUnicode(const CString &str)
  490. {
  491. #ifdef UNICODE
  492. return String::EncodeImpl<mpt::ustring>(mpt::Charset::UTF8, str.GetString());
  493. #else // !UNICODE
  494. return String::ConvertImpl<mpt::ustring, std::string>(mpt::Charset::UTF8, mpt::Charset::Locale, str.GetString());
  495. #endif // UNICODE
  496. }
  497. #endif // MPT_WITH_MFC
  498. #endif // MPT_USTRING_MODE_WIDE
  499. #if MPT_USTRING_MODE_WIDE
  500. // nothing, std::wstring overloads will catch all stuff
  501. #else // !MPT_USTRING_MODE_WIDE
  502. #if MPT_WSTRING_CONVERT
  503. std::wstring ToWide(const mpt::ustring &str)
  504. {
  505. return String::DecodeImpl<mpt::ustring>(mpt::Charset::UTF8, str);
  506. }
  507. #endif
  508. std::string ToCharset(Charset to, const mpt::ustring &str)
  509. {
  510. return String::ConvertImpl<std::string, mpt::ustring>(to, mpt::Charset::UTF8, str);
  511. }
  512. #if defined(MPT_ENABLE_CHARSET_LOCALE)
  513. mpt::lstring ToLocale(const mpt::ustring &str)
  514. {
  515. return String::ConvertImpl<mpt::lstring, mpt::ustring>(mpt::Charset::Locale, mpt::Charset::UTF8, str);
  516. }
  517. #endif // MPT_ENABLE_CHARSET_LOCALE
  518. #if MPT_OS_WINDOWS
  519. mpt::winstring ToWin(const mpt::ustring &str)
  520. {
  521. #ifdef UNICODE
  522. return String::DecodeImpl<mpt::ustring>(mpt::Charset::UTF8, str);
  523. #else
  524. return String::ConvertImpl<mpt::lstring, mpt::ustring>(mpt::Charset::Locale, mpt::Charset::UTF8, str);
  525. #endif
  526. }
  527. #endif // MPT_OS_WINDOWS
  528. #if defined(MPT_WITH_MFC)
  529. CString ToCString(const mpt::ustring &str)
  530. {
  531. #ifdef UNICODE
  532. return String::DecodeImpl<mpt::ustring>(mpt::Charset::UTF8, str).c_str();
  533. #else // !UNICODE
  534. return String::ConvertImpl<std::string, mpt::ustring>(mpt::Charset::Locale, mpt::Charset::UTF8, str).c_str();
  535. #endif // UNICODE
  536. }
  537. #endif // MPT_WITH_MFC
  538. #endif // MPT_USTRING_MODE_WIDE
  539. static mpt::Charset CharsetFromCodePage(uint16 codepage, mpt::Charset fallback, bool * isFallback = nullptr)
  540. {
  541. mpt::Charset result = fallback;
  542. switch(codepage)
  543. {
  544. case 65001:
  545. result = mpt::Charset::UTF8;
  546. if(isFallback) *isFallback = false;
  547. break;
  548. case 20127:
  549. result = mpt::Charset::ASCII;
  550. if(isFallback) *isFallback = false;
  551. break;
  552. case 28591:
  553. result = mpt::Charset::ISO8859_1;
  554. if(isFallback) *isFallback = false;
  555. break;
  556. case 28605:
  557. result = mpt::Charset::ISO8859_15;
  558. if(isFallback) *isFallback = false;
  559. break;
  560. case 437:
  561. result = mpt::Charset::CP437;
  562. if(isFallback) *isFallback = false;
  563. break;
  564. case 1252:
  565. result = mpt::Charset::Windows1252;
  566. if(isFallback) *isFallback = false;
  567. break;
  568. default:
  569. result = fallback;
  570. if(isFallback) *isFallback = true;
  571. break;
  572. }
  573. return result;
  574. }
  575. mpt::ustring ToUnicode(uint16 codepage, mpt::Charset fallback, const std::string &str)
  576. {
  577. #if MPT_OS_WINDOWS
  578. mpt::ustring result;
  579. bool noCharsetMatch = true;
  580. mpt::Charset charset = mpt::CharsetFromCodePage(codepage, fallback, &noCharsetMatch);
  581. if(noCharsetMatch && mpt::has_codepage(codepage))
  582. {
  583. result = mpt::ToUnicode(mpt::decode<std::string>(codepage, str));
  584. } else
  585. {
  586. result = mpt::ToUnicode(charset, str);
  587. }
  588. return result;
  589. #else // !MPT_OS_WINDOWS
  590. return mpt::ToUnicode(mpt::CharsetFromCodePage(codepage, fallback), str);
  591. #endif // MPT_OS_WINDOWS
  592. }
  593. char ToLowerCaseAscii(char c)
  594. {
  595. return mpt::to_lower_ascii(c);
  596. }
  597. char ToUpperCaseAscii(char c)
  598. {
  599. return mpt::to_upper_ascii(c);
  600. }
  601. std::string ToLowerCaseAscii(std::string s)
  602. {
  603. std::transform(s.begin(), s.end(), s.begin(), static_cast<char(*)(char)>(&mpt::ToLowerCaseAscii));
  604. return s;
  605. }
  606. std::string ToUpperCaseAscii(std::string s)
  607. {
  608. std::transform(s.begin(), s.end(), s.begin(), static_cast<char(*)(char)>(&mpt::ToUpperCaseAscii));
  609. return s;
  610. }
  611. int CompareNoCaseAscii(const char *a, const char *b, std::size_t n)
  612. {
  613. while(n--)
  614. {
  615. unsigned char ac = mpt::char_value(mpt::ToLowerCaseAscii(*a));
  616. unsigned char bc = mpt::char_value(mpt::ToLowerCaseAscii(*b));
  617. if(ac != bc)
  618. {
  619. return ac < bc ? -1 : 1;
  620. } else if(!ac && !bc)
  621. {
  622. return 0;
  623. }
  624. ++a;
  625. ++b;
  626. }
  627. return 0;
  628. }
  629. int CompareNoCaseAscii(std::string_view a, std::string_view b)
  630. {
  631. for(std::size_t i = 0; i < std::min(a.length(), b.length()); ++i)
  632. {
  633. unsigned char ac = mpt::char_value(mpt::ToLowerCaseAscii(a[i]));
  634. unsigned char bc = mpt::char_value(mpt::ToLowerCaseAscii(b[i]));
  635. if(ac != bc)
  636. {
  637. return ac < bc ? -1 : 1;
  638. } else if(!ac && !bc)
  639. {
  640. return 0;
  641. }
  642. }
  643. if(a.length() == b.length())
  644. {
  645. return 0;
  646. }
  647. return a.length() < b.length() ? -1 : 1;
  648. }
  649. int CompareNoCaseAscii(const std::string &a, const std::string &b)
  650. {
  651. return CompareNoCaseAscii(std::string_view(a), std::string_view(b));
  652. }
  653. #if defined(MODPLUG_TRACKER)
  654. mpt::ustring ToLowerCase(const mpt::ustring &s)
  655. {
  656. #if defined(MPT_WITH_MFC)
  657. #if defined(UNICODE)
  658. CString tmp = mpt::ToCString(s);
  659. tmp.MakeLower();
  660. return mpt::ToUnicode(tmp);
  661. #else // !UNICODE
  662. CStringW tmp = mpt::ToWide(s).c_str();
  663. tmp.MakeLower();
  664. return mpt::ToUnicode(tmp.GetString());
  665. #endif // UNICODE
  666. #else // !MPT_WITH_MFC
  667. std::wstring ws = mpt::ToWide(s);
  668. std::transform(ws.begin(), ws.end(), ws.begin(), &std::towlower);
  669. return mpt::ToUnicode(ws);
  670. #endif // MPT_WITH_MFC
  671. }
  672. mpt::ustring ToUpperCase(const mpt::ustring &s)
  673. {
  674. #if defined(MPT_WITH_MFC)
  675. #if defined(UNICODE)
  676. CString tmp = mpt::ToCString(s);
  677. tmp.MakeUpper();
  678. return mpt::ToUnicode(tmp);
  679. #else // !UNICODE
  680. CStringW tmp = mpt::ToWide(s).c_str();
  681. tmp.MakeUpper();
  682. return mpt::ToUnicode(tmp.GetString());
  683. #endif // UNICODE
  684. #else // !MPT_WITH_MFC
  685. std::wstring ws = mpt::ToWide(s);
  686. std::transform(ws.begin(), ws.end(), ws.begin(), &std::towlower);
  687. return mpt::ToUnicode(ws);
  688. #endif // MPT_WITH_MFC
  689. }
  690. #endif // MODPLUG_TRACKER
  691. } // namespace mpt
  692. OPENMPT_NAMESPACE_END