Changeset 826 in dev


Ignore:
Timestamp:
Mar 18, 2012, 11:48:48 PM (12 years ago)
Author:
イグトランス (egtra)
Message:

jenga/src/common/String.cppの実装を整理。

Location:
branches/egtra/ab5.0/jenga
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/egtra/ab5.0/jenga/projects/jenga/stdafx.h

    r763 r826  
    11#pragma once
    22
     3#include <algorithm>
    34#include <map>
    45#include <string>
     
    2122#include <assert.h>
    2223
    23 //boost libraries
    2424#include <boost/foreach.hpp>
     25#include <boost/implicit_cast.hpp>
     26#include <boost/range/algorithm.hpp>
    2527#include <boost/serialization/serialization.hpp>
    2628#include <boost/serialization/nvp.hpp>
  • branches/egtra/ab5.0/jenga/src/common/String.cpp

    r763 r826  
    11#include "stdafx.h"
    2 #include <algorithm>
     2#include <boost/numeric/conversion/cast.hpp>
     3#include <boost/algorithm/string/replace.hpp>
    34
    4 bool Jenga::Common::IsExistString( const Jenga::Common::Strings &strings, const std::string &findStr )
     5using boost::numeric_cast;
     6using boost::implicit_cast;
     7
     8bool Jenga::Common::IsExistString(const Jenga::Common::Strings &strings, const std::string &findStr)
    59{
    6     return std::find( strings.begin(), strings.end(), findStr ) != strings.end();
     10    return boost::find(strings, findStr) != strings.end();
    711}
    812
    9 std::string& Jenga::Common::StringReplace( std::string& str, const std::string &sb, const std::string &sa )
     13std::string& Jenga::Common::StringReplace(std::string& str, const std::string &sb, const std::string &sa)
    1014{
    11     std::string::size_type n, nb = 0;
    12    
    13     while ((n = str.find(sb,nb)) != std::string::npos)
    14     {
    15         str.replace(n,sb.size(),sa);
    16         nb = n + sa.size();
    17     }
    18    
     15    boost::algorithm::replace_all(str, sb, sa);
    1916    return str;
    2017}
    2118
    22 std::string Jenga::Common::ToString( int n )
     19std::string Jenga::Common::ToString(int n)
    2320{
    24     char temp[255];
    25     wsprintf( temp, "%d", n );
    26     return temp;
     21    // VC++ 2010だとこのキャストが必要。
     22    return std::to_string(implicit_cast<long long>(n));
    2723}
    2824
    2925std::string Jenga::Common::ToString( const std::wstring &wstr )
    3026{
    31     int needSize =  WideCharToMultiByte(
    32         CP_THREAD_ACP,
     27    if (wstr.empty())
     28    {
     29        return std::string();
     30    }
     31    int srcSize = numeric_cast<int>(wstr.size());
     32    int needSize = WideCharToMultiByte(
     33        CP_ACP,
    3334        0,
    34         wstr.data(), static_cast<int>(wstr.size()),
    35         NULL, NULL,
    36         NULL, NULL );
     35        wstr.data(), srcSize,
     36        nullptr, 0,
     37        nullptr, nullptr);
     38    if (needSize <= 0)
     39    {
     40        throw std::runtime_error("WideCharToMultiByte error");
     41    }
    3742
    38     char *pstr = (char *)calloc( needSize, 1 );
    39     WideCharToMultiByte(
    40         CP_THREAD_ACP,
     43    std::string ret(needSize, '\0');
     44    int res = WideCharToMultiByte(
     45        CP_ACP,
    4146        0,
    42         wstr.data(), static_cast<int>(wstr.size()),
    43         pstr, needSize,
    44         NULL, NULL );
     47        wstr.data(), srcSize,
     48        &ret[0], needSize,
     49        nullptr, nullptr);
     50    if (res <= 0)
     51    {
     52        throw std::runtime_error("WideCharToMultiByte error");
     53    }
    4554
    46     std::string result(pstr, needSize);
    47 
    48     free( pstr );
    49 
    50     return result;
     55    return ret;
    5156}
    5257
    53 std::wstring Jenga::Common::ToWString( const std::string &str )
     58std::wstring Jenga::Common::ToWString(const std::string &str)
    5459{
    55     int size = MultiByteToWideChar(
     60    if (str.empty())
     61    {
     62        return std::wstring();
     63    }
     64    int srcSize = numeric_cast<int>(str.size());
     65    int needSize = MultiByteToWideChar(
    5666        CP_ACP,
    5767        0,
    58         str.data(), static_cast<int>(str.size()),
    59         NULL, 0 );
     68        str.data(), srcSize,
     69        nullptr, 0);
     70    if (needSize <= 0)
     71    {
     72        throw std::runtime_error("WideCharToMultiByte error");
     73    }
    6074
    61     LPWSTR pwstr = (LPWSTR)calloc( size, sizeof (wchar_t) );
    62 
    63     MultiByteToWideChar(
     75    std::wstring ret(needSize, L'\0');
     76    int res = MultiByteToWideChar(
    6477        CP_ACP,
    6578        0,
    66         str.data(), static_cast<int>(str.size()),
    67         pwstr, size );
     79        str.data(), srcSize,
     80        &ret[0], needSize);
     81    if (res <= 0)
     82    {
     83        throw std::runtime_error("WideCharToMultiByte error");
     84    }
    6885
    69     std::wstring wstr( pwstr, size );
    70 
    71     free( pwstr );
    72 
    73     return wstr;
     86    return ret;
    7487}
    7588
    7689bool Jenga::Common::IsIdentifierTopChar( char c )
    7790{
    78     return ( isalpha( c ) || c == '_' );
     91    return ('A' <= c && c <= 'Z')
     92        || ('a' <= c && c <= 'z')
     93        || c == '_' ;
    7994}
    8095
    8196bool Jenga::Common::IsIdentifierChar( char c )
    8297{
    83     return ( IsIdentifierTopChar( c ) || isdigit( c ) );
     98    return IsIdentifierTopChar(c)
     99        || ('0' <= c && c <= '9');
    84100}
Note: See TracChangeset for help on using the changeset viewer.