Skip to content

Commit 4058b39

Browse files
committed
Changes in String: no implicit conversion to char* (only to const char*)
1 parent 557f646 commit 4058b39

File tree

6 files changed

+36
-21
lines changed

6 files changed

+36
-21
lines changed

include/asl/String.h

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright(c) 1999-2022 aslze
1+
// Copyright(c) 1999-2023 aslze
22
// Licensed under the MIT License (http://opensource.org/licenses/MIT)
33

44
#ifndef ASL_STRING_H
@@ -95,7 +95,7 @@ characters transparently.
9595
9696
~~~
9797
String dirName = "Mis imágenes" // this is UTF-8
98-
CreateDirectoryW( dirName ); // this gets converted to UTF-16 LPWSTRING
98+
CreateDirectoryW( dirName ); // this gets converted to UTF-16 LPCWSTR
9999
~~~
100100
101101
There are automatic conversions to/from many basic types, but please use with care!
@@ -336,7 +336,8 @@ class ASL_API String
336336
/**
337337
Returns true if this string is not empty (warning: this might change in the future to mean isTrue(), use `ok()`)
338338
*/
339-
operator bool() const {return _len > 0;}
339+
ASL_EXPLICIT operator bool() const { return _len > 0; }
340+
340341
/**
341342
Returns true if this string is empty (warning: this might change in the future to mean !isTrue(), use `!ok()`)
342343
*/
@@ -354,14 +355,21 @@ class ASL_API String
354355
Returns a pointer to the beginning of the character data (suitable for functions
355356
requiring C-style strings)
356357
*/
357-
operator char*() {return str();}
358+
char* data() { return str(); }
359+
const char* data() const { return str(); }
360+
358361
/**
359-
Returns a const pointer to a Unicode UCS2 representation of this string by expanding from the internal
360-
byte representation (suitable for functions requiring C-style wide strings (LPWSTR))
362+
Returns a const pointer to a Unicode UTF16 representation of this string by expanding from the internal
363+
byte representation (suitable for functions requiring C-style wide strings (LPCWSTR))
361364
*/
362365
operator const wchar_t*() const;
363366

364-
operator wchar_t*() const {return (wchar_t*)(const wchar_t*)*this;}
367+
/**
368+
Returns a pointer to a Unicode UTF16 representation of this string by expanding from the internal
369+
byte representation (suitable for functions requiring C-style wide strings (LPWSTR))
370+
*/
371+
wchar_t* dataw() { return (wchar_t*)(const wchar_t*)*this; }
372+
const wchar_t* dataw() const { return (const wchar_t*)*this; }
365373

366374
/**
367375
Returns a const pointer to the beginning of the character data (suitable for functions
@@ -711,8 +719,8 @@ class SafeString
711719
SafeString(String& s, int n) : _s(s), _wide(false) { _s.resize(3 * n); }
712720
~SafeString() { if (_wide) _s.fixW(); else _s.fix(); }
713721
operator const char*() const {return _s;}
714-
operator char*() const {return _s;}
715-
operator const wchar_t*() { _wide = true; return (wchar_t*)_s; }
722+
operator char*() const {return _s.data();}
723+
operator const wchar_t*() { _wide = true; return _s.dataw(); }
716724
operator const wchar_t*() const { _wide = true; return(const wchar_t*)(String*)&_s; }
717725
operator wchar_t*() { _wide = true; return(wchar_t*)(const wchar_t*)_s; }
718726
};

src/IniFile.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,7 @@ IniFile::IniFile(const String& name, bool shouldwrite)
6565
}
6666
String key = line.substring(0,i).trimmed();
6767
String value = line.substring(i+1).trimmed();
68-
for(char* p=key; *p; p++)
69-
if(*p == '/')
70-
*p = '\\';
68+
key.replaceme('/', '\\');
7169
_sections[_currentTitle][key] = value;
7270
}
7371
}

src/Process.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ namespace asl {
122122
DWORD len = 15;
123123
do {
124124
value.resize(len, false);
125-
len = GetEnvironmentVariableA(var, value, value.length() + 1);
125+
len = GetEnvironmentVariableA(var, value.data(), value.length() + 1);
126126
if (!len)
127127
value = "";
128128
} while ((int)len > value.length() + 1);
@@ -230,7 +230,7 @@ namespace asl {
230230

231231
PROCESS_INFORMATION procInfo;
232232
_ok = CreateProcessW(NULL,
233-
commandline, // application name
233+
commandline.dataw(), // application name
234234
NULL, // process security attributes
235235
NULL, // primary thread security attributes
236236
TRUE, // handles are inherited

src/Var.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,15 +265,15 @@ String Var::toString() const
265265
r[0]='\0';
266266
switch(_type) {
267267
case INT:
268-
r.fix(snprintf(r, r.cap(), "%i", i));
268+
r.fix(snprintf(r.data(), r.cap(), "%i", i));
269269
break;
270270
case FLOAT:
271271
r.resize(16);
272-
r.fix(snprintf(r, r.cap(), "%.7g", d));
272+
r.fix(snprintf(r.data(), r.cap(), "%.7g", d));
273273
break;
274274
case NUMBER:
275275
r.resize(29);
276-
r.fix(snprintf(r, r.cap(), "%.15g", d));
276+
r.fix(snprintf(r.data(), r.cap(), "%.15g", d));
277277
break;
278278
case BOOL:
279279
r=b?"true":"false";

src/Xdl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ void XdlParser::parse(const char* s)
270270
else if (c == ',' || myisspace(c) || c == ']' || c == '}')
271271
{
272272
#ifndef ASL_FAST_JSON
273-
for (char* p = _buffer; *p; p++)
273+
for (char* p = _buffer.data(); *p; p++)
274274
if (*p == '.')
275275
{
276276
*p = _ldp;
@@ -300,7 +300,7 @@ void XdlParser::parse(const char* s)
300300
else if(c == ',' || myisspace(c) || c == ']' || c == '}')
301301
{
302302
#ifndef ASL_FAST_JSON
303-
for(char* p = _buffer; *p; p++)
303+
for(char* p = _buffer.data(); *p; p++)
304304
if (*p == '.')
305305
{
306306
*p = _ldp;

tests/unittests.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,6 @@ ASL_TEST(String)
289289
ASL_ASSERT(f1 == xxx);
290290

291291
String u1(L"1");
292-
293292
ASL_ASSERT(u1 == "1");
294293

295294
String sf = String::f("a%i", 2);
@@ -369,14 +368,24 @@ ASL_TEST(String)
369368
Dic<> dic = String("x=1,y=2").split(',', '=');
370369
ASL_ASSERT(dic["x"] == "1" && dic["y"] == "2");
371370

372-
String empty = "";
371+
String empty;
373372
ASL_ASSERT(!empty.ok());
374373
ASL_ASSERT(String("c").ok());
375374
String full = "abc";
376375
int valid1 = empty.ok()? 1 : 0;
377376
int valid2 = full.ok()? 1 : 0;
378377
ASL_ASSERT(valid1 == 0 && valid2 == 1);
379378

379+
// bool conversion means not empty (might change in the future)
380+
ASL_ASSERT(!empty);
381+
int valid1b = empty ? 1 : 0;
382+
int valid2b = full ? 1 : 0;
383+
ASL_ASSERT(valid1b == 0 && valid2b == 1);
384+
385+
String five = "5";
386+
int intfive = five;
387+
ASL_CHECK(intfive, ==, 5);
388+
380389
ASL_ASSERT(!empty.isTrue());
381390
ASL_ASSERT(!String("false").isTrue());
382391

0 commit comments

Comments
 (0)