相当于C中的wstring
Equivalent of wstring in C
我如何使用标准C读取和访问Unicode字符。以前我使用c++和std::wstring
作为整个单词,使用'const wchar_t'作为单个字符,这工作得很好(下面是示例代码)。
但是现在我不允许使用c++。我如何在C中替换'wstring' ?如何将我的代码转换为标准C语言?
...
...
const wchar_t small_char[10]={ L'锕',L'吖',L'啊',L'阿',L'呵',L'嗄',L'埃',L'哀',L'哎'};
std::wstring strIn=L"锕吖哎";
std::wstring s_temp=L"";
int length= strIn.length();
for(int i=0;i<length;i++){
if(strIn[i]==small_char[2]){
s_temp=s_temp+L"ba";
}
else if(strIn[i]==small_char[5]){
s_temp=s_temp+L"pe";
}
else{
s_temp=s_temp+strIn[i];
}
}
...
...
我如何在C中替换'wstring' ?如何将我的代码转换为标准C语言?
std::wstring
只是wchar_t*
的包装。您可以直接使用wchar_t
,您只需要手动管理字符串内存和连接。
试试这个:
...
const wchar_t small_char[10] = { L'锕', L'吖', L'啊', L'阿', L'呵', L'嗄', L'埃', L'哀', L'哎'};
wchar_t *strIn = L"锕吖哎";
int length = wcslen(strIn);
wchar_t *s_temp = (wchar_t*) calloc((length*2)+1, sizeof(wchar_t));
int s_temp_len = 0;
for(int i = 0; i < length; i++)
{
if (strIn[i] == small_char[2])
{
memcpy(&s_temp[s_temp_len], L"ba", 2*sizeof(wchar_t));
s_temp_len += 2;
s_temp[s_temp_len] = L' ';
}
else if (strIn[i] == small_char[5])
{
memcpy(&s_temp[s_temp_len], L"pe", 2*sizeof(wchar_t));
s_temp_len += 2;
s_temp[s_temp_len] = L' ';
}
else
{
s_temp[s_temp_len] = strIn[i];
s_temp_len += 1;
s_temp[s_temp_len] = L' ';
}
}
// use s_temp up to s_temp_len characters as needed...
free(s_temp);
...
如果您想要更像std::wstring
的东西,您应该预先分配一个小缓冲区,并在连接期间超过其容量时调整它的大小。struct
对于跟踪这一点很有用:
struct my_wstring
{
wchar_t *data;
int length;
int capacity;
};
void wstring_init(struct my_wstring *str)
{
str->data = NULL;
str->length = 0;
str->capacity = 0;
};
void wstring_clear(struct my_wstring *str)
{
free(str->data);
str->data = NULL;
str->length = 0;
str->capacity = 0;
};
// allocate in multiples of 32
const int delta = 32;
void wstring_append_str_len(struct my_wstring *str, const wchar_t *value, int valueLen)
{
if ((!str) || (!value) || (valueLen < 1)) return;
int newLen = str->length + valueLen;
if ((newLen + 1) > str->capacity)
{
// round to next highest multiple of 32
int newCap = ((newLen + 1) + (delta - 1)) & ~delta;
wchar_t *newData = (wchar_t*) realloc(str->data, newCap * sizeof(wchar_t));
if (!newData)
{
// memory allocation error, do something!
return;
}
str->data = newData;
str->capacity = newCap;
}
memcpy(&(str->data[str->length]), value, valueLen * sizeof(wchar_t));
str->length = newLen;
str->data[newLen] = L' ';
}
void wstring_append_str(struct wstring *str, const wchar_t *value)
{
wstring_append_str_len(str, value, wcslen(value));
}
void wstring_append_chr(struct wstring *str, const wchar_t value)
{
wstring_append_str_len(str, &value, 1);
}
...
const wchar_t small_char[10] = { L'锕', L'吖', L'啊', L'阿', L'呵', L'嗄', L'埃', L'哀', L'哎'};
wchar_t *strIn = L"锕吖哎";
struct my_wstring s_temp;
wstring_init(&s_temp);
int length = wcslen(strIn);
for(int i = 0; i < length; i++)
{
if (strIn[i] == small_char[2])
{
wstring_append_str(&s_temp, L"ba");
}
else if (strIn[i] == small_char[5])
{
wstring_append_str(&s_temp, L"pe");
}
else
{
wstring_append_chr(&s_temp, strIn[i]);
}
}
// use s_temp.data up to s_temp.length characters as needed...
wstring_clear(&s_temp);
...
等效的C例程为
== wcscmp or wcsncmp
+= wcscat or wcscat_s
= wcscpy or wcsncpy or wcscpy_s
.size() or length() wcslen
在您的示例中,由于每次比较一个字符,因此不需要wcscmp。确保所有的字符串都以null结尾,否则非_s版本将无法工作。
相关文章:
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- .cpp和.h文件中的模板专用化声明
- 找不到 std::wstring 中存在的wchar_t
- 在 C++ 中将 utf8 wstring 转换为窗口中的字符串
- 使用 ReadProcessMemory() 将未知大小的字符数组或字符串读入 wstring 中
- 如何将WSTRING中的Unix时间戳转换为char数组中格式的日期
- 如何写入 wstring 数组中的特定位置
- 如何通过 std::unordered_map<std::wstring, T> 中的 std::wstring_view 类型的键查找?
- Windows中的exe在调用getWindowsDirectory时附加wstring时正在崩溃
- C++:为什么在 #include 之前调用 std::wstring::begin() 会导致<vector>此代码中的编译器错误?
- std::wstring中ASCII符号的字节表示,具有不同的语言环境
- 字符集在字符串和 wstring 中的存储方式
- 如何使用正则表达式删除std::wstring中特定短语的所有实例
- wstring.size()在xcode或visual c++中的工作方式不同
- 在Boost Python中使用一个接受std::wstring参数的c++函数来使用Unicode
- c++ - std::wstring到std::string的快速转换,用作std::map中的键
- 如何将C++dll中的unicode字符串/wstring/CStringW返回到InstallScript
- 运行存储在“std::wstring”中的控制台命令
- 根据MSVC++中的unicode设置,在std::string和std::wstring之间自动切换
- 相当于C中的wstring