tags:

views:

28

answers:

1

I have the following code.

// 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;
}

For 1st message box,

My Leg Long

will be shown.

For 2nd message box, crash will happen, with assertion fail at isctype.c

_ASSERTE((unsigned)(c + 1) <= 256);

How I can get a standard function (isdigit, isspace...), to support unicode out of 256 range?