Add conversion to signed/unsigned int to wxString
Add wxString::ToInt() and ToUInt() for convenience and consistency with the existing ToLong() and ToULong(). Closes #22068.
This commit is contained in:
committed by
Vadim Zeitlin
parent
c10a70be67
commit
9dc7248b1d
@@ -43,6 +43,8 @@ private:
|
||||
CPPUNIT_TEST( Compare );
|
||||
CPPUNIT_TEST( CompareNoCase );
|
||||
CPPUNIT_TEST( Contains );
|
||||
CPPUNIT_TEST( ToInt );
|
||||
CPPUNIT_TEST( ToUInt );
|
||||
CPPUNIT_TEST( ToLong );
|
||||
CPPUNIT_TEST( ToULong );
|
||||
#ifdef wxLongLong_t
|
||||
@@ -78,6 +80,8 @@ private:
|
||||
void Compare();
|
||||
void CompareNoCase();
|
||||
void Contains();
|
||||
void ToInt();
|
||||
void ToUInt();
|
||||
void ToLong();
|
||||
void ToULong();
|
||||
#ifdef wxLongLong_t
|
||||
@@ -602,7 +606,8 @@ enum
|
||||
Number_Unsigned = 2, // if not specified, works for signed conversion
|
||||
Number_Signed = 4, // if not specified, works for unsigned
|
||||
Number_LongLong = 8, // only for long long tests
|
||||
Number_Long = 16 // only for long tests
|
||||
Number_Long = 16, // only for long tests
|
||||
Number_Int = 32 // only for int tests
|
||||
};
|
||||
|
||||
#ifdef wxLongLong_t
|
||||
@@ -613,6 +618,38 @@ typedef long TestValue_t;
|
||||
|
||||
wxGCC_WARNING_SUPPRESS(missing-field-initializers)
|
||||
|
||||
static const struct ToIntData
|
||||
{
|
||||
const wxChar *str;
|
||||
TestValue_t value;
|
||||
int flags;
|
||||
int base;
|
||||
|
||||
int IValue() const { return value; }
|
||||
unsigned int UIValue() const { return value; }
|
||||
|
||||
bool IsOk() const { return !(flags & Number_Invalid); }
|
||||
} intData[] =
|
||||
{
|
||||
{ wxT("1"), 1, Number_Ok },
|
||||
{ wxT("0"), 0, Number_Ok },
|
||||
{ wxT("a"), 0, Number_Invalid },
|
||||
{ wxT("12345"), 12345, Number_Ok },
|
||||
{ wxT("--1"), 0, Number_Invalid },
|
||||
|
||||
{ wxT("-1"), -1, Number_Signed | Number_Int },
|
||||
{ wxT("-1"), (TestValue_t)UINT_MAX, Number_Unsigned | Number_Int | Number_Invalid },
|
||||
|
||||
{ wxT("2147483647"), (TestValue_t)INT_MAX, Number_Int | Number_Signed },
|
||||
{ wxT("2147483648"), (TestValue_t)INT_MAX, Number_Int | Number_Signed | Number_Invalid },
|
||||
|
||||
{ wxT("-2147483648"), (TestValue_t)INT_MIN, Number_Int | Number_Signed },
|
||||
{ wxT("-2147483649"), (TestValue_t)INT_MIN, Number_Int | Number_Signed | Number_Invalid },
|
||||
|
||||
{ wxT("4294967295"), (TestValue_t)UINT_MAX, Number_Int | Number_Unsigned },
|
||||
{ wxT("4294967296"), (TestValue_t)UINT_MAX, Number_Int | Number_Unsigned | Number_Invalid },
|
||||
};
|
||||
|
||||
static const struct ToLongData
|
||||
{
|
||||
const wxChar *str;
|
||||
@@ -670,6 +707,64 @@ static const struct ToLongData
|
||||
|
||||
wxGCC_WARNING_RESTORE(missing-field-initializers)
|
||||
|
||||
void StringTestCase::ToInt()
|
||||
{
|
||||
int i;
|
||||
for (size_t n = 0; n < WXSIZEOF(intData); n++)
|
||||
{
|
||||
const ToIntData &id = intData[n];
|
||||
|
||||
if (id.flags & (Number_Unsigned))
|
||||
continue;
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(id.IsOk(),
|
||||
wxString(id.str).ToInt(&i, id.base));
|
||||
|
||||
if (id.IsOk())
|
||||
CPPUNIT_ASSERT_EQUAL(id.IValue(), i);
|
||||
}
|
||||
|
||||
// special case: check that the output is not modified if the parsing
|
||||
// failed completely
|
||||
i = 17;
|
||||
CPPUNIT_ASSERT(!wxString("foo").ToInt(&i));
|
||||
CPPUNIT_ASSERT_EQUAL(17, i);
|
||||
|
||||
// also check that it is modified if we did parse something successfully in
|
||||
// the beginning of the string
|
||||
CPPUNIT_ASSERT(!wxString("9 cats").ToInt(&i));
|
||||
CPPUNIT_ASSERT_EQUAL(9, i);
|
||||
}
|
||||
|
||||
void StringTestCase::ToUInt()
|
||||
{
|
||||
unsigned int i;
|
||||
for (size_t n = 0; n < WXSIZEOF(intData); n++)
|
||||
{
|
||||
const ToIntData &id = intData[n];
|
||||
|
||||
if (id.flags & (Number_Signed))
|
||||
continue;
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(id.IsOk(),
|
||||
wxString(id.str).ToUInt(&i, id.base));
|
||||
|
||||
if (id.IsOk())
|
||||
CPPUNIT_ASSERT_EQUAL(id.UIValue(), i);
|
||||
}
|
||||
|
||||
// special case: check that the output is not modified if the parsing
|
||||
// failed completely
|
||||
i = 17;
|
||||
CPPUNIT_ASSERT(!wxString("foo").ToUInt(&i));
|
||||
CPPUNIT_ASSERT_EQUAL(17, i);
|
||||
|
||||
// also check that it is modified if we did parse something successfully in
|
||||
// the beginning of the string
|
||||
CPPUNIT_ASSERT(!wxString("9 cats").ToUInt(&i));
|
||||
CPPUNIT_ASSERT_EQUAL(9, i);
|
||||
}
|
||||
|
||||
void StringTestCase::ToLong()
|
||||
{
|
||||
long l;
|
||||
|
||||
Reference in New Issue
Block a user