ultimatepp/uppsrc/Core/StrUtil.cpp
Mirek Fidler 34ff691308 sizeof(wchar) is changed to 4 (32 bits) to support non BMP unicode characters
This might bring some incompatibilities in the code that expects wchar to be 16 bit, which
  escpecially involves dealing with Win32 (and to lesser extend MacOS) APIs, so if your application
  is doing that, please check all instances of WCHAR (UniChar on MacOS) or even wchar
  especially type casts.

  To support host APIs, char16 is introduced (but there is no 16-bit String varian).

  Use ToSystemCharsetW, FromSystemCharsetW to convert texts to Win32 API.

- Support of drawing non-BMP characters in GUI
- Vastly improved character font replacement code (when drawing characters missing with requested font, replacement font is used)
- Last instances of Win32 ANSI calls (those ending with A) are removed
- UTF handling routines are refactored and their's naming is unified
- RTF is now being able to handle non-BMP characters (RTF is used as clipboard format for RichText)

Other minor changes:

- fixed TryRealloc issue
- improved MemoryCheck
- Removed MemoryAlloc48/MemoryFree48
- In theide Background parsing should less often cause delays in the main thread
2021-12-02 12:03:19 +01:00

162 lines
2.3 KiB
C++

#include "Core.h"
namespace Upp {
int strlen16(const char16 *s)
{
if(!s) return 0;
const char16 *s0 = s;
while(*s) s++;
return int(s - s0);
}
int strlen32(const wchar *s)
{
if(!s) return 0;
const wchar *s0 = s;
while(*s) s++;
return int(s - s0);
}
unsigned ctoi(int c)
{
if(c >= '0' && c <= '9')
return c - '0';
if(c >= 'A' && c <= 'Z')
return c - 'A' + 10;
if(c >= 'a' && c <= 'z')
return c - 'a' + 10;
return (unsigned)-1;
}
int CharFilterAscii(int c)
{
return c >= 32 && c < 256 ? c : 0;
}
int CharFilterAscii128(int c)
{
return c >= 32 && c < 128 ? c : 0;
}
int CharFilterUnicode(int c)
{
return c >= 32 && c < 0x10FFFF ? c : 0;
}
int CharFilterDigit(int c)
{
return IsDigit(c) ? c : 0;
}
int CharFilterInt(int c)
{
if(c == '+' || c == '-') return c;
return CharFilterDigit(c);
}
int CharFilterDouble(int c)
{
if(c == ',' || c == '.') return '.';
if(c == 'e' || c == 'E') return 'E';
return CharFilterInt(c);
}
int CharFilterWhitespace(int c)
{
return IsSpace(c) ? c : 0;
}
int CharFilterNotWhitespace(int c)
{
return IsSpace(c) ? 0 : c;
}
int CharFilterAlpha(int c)
{
return IsAlpha(c) ? c : 0;
}
int CharFilterToUpper(int c)
{
return ToUpper(c);
}
int CharFilterToLower(int c)
{
return ToLower(c);
}
int CharFilterToUpperAscii(int c)
{
return ToUpperAscii(c);
}
int CharFilterAlphaToUpper(int c)
{
return IsAlpha(c) ? IsUpper(c) ? c : ToUpper(c) : 0;
}
int CharFilterAlphaToLower(int c)
{
return IsAlpha(c) ? IsLower(c) ? c : ToLower(c) : 0;
}
int CharFilterDefaultToUpperAscii(int c)
{
return ToUpper(ToAscii(c, CHARSET_DEFAULT));
}
int CharFilterCrLf(int c)
{
return c == '\r' || c == '\n' ? c : 0;
}
int CharFilterNoCrLf(int c)
{
return c != '\r' && c != '\n' ? c : 0;
}
String Filter(const char *s, int (*filter)(int))
{
String result;
while(*s) {
int c = (*filter)((byte)*s++);
if(c) result.Cat(c);
}
return result;
}
String FilterWhile(const char *s, int (*filter)(int))
{
String result;
while(*s) {
int c = (*filter)((byte)*s++);
if(!c) break;
result.Cat(c);
}
return result;
}
WString Filter(const wchar *s, int (*filter)(int))
{
WString result;
while(*s) {
int c = (*filter)(*s++);
if(c) result.Cat(c);
}
return result;
}
WString FilterWhile(const wchar *s, int (*filter)(int))
{
WString result;
while(*s) {
int c = (*filter)(*s++);
if(!c) break;
result.Cat(c);
}
return result;
}
}