相当于C中的wstring

时间:2015-05-26 01:36:49

标签: c++ c wchar-t wstring

我如何使用标准C读取和访问Unicode字符。以前我使用C ++和std::wstring表示整个单词,'const wchar_t'表示单个字符,这完全有效(下面是示例代码)。 / p>

但现在我不允许使用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];
    }
}
...
...

2 个答案:

答案 0 :(得分:2)

  

如何更换&#39; wstring&#39;在C?如何将我的代码转换为标准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'\0';
    }
    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'\0';
    }
    else
    {
        s_temp[s_temp_len] = strIn[i];
        s_temp_len += 1;
        s_temp[s_temp_len] = L'\0';
    }
}
// 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'\0';
}

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);
...

答案 1 :(得分:0)

等效的C例程是

==                   wcscmp or wcsncmp 
+=                   wcscat or wcscat_s
=                    wcscpy or wcsncpy or wcscpy_s
.size() or length()  wcslen

在您的情况下,因为您一次比较一个字符,所以您不需要wcscmp。确保所有字符串都为空终止,否则非_s版本将无效。

相关问题