From a3ff3da58c77ce1faf2f33063ddec01998cb3f7b Mon Sep 17 00:00:00 2001 From: Alvaro Date: Fri, 8 Mar 2024 00:09:58 +0100 Subject: [PATCH] Console::setCP can set ANSI locale (if ANSI mode), doc fixes, internal String improvements --- include/asl/Array.h | 8 ++++-- include/asl/Array_.h | 5 ++-- include/asl/Console.h | 7 +++-- include/asl/File.h | 10 ++++--- include/asl/Http.h | 4 +-- include/asl/String.h | 2 +- include/asl/Var.h | 4 +++ src/Console.cpp | 11 ++++++-- src/File.cpp | 4 +-- src/Http.cpp | 2 +- src/String.cpp | 65 +++++++++++++------------------------------ 11 files changed, 57 insertions(+), 65 deletions(-) diff --git a/include/asl/Array.h b/include/asl/Array.h index 0f31e08..e2ff798 100644 --- a/include/asl/Array.h +++ b/include/asl/Array.h @@ -174,16 +174,18 @@ class Array Removes all elements in the array */ void clear() { resize(0); } - /* Frees all elements (with delete) and clears the array; elements must be pointers */ + /* Frees all elements (with delete) and clears the array; elements must be pointers + \deprecated Delete items explicitly or use smart pointers + */ ASL_DEPRECATED(void destroy(), "") { for(int i=0; i #include +#ifdef _WIN32 +#include +#endif namespace asl { @@ -45,9 +48,13 @@ Console::~Console() } } -void Console::setCP(int cp) +void Console::setCP(int cp, bool loc) { SetConsoleOutputCP(cp == 0 ? _cp0 : cp < 0 ? GetACP() : cp); +#ifdef ASL_ANSI + if (loc) + setlocale(LC_CTYPE, ".ACP"); +#endif } void Console::gotoxy(int x, int y) @@ -174,7 +181,7 @@ Console::~Console() color(); } -void Console::setCP(int cp) {} +void Console::setCP(int cp, bool loc) {} void Console::gotoxy(int x, int y) { diff --git a/src/File.cpp b/src/File.cpp index 2c3d3e0..ba37643 100644 --- a/src/File.cpp +++ b/src/File.cpp @@ -280,7 +280,7 @@ bool File::put(const Array& data) { if(!_file && !open(_path, WRITE)) return false; - return write(data.ptr(), data.length()) == data.length(); + return write(data.data(), data.length()) == data.length(); } Array File::firstBytes(int n) @@ -321,7 +321,7 @@ File File::temp(const String& ext) unsigned pid = (unsigned)getpid(); #endif do { - file._path = String(0, "%s/%04x%08x%08x%s", *tmpDir, pid, p, num++, *ext); + file._path = String::f("%s/%04x%08x%08x%s", *tmpDir, pid, p, num++, *ext); } while (file.exists()); file.open(File::WRITE); return file; diff --git a/src/Http.cpp b/src/Http.cpp index 7c053ce..ad0b1b7 100644 --- a/src/Http.cpp +++ b/src/Http.cpp @@ -582,7 +582,7 @@ bool HttpMessage::write() if (_fileBody) return putFile(_body); else - return write((const char*)_body.ptr(), _body.length()) > 0; + return write((const char*)_body.data(), _body.length()) > 0; } void HttpMessage::write(const String& text) diff --git a/src/String.cpp b/src/String.cpp index 48d6f2d..fba9a7b 100644 --- a/src/String.cpp +++ b/src/String.cpp @@ -64,11 +64,15 @@ int local8toUtf16(const char* u, wchar_t* p, int n) #endif } -int local8toUtf32(const char* u, int* p, int) +int local8toUtf32(const char* u, int* p, int n) { int* p0 = p; while (int c = *u++) + { *p++ = c; + if (--n == 0) + break; + } *p = 0; return int(p - p0); } @@ -102,7 +106,7 @@ int utf32toUtf8(const int* p, char* u, int n) return int(u - u0); } -int utf8toUtf32(const char* u, int* p, int) +int utf8toUtf32(const char* u, int* p, int n) { int* p0 = p; while (int c = *u++) @@ -131,12 +135,14 @@ int utf8toUtf32(const char* u, int* p, int) if (c4 == 0) break; *p++ = ((c & 0x07) << 18) | ((c2 & 0x3f) << 12) | ((c3 & 0x3f) << 6) | (c4 & 0x3f); } + if (--n == 0) + break; } *p = 0; return int(p - p0); } -int utf16toUtf8(const wchar_t* p, char* u, int) +int utf16toUtf8(const wchar_t* p, char* u, int n) { char* u0 = u; while(wchar_t c = *p++) { @@ -162,13 +168,16 @@ int utf16toUtf8(const wchar_t* p, char* u, int) *u++ = ((d >> 6 & 0x3F) | 0x80); *u++ = ((d & 0x3F) | 0x80); } - else break; + else + break; + if (--n == 0) + break; } *u='\0'; return int(u - u0); } -int utf8toUtf16(const char* u, wchar_t* p, int) +int utf8toUtf16(const char* u, wchar_t* p, int n) { wchar_t* p0 = p; while(int c = *u++) @@ -199,7 +208,10 @@ int utf8toUtf16(const char* u, wchar_t* p, int) *p++ = (d >> 10) + 0xd800; *p++ = (d & 0x3ff) + 0xdc00; } - else break; + else + break; + if (--n == 0) + break; } *p=L'\0'; return int(p - p0); @@ -518,9 +530,8 @@ bool String::isTrue() const Array String::chars() const { Array c(length() + 1); - int n = utf8toUtf32(str(), c.data(), 1); - c.resize(n); - return c; + int n = utf8toUtf32(str(), c.data(), length()); + return c.resize(n); } void String::assign(const char* b, int n) @@ -869,42 +880,6 @@ Long myatol(const char* s) return y*sgn; } -/* -double myatof(const char* s) -{ -double y = 0; -double m = 1; -int exp = 0; -if (s[0] == '-') { m = -1; s++; } -const char* p = strchr(s, '.'); -if(p) { -p++; -while (*p != '\0' && *p!='e' && *p!='E') { -exp--; -p++; -} -p--; -} -if (!p) p = s; -while(*p++) -if(*p == 'e' || *p == 'E'){ -if(*(p+1) == '+') -p++; -exp += myatoiz(p+1); -break; -} -while(int c=*s++) -{ -if(c=='.') continue; -if(c=='E' || c=='e') break; -y = 10.0*y + (c-'0'); -} -if(exp != 0) -y *= pow(10.0, exp); -return y * m; -} -*/ - double myatof(const char* s) { double y = 0;