cpputil/include/ustring.h

85 lines
2.9 KiB
C++
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#ifndef USTRING_H
#define USTRING_H
#include "utf.h"
#include <iostream>
#include <cstdint>
/**
* Класс строк, работающий с некоторыми кодировками юникода (UTF-8, UTF-16, UTF-32)
* Хранит данные внутри себя в UCS-4 и инкапсулирует в себе кодирование/декодирование
* указанных выше кодировок.
*/
class ustring
{
private:
std::size_t _len;
char32_t* _pData;
private:
mutable std::size_t _capacity;
mutable char* _pUtf8;
mutable char16_t* _pUtf16;
mutable char32_t* _pUtf32;
private:
inline void clear_internal_buffers() const;
inline void init_from_utf8_str(const char* str, bool needUnescape);
inline void init_from_utf16_str(const char16_t* str, byte_order byteOrder = BYTE_ORDER_LITTLE_ENDIAN);
inline void init_from_utf32_str(const char32_t* str, byte_order byteOrder = BYTE_ORDER_LITTLE_ENDIAN);
public:
static const std::size_t npos = -1;
public:
enum encoding { UTF8, UTF16LE, UTF16BE, UTF32LE, UTF32BE };
public:
ustring();
ustring(const ustring& str);
ustring(ustring&& str);
ustring(const char* str, bool needUnescape = false);
ustring(const char16_t* str, byte_order byteOrder = BYTE_ORDER_LITTLE_ENDIAN);
ustring(const char32_t* str, byte_order byteOrder = BYTE_ORDER_LITTLE_ENDIAN);
ustring(const std::string& str, bool needUnescape = false);
ustring(const std::wstring& str);
explicit ustring(std::size_t n);
operator std::string() const;
operator std::wstring() const;
const char* utf8_str(bool addBom = false, bool needEscape = false) const;
const char16_t* utf16_str(bool addBom = false, byte_order byteOrder = BYTE_ORDER_LITTLE_ENDIAN) const;
const char32_t* utf32_str(bool addBom = false, byte_order byteOrder = BYTE_ORDER_LITTLE_ENDIAN) const;
std::size_t length() const;
std::size_t load_from_file(const ustring& fileName, encoding enc);
std::size_t store_to_file(const ustring& fileName, encoding enc) const;
ustring& operator=(const ustring& str);
ustring& operator=(ustring&& str);
ustring& operator+=(const ustring& str);
ustring& operator+(const ustring& str);
bool operator==(const ustring& str) const;
bool operator!=(const ustring& str) const;
char32_t& operator[](std::size_t index);
ustring& assign(const char32_t* str, std::size_t n);
ustring& erase(std::size_t pos = 0, std::size_t len = npos);
ustring& insert(std::size_t pos, const ustring& str);
ustring& replace(const ustring& what, const ustring& by);
void clear();
bool empty() const;
std::size_t find(char32_t symbol, std::size_t pos = 0) const;
std::size_t find(const ustring& str, std::size_t pos = 0) const;
ustring substr(std::size_t pos = 0, std::size_t len = npos) const;
friend std::ostream& operator<<(std::ostream& os, const ustring& ustr);
friend std::istream& operator>>(std::istream& is, ustring& ustr);
~ustring();
};
#endif // USTRING_H