我有以下代碼。Unicode支持isdigit和isspace功能
// mfc.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "mfc.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
#include <cctype>
#include <string>
#include <sstream>
#include <tchar.h>
#include <iostream>
#include <Strsafe.h>
#include <algorithm>
#include <cmath>
#include <limits>
#include <functional>
#include <cassert>
std::wstring toStringWithoutNumerical(const std::wstring& str) {
std::wstring result;
bool alreadyAppendSpace = false;
for (int i = 0, length = str.length(); i < length; i++) {
const TCHAR c = str.at(i);
if (isdigit(c)) {
continue;
}
if (isspace(c)) {
if (false == alreadyAppendSpace) {
result.append(1, c);
alreadyAppendSpace = true;
}
continue;
}
result.append(1, c);
alreadyAppendSpace = false;
}
return result;
}
// The one and only application object
CWinApp theApp;
using namespace std;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
_tprintf(_T("Fatal Error: MFC initialization failed\n"));
nRetCode = 1;
}
else
{
// TODO: code your application's behavior here.
}
std::wstring me = toStringWithoutNumerical(_T("My Leg 1 Long"));
AfxMessageBox(me.c_str());
// Crash!
std::wstring he = toStringWithoutNumerical(L"我的腳1盤");
AfxMessageBox(he.c_str());
return nRetCode;
}
對於第一個消息框,
我的腿長
將被顯示。
對於第二個消息框,碰撞會發生,與斷言失敗在isctype.c
_ASSERTE((unsigned)(c + 1) <= 256);
我怎樣才能得到一個標準功能(ISDIGIT,isspace爲...),以支持Unicode出256範圍?
只有文檔說'在程序的當前語言環境中',即使是'iswdigit()'和'iswspace()'也是如此。我是否無視這句話?或者是因爲少數Unicode字符對幾個區域設置的反應不同? (即土耳其我到我和我到我)。 – 2014-06-15 20:43:22