Skip to content

Commit

Permalink
Console::setCP can set ANSI locale (if ANSI mode), doc fixes, interna…
Browse files Browse the repository at this point in the history
…l String improvements
  • Loading branch information
aslze committed Mar 7, 2024
1 parent cb4e31c commit a3ff3da
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 65 deletions.
8 changes: 5 additions & 3 deletions include/asl/Array.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<length(); i++) delete _a[i]; clear(); }
/**
Returns a pointer to the base of the array
\deprecated this conversion will not be implicit! Use .data()
\deprecated This conversion will not be implicit! Use .data()
*/
ASL_DEPRECATED(operator const T*() const, "Use .data()") { return &_a[0]; }
/**
Returns a pointer to the base of the array
\deprecated this conversion will not be implicit! Use .data()
\deprecated This conversion will not be implicit! Use .data()
*/
ASL_DEPRECATED(operator T*(), "Use .data()") { return &_a[0]; }
/**
Expand Down
5 changes: 3 additions & 2 deletions include/asl/Array_.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright(c) 1999-2023 aslze
// Copyright(c) 1999-2024 aslze
// Licensed under the MIT License (http://opensource.org/licenses/MIT)

#ifndef ASL_ARRAYX_H
Expand Down Expand Up @@ -192,8 +192,9 @@ class Array_
/**
Returns a string representation of the array, formed by joining its
elements with commas. The elements need to be convertible to String
\deprecated Use .join()
*/
ASL_DEPRECATED(operator String() const, "Use join()");
ASL_DEPRECATED(operator String() const, "Use .join()");
/**
Returns a string representation of the array, formed by joining its
elements with the given separator string sep. The elements need to be
Expand Down
7 changes: 4 additions & 3 deletions include/asl/Console.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright(c) 1999-2023 aslze
// Copyright(c) 1999-2024 aslze
// Licensed under the MIT License (http://opensource.org/licenses/MIT)

#ifndef ASL_CONSOLE_H
Expand Down Expand Up @@ -77,9 +77,10 @@ class ASL_API Console
~Console();

/**
On Windows, sets the console output code page (if 0 given, uses the system code page, if -1 uses the default console page)
On Windows, sets the console output code page (if 0 given, uses the system code page, if -1 uses the default console page),
and if loc is true sets the locale for string case management to the system locale. (this behavior might change in the future)
*/
void setCP(int cp = 0);
void setCP(int cp = 0, bool loc = false);

/**
Sets the cursor position to coordinates `x`, `y`
Expand Down
10 changes: 6 additions & 4 deletions include/asl/File.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright(c) 1999-2023 aslze
// Copyright(c) 1999-2024 aslze
// Licensed under the MIT License (http://opensource.org/licenses/MIT)

#ifndef ASL_FILE_H
Expand Down Expand Up @@ -152,14 +152,16 @@ class ASL_API File
*/
String directory() const;
/**
Returns true if the 'file' is a directory
Returns true if the 'file' path is a directory
*/
bool isDirectory() const;
/**
Returns true if the file exists
Returns true if the 'file' path exists (file or directory)
*/
bool exists() const { _info.clear(); return creationDate().time() != 0; }

/**
Returns true if the 'file' path exists and is actually a file
*/
bool isFile() const { return creationDate().time() != 0 && !isDirectory(); }

/**
Expand Down
4 changes: 2 additions & 2 deletions include/asl/Http.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,9 @@ class ASL_API HttpMessage
Var json() const;

/**
\deprecated Use json()
\deprecated Use .json()
*/
ASL_DEPRECATED(Var data() const, "Use json()") { return json(); }
ASL_DEPRECATED(Var data() const, "Use .json()") { return json(); }

bool write();
/**
Expand Down
2 changes: 1 addition & 1 deletion include/asl/String.h
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,7 @@ int ASL_API local8toUtf16(const char* u, wchar_t* p, int n);
int ASL_API utf16toUtf8(const wchar_t* p, char* u, int);
int ASL_API utf8toUtf16(const char* u, wchar_t* p, int);
int ASL_API utf32toUtf8(const int* p, char* u, int n);
int ASL_API utf8toUtf32(const char* u, int* p, int);
int ASL_API utf8toUtf32(const char* u, int* p, int n);
String ASL_API localToUtf8(const String& a);
String ASL_API utf8ToLocal(const String& a);
}
Expand Down
4 changes: 4 additions & 0 deletions include/asl/Var.h
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,10 @@ class ASL_API Var
*/
operator bool() const;

/**
Returns a char pointer to the beginning of the string if this var is a string; this
is faster than calling `.toString()` but will not stringify numbers or other types.
*/
const char* operator*() const;

ASL_DEPRECATED(operator const char*() const, "Use operator*") { return *(*this); }
Expand Down
11 changes: 9 additions & 2 deletions src/Console.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#include <asl/Console.h>
#include <asl/defs.h>
#ifdef _WIN32
#include <locale.h>
#endif

namespace asl {

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
{
Expand Down
4 changes: 2 additions & 2 deletions src/File.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ bool File::put(const Array<byte>& 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<byte> File::firstBytes(int n)
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Http.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
65 changes: 20 additions & 45 deletions src/String.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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++)
Expand Down Expand Up @@ -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++) {
Expand All @@ -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++)
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -518,9 +530,8 @@ bool String::isTrue() const
Array<int> String::chars() const
{
Array<int> 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)
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit a3ff3da

Please sign in to comment.