Changeset 1285 for cpp/common


Ignore:
Timestamp:
12/06/23 03:27:30 (12 months ago)
Author:
Maciej Komosinski
Message:

Added toLowerCase/toUpperCase for UTF-8 strings

Location:
cpp/common
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • cpp/common/Convert.cpp

    r1274 r1285  
    11// This file is a part of Framsticks SDK.  http://www.framsticks.com/
    2 // Copyright (C) 1999-2021  Maciej Komosinski and Szymon Ulatowski.
     2// Copyright (C) 1999-2023  Maciej Komosinski and Szymon Ulatowski.
    33// See LICENSE.txt for details.
    44
     
    1818
    1919#include <stdio.h>
    20 
     20#include <locale>
     21#include <iostream>
     22#ifndef IPHONE
     23#include <codecvt>
     24#endif
    2125
    2226int Convert::toInt(string s) { return atoi(s.c_str()); }
     27int Convert::toInt_HexIf0x(string s) { return (s.size() > 2 && s[0] == '0' && s[1] == 'x') ? (int)hexToInt(s.substr(2)) : atoi(s.c_str()); }
    2328float Convert::toFloat(string s) { return (float)atof(s.c_str()); }
    2429string Convert::toLowerCase(string s) { std::transform(s.begin(), s.end(), s.begin(), ::tolower);  return s; }
    2530string Convert::toUpperCase(string s) { std::transform(s.begin(), s.end(), s.begin(), ::toupper);  return s; }
     31#if !defined IPHONE && !defined MACOS
     32static string transformUTF8(string in, wint_t(*fun)(wint_t))
     33{
     34        static std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t> converter;
     35        auto wstr = converter.from_bytes(in);
     36        for (auto it = wstr.begin(); it != wstr.end(); it++)
     37                *it = fun(*it);
     38        return converter.to_bytes(wstr);
     39}
     40//does not seem to work in iPhone e.g. for Polish-specific letters, or a different locale settings should be used (but pl-PL did not work properly either) - so instead implemented this using NSString function in objc-interface.mm
     41string Convert::toLowerCaseUTF8(string s) { return transformUTF8(s, towlower); }
     42string Convert::toUpperCaseUTF8(string s) { return transformUTF8(s, towupper); }
     43#endif
    2644char Convert::toLowerCase(char c) { return (char)tolower(c); }
    2745char Convert::toUpperCase(char c) { return (char)toupper(c); }
  • cpp/common/Convert.h

    r1028 r1285  
    2222public:
    2323        static int toInt(string s);
     24        static int toInt_HexIf0x(string s);
    2425        static float toFloat(string s);
    2526        static string toLowerCase(string s);
    2627        static string toUpperCase(string s);
     28        static string toLowerCaseUTF8(string s);
     29        static string toUpperCaseUTF8(string s);
    2730        static char toLowerCase(char c);
    2831        static char toUpperCase(char c);
Note: See TracChangeset for help on using the changeset viewer.