2013-11-23 21:34:54 +01:00
|
|
|
/* ScummVM - Graphic Adventure Engine
|
|
|
|
*
|
|
|
|
* ScummVM is the legal property of its developers, whose names
|
|
|
|
* are too numerous to list here. Please refer to the COPYRIGHT
|
|
|
|
* file distributed with this source distribution.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2014-02-18 02:34:18 +01:00
|
|
|
*
|
2013-11-23 21:34:54 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef COMMON_USTR_H
|
|
|
|
#define COMMON_USTR_H
|
|
|
|
|
|
|
|
#include "common/scummsys.h"
|
2019-10-19 17:38:26 +02:00
|
|
|
#include "common/str-enc.h"
|
2020-10-27 23:22:25 +01:00
|
|
|
#include "common/base-str.h"
|
2013-11-23 21:34:54 +01:00
|
|
|
|
|
|
|
namespace Common {
|
|
|
|
|
2020-07-08 23:30:36 +02:00
|
|
|
/**
|
|
|
|
* @defgroup common_ustr UTF-32 strings
|
|
|
|
* @ingroup common_str
|
|
|
|
*
|
|
|
|
* @brief API for working with UTF-32 strings.
|
|
|
|
*
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2018-06-18 00:04:03 +02:00
|
|
|
class String;
|
|
|
|
|
2013-11-23 21:34:54 +01:00
|
|
|
/**
|
|
|
|
* Very simple string class for UTF-32 strings in ScummVM. The main intention
|
|
|
|
* behind this class is to feature a simple way of displaying UTF-32 strings
|
|
|
|
* through the Graphics::Font API.
|
|
|
|
*
|
|
|
|
* Please note that operations like equals, deleteCharacter, toUppercase, etc.
|
|
|
|
* are only very simplified convenience operations. They might not fully work
|
|
|
|
* as you would expect for a proper UTF-32 string class.
|
|
|
|
*
|
|
|
|
* The presence of \0 characters in the string will cause undefined
|
|
|
|
* behavior in some operations.
|
|
|
|
*/
|
2020-10-27 23:22:25 +01:00
|
|
|
#ifdef USE_CXX11
|
|
|
|
typedef char32_t u32char_type_t;
|
|
|
|
#else
|
|
|
|
typedef uint32 u32char_type_t;
|
|
|
|
#endif
|
2013-11-23 21:34:54 +01:00
|
|
|
|
2020-10-27 23:22:25 +01:00
|
|
|
class U32String : public BaseString<u32char_type_t> {
|
|
|
|
public:
|
2013-11-23 21:34:54 +01:00
|
|
|
typedef uint32 unsigned_type;
|
|
|
|
public:
|
2013-11-23 21:34:54 +01:00
|
|
|
/** Construct a new empty string. */
|
2020-10-31 16:56:00 +00:00
|
|
|
U32String() : BaseString<u32char_type_t>() {}
|
2013-11-23 21:34:54 +01:00
|
|
|
|
2013-11-23 21:34:54 +01:00
|
|
|
/** Construct a new string from the given NULL-terminated C string. */
|
2020-10-31 16:56:00 +00:00
|
|
|
explicit U32String(const value_type *str) : BaseString<u32char_type_t>(str) {}
|
2013-11-23 21:34:54 +01:00
|
|
|
|
2013-11-23 21:34:54 +01:00
|
|
|
/** Construct a new string containing exactly len characters read from address str. */
|
2020-10-31 16:56:00 +00:00
|
|
|
U32String(const value_type *str, uint32 len) : BaseString<u32char_type_t>(str, len) {}
|
2020-10-27 23:22:25 +01:00
|
|
|
|
|
|
|
#ifdef USE_CXX11
|
2020-10-31 16:56:00 +00:00
|
|
|
explicit U32String(const uint32 *str) : BaseString<u32char_type_t>((const value_type *) str) {}
|
|
|
|
U32String(const uint32 *str, uint32 len) : BaseString<u32char_type_t>((const value_type *) str, len) {}
|
|
|
|
U32String(const uint32 *beginP, const uint32 *endP) : BaseString<u32char_type_t>((const value_type *) beginP, (const value_type *) endP) {}
|
2020-10-27 23:22:25 +01:00
|
|
|
#endif
|
2013-11-23 21:34:54 +01:00
|
|
|
|
2013-11-23 21:34:54 +01:00
|
|
|
/** Construct a new string containing the characters between beginP (including) and endP (excluding). */
|
2020-10-31 16:56:00 +00:00
|
|
|
U32String(const value_type *beginP, const value_type *endP) : BaseString<u32char_type_t>(beginP, endP) {}
|
2013-11-23 21:34:54 +01:00
|
|
|
|
2013-11-23 21:34:54 +01:00
|
|
|
/** Construct a copy of the given string. */
|
2020-10-31 16:56:00 +00:00
|
|
|
U32String(const U32String &str) : BaseString<u32char_type_t>(str) {}
|
2013-11-23 21:34:54 +01:00
|
|
|
|
2019-01-01 00:40:17 -08:00
|
|
|
/** Construct a new string from the given NULL-terminated C string. */
|
|
|
|
explicit U32String(const char *str);
|
|
|
|
|
|
|
|
/** Construct a new string containing exactly len characters read from address str. */
|
|
|
|
U32String(const char *str, uint32 len);
|
|
|
|
|
|
|
|
/** Construct a new string containing the characters between beginP (including) and endP (excluding). */
|
|
|
|
U32String(const char *beginP, const char *endP);
|
|
|
|
|
|
|
|
/** Construct a copy of the given string. */
|
|
|
|
U32String(const String &str);
|
|
|
|
|
2013-11-23 21:34:54 +01:00
|
|
|
U32String &operator=(const U32String &str);
|
2019-01-01 00:40:17 -08:00
|
|
|
U32String &operator=(const String &str);
|
|
|
|
U32String &operator=(const value_type *str);
|
|
|
|
U32String &operator=(const char *str);
|
2013-11-23 21:34:54 +01:00
|
|
|
U32String &operator+=(const U32String &str);
|
|
|
|
U32String &operator+=(value_type c);
|
2020-10-27 23:22:25 +01:00
|
|
|
using BaseString<value_type>::operator==;
|
|
|
|
using BaseString<value_type>::operator!=;
|
2019-01-01 00:40:17 -08:00
|
|
|
bool operator==(const String &x) const;
|
|
|
|
bool operator==(const char *x) const;
|
|
|
|
bool operator!=(const String &x) const;
|
|
|
|
bool operator!=(const char *x) const;
|
2013-11-23 21:34:54 +01:00
|
|
|
|
2020-11-05 23:10:52 +00:00
|
|
|
/** Python-like method **/
|
|
|
|
String encode(CodePage page = kUtf8) const;
|
2019-10-19 17:38:26 +02:00
|
|
|
|
2020-06-13 22:12:25 +05:30
|
|
|
/**
|
2020-06-22 22:05:11 +05:30
|
|
|
* Print formatted data into a U32String object.
|
2020-06-13 22:12:25 +05:30
|
|
|
*/
|
2020-06-22 22:05:11 +05:30
|
|
|
static U32String format(U32String fmt, ...);
|
2020-09-08 21:22:04 +01:00
|
|
|
static U32String format(const char *fmt, ...);
|
2020-06-13 22:12:25 +05:30
|
|
|
|
|
|
|
/**
|
2020-06-22 22:05:11 +05:30
|
|
|
* Print formatted data into a U32String object. It takes in the
|
|
|
|
* output by reference and works with iterators.
|
2020-06-13 22:12:25 +05:30
|
|
|
*/
|
2020-10-27 23:22:25 +01:00
|
|
|
static int vformat(const value_type *fmt, const value_type *fmtEnd, U32String &output, va_list args);
|
2020-06-13 22:12:25 +05:30
|
|
|
|
2020-06-24 17:40:09 +05:30
|
|
|
/**
|
|
|
|
* Helper function for vformat, convert an int to string
|
|
|
|
* minimal implementation, only for base 10
|
|
|
|
*/
|
|
|
|
static char* itoa(int num, char* str, int base);
|
|
|
|
|
2020-10-27 23:22:25 +01:00
|
|
|
using BaseString<value_type>::insertString;
|
|
|
|
void insertString(const char *s, uint32 p);
|
|
|
|
void insertString(const String &s, uint32 p);
|
|
|
|
|
2020-11-05 23:10:52 +00:00
|
|
|
/** Return a substring of this string */
|
2020-11-01 22:53:03 +01:00
|
|
|
U32String substr(size_t pos = 0, size_t len = npos) const;
|
|
|
|
|
2020-10-27 23:22:25 +01:00
|
|
|
const uint32 *u32_str() const {
|
|
|
|
return (const uint32 *) _str;
|
|
|
|
}
|
2013-11-23 21:34:54 +01:00
|
|
|
|
2020-10-27 23:22:25 +01:00
|
|
|
private:
|
2019-10-19 17:38:26 +02:00
|
|
|
void encodeUTF8(String &dst) const;
|
|
|
|
void encodeOneByte(String &dst, CodePage page) const;
|
2018-08-05 20:32:25 +03:00
|
|
|
};
|
|
|
|
|
2019-10-18 17:18:54 +02:00
|
|
|
U32String operator+(const U32String &x, const U32String &y);
|
2020-10-27 23:22:25 +01:00
|
|
|
U32String operator+(const U32String &x, U32String::value_type y);
|
2020-07-08 23:30:36 +02:00
|
|
|
|
|
|
|
/** @} */
|
|
|
|
|
2013-11-23 21:34:54 +01:00
|
|
|
} // End of namespace Common
|
|
|
|
|
|
|
|
#endif
|