我正在使用 Linux 系统,我认为标准 Linuxstd::string
支持 Unicode 和 ASCII 字符。所以,我想std::string
在我的代码中使用,但我从应用程序接收格式为std::basic_string<unsigned short int, TRAIT_CLASS>
(支持 Windows 和 Linux)的字符串。TRAITS_CLASS
如下:
class TRAITS_CLASS
{
public:
typedef unsigned short char_type;
typedef unsigned short int_type;
typedef size_t pos_type;
typedef size_t off_type;
typedef int state_type;
static inline void assign(unsigned short &dest, const unsigned short &src)
{
dest = src;
}
static inline bool eq(const unsigned short &left, const unsigned short &right)
{
return left == right;
}
static inline bool lt(const unsigned short &left, const unsigned short &right)
{
return left < right;
}
static int compare(const unsigned short *p1, const unsigned short *p2, size_t count)
{
for (; 0 < count; --count, ++p1, ++p2)
{
if (!eq(*p1, *p2))
{
return lt(*p1, *p2) ? -1 : 1;
}
}
return 0;
}
static size_t length(const unsigned short *p)
{
size_t count = 0;
while (*p++)
{
++count;
}
return count;
}
static unsigned short* copy(unsigned short *p1, const unsigned short *p2, size_t count)
{
unsigned short *res = p1;
for (; 0 < count; --count, ++p1, ++p2)
{
assign(*p1, *p2);
}
return res;
}
static const unsigned short* find(const unsigned short *p, size_t count,
const unsigned short &value)
{
for (; 0 < count; --count, ++p)
{
if (eq(*p, value))
{
return p;
}
}
return 0;
}
static unsigned short* move(unsigned short *dest, const unsigned short *src, size_t count)
{
unsigned short *res = dest;
if ((src < dest) && (dest < src + count))
{
for (dest += count, src += count; 0 < count; --count)
{
assign(*--dest, *--src);
}
}
else
{
for (; 0 < count; --count, ++dest, ++src)
{
assign(*dest, *src);
}
}
return res;
}
static unsigned short* assign(unsigned short *dest, size_t count, unsigned short value)
{
unsigned short *res = dest;
for (; 0 < count; --count, ++dest)
{
assign(*dest, value);
}
return res;
}
static inline unsigned short to_char_type(const int_type &arg)
{
return static_cast<unsigned short>(arg);
}
static inline int_type to_int_type(const unsigned short &value)
{
return static_cast<int_type>(value);
}
static inline bool eq_int_type(const int_type &left, const int_type &right)
{
return left == right;
}
static inline int_type eof()
{
return static_cast<int_type>(EOF);
}
static inline int_type not_eof(const int_type &value)
{
return value != eof() ? value : 1;
}
};
如何std::string
为上述std::basic_string
模板分配法线?喜欢:
basic_string<unsigned short int, TRAIT_ClASS> temp = u"string";
如果无法分配,我该如何使用上述basic_string
模板?