bool TParserBase::RecodeToUtf8(Stroka& text)
{
    if (Encoding == CODES_UNKNOWN) {
        if (!IsStringASCII(~text, ~text + +text)) {
            AddError(Substitute("Unknown encoding: \"$0\".", text));
            return false;
        }

    } else if (Encoding == CODES_UTF8) {
        if (!IsUtf(text)) {
            AddError(Substitute("Invalid utf8: \"$0\".", text));
            return false;
        }

    } else {
        try {
            CharToWide(text, RecodeBuffer, Encoding);
            WideToUTF8(RecodeBuffer, text);
        } catch (yexception&) {
            AddError(Substitute("Cannot recode from $0 to utf8: \"$1\".", NameByCharset(Encoding), text));
            return false;
        }
    }

    return true;
}
Exemple #2
0
std::string CommandLine::GetSwitchValueASCII(
    const std::string& switch_string) const
{
    StringType value = GetSwitchValueNative(switch_string);
    if(!IsStringASCII(value))
    {
        LOG(WARNING) << "Value of --" << switch_string << " must be ASCII.";
        return "";
    }
    return WideToASCII(value);
}
Exemple #3
0
    bool DictionaryValue::GetStringASCII(const std::string& path,
        std::string* out_value) const
    {
        std::string out;
        if(!GetString(path, &out))
        {
            return false;
        }

        if(!IsStringASCII(out))
        {
            NOTREACHED();
            return false;
        }

        out_value->assign(out);
        return true;
    }
std::string UTF16ToASCII(const string16& utf16)
{
    DCHECK(IsStringASCII(utf16)) << utf16;
    return std::string(utf16.begin(), utf16.end());
}
std::string WideToASCII(const std::wstring& wide)
{
    DCHECK(IsStringASCII(wide)) << wide;
    return std::string(wide.begin(), wide.end());
}