tsc_String example code
The following example code is an extract of the tsc_String unit tests, and demonstrates just about every way to use the class. For this reason, a lot of the examples are trivial and obvious.
class ToStringObject : public tsc_Object
{
public:
virtual tsc_String ToString() const
{
return "Greetings from ToStringObject";
}
};
#define RunTest(testExpression, expectedResult) ...
class StringsTestRig : public ApiTestRigBase
{
void RunTests()
{
RunTest (StaticString == "Static-one-two", true);
const char * csts = "TestString1";
tsc_String s0 = "";
tsc_String s1 = csts;
tsc_String s2 = "TestString2";
tsc_String s3 ("TEST", "String2");
tsc_String s4 ('a', 4);
tsc_String s5 = "0a0bc0def0ghij0klmno0pqrstu";
tsc_String si = 'i';
s5 += "0vwxyz";
RunTest (s0.IsEmpty(), true);
RunTest (s1.IsEmpty(), false);
RunTest (s0 == s0, true);
RunTest (s0 != s0, false);
RunTest (s1 == s1, true);
RunTest (s1 != s1, false);
RunTest (s0 == s1, false);
RunTest (s0 != s1, true);
RunTest (s1 == s2, false);
RunTest (s1 != s2, true);
RunTest (si == 'i', true);
RunTest (si == 'I', false);
RunTest (s0 + "", "");
RunTest (s1 + "", csts);
RunTest (s1 + "Hundred", "TestString1Hundred");
RunTest ("" + s1, "TestString1");
RunTest ("Bollocks" + s1, "BollocksTestString1");
RunTest (s1 + s3, "TestString1TESTString2");
RunTest (s1 + s2 + s3, "TestString1TestString2TESTString2");
RunTest (s1 + s1 + s1, "TestString1TestString1TestString1");
RunTest (s1[0], 'T');
RunTest (s1[1], 'e');
RunTest (s1[2], 's');
RunTest (s1[10], '1');
RunTest (s1.Compare (s2), -1);
RunTest (s2.Compare (s1), 1);
RunTest (s1.Compare (s1), 0);
RunTest (s0.Compare (s0), 0);
RunTest (s2.Compare (s3), 1);
RunTest (s2.Compare (s3, Case_Insensitive), 0);
RunTest (s0.Length(), 0);
RunTest (s1.Length(), 11);
RunTest (s0.ToLower(), "");
RunTest (s1.ToLower(), "teststring1");
RunTest (s0.ToUpper(), "");
RunTest (s1.ToUpper(), "TESTSTRING1");
RunTest (s1.ToString(), "TestString1");
RunTest (s0.IndexOf ('r'), -1);
RunTest (s0.IndexOf (""), -1);
RunTest (s1.IndexOf ('r'), 6);
RunTest (s1.IndexOf ("T"), 0);
RunTest (s1.IndexOf ("TestString1"), 0);
RunTest (s1.IndexOf ("TestString12"), -1);
RunTest (s1.IndexOf ("ing1"), 7);
RunTest (s1.IndexOf ('R'), -1);
RunTest (s1.IndexOf ('S'), 4);
RunTest (s1.IndexOf ('1'), 10);
RunTest (s1.IndexOf ('t',4), 5);
RunTest (s1.IndexOf ('1',10), 10);
RunTest (s1.IndexOf ('1',11), -1);
RunTest (s1.IndexOf (si,2), 7);
RunTest (s1.IndexOf ('G',0,Case_Insensitive), 9);
RunTest (s1.IndexOf ('g',0,Case_Insensitive), 9);
RunTest (s1.IndexOf ('t',0,Case_Insensitive), 0);
RunTest (s1.IndexOf ('S',0,Case_Insensitive), 2);
RunTest (s1.IndexOf ("st",3,Case_Insensitive), 4);
RunTest (s1.IndexOfAny (""), -1);
RunTest (s1.IndexOfAny ("ERING"), -1);
RunTest (s1.IndexOfAny ("ERING1"), 10);
RunTest (s1.IndexOfAny ("t",4), 5);
RunTest (s1.IndexOfAny ("ring"), 6);
RunTest (s1.IndexOfAny ("1",10), 10);
RunTest (s1.IndexOfAny ("1",11), -1);
RunTest (s1.IndexOfAny (si,2), 7);
RunTest (s1.IndexOfAny ("test",4), 5);
RunTest (s1.IndexOfAny ("teSt",4), 4);
RunTest (s1.IndexOfAny ("test",5), 5);
RunTest (s1.IndexOfAny ("1",10), 10);
RunTest (s1.IndexOfAny ("ng1",11), -1);
RunTest (s1.IndexOfAny ("abcdfhjklmopquvwxy1z"), 10);
RunTest (s1.IndexOfAny ("abcdfhjklmopquvwxy1z"), 10);
RunTest (s1.IndexOfAny ("1G",0,Case_Insensitive), 9);
RunTest (s1.IndexOfAny ("1g",0,Case_Insensitive), 9);
RunTest (s1.IndexOfAny ("ts",0,Case_Insensitive), 0);
RunTest (s1.IndexOfAny ("S",0,Case_Insensitive), 2);
RunTest (s1.IndexOfAny ("srng",3,Case_Insensitive), 4);
RunTest (s0.Contains ('r'), false);
RunTest (s0.Contains (""), false);
RunTest (s1.Contains ('r'), true);
RunTest (s1.Contains ('R'), false);
RunTest (s1.Contains ('S'), true);
RunTest (s1.Contains ('1'), true);
RunTest (s1.Contains (si), true);
RunTest (s1.Contains ('G', Case_Insensitive), true);
RunTest (s1.Contains ('g', Case_Insensitive), true);
RunTest (s1.Contains ('t', Case_Insensitive), true);
RunTest (s1.Contains ('S', Case_Insensitive), true);
RunTest (s0.LastIndexOf ('r'), -1);
RunTest (s1.LastIndexOf ('q'), -1);
RunTest (s1.LastIndexOf ('t'), 5);
RunTest (s1.LastIndexOf ('T'), 0);
RunTest (s1.LastIndexOf ('1'), 10);
RunTest (s1.LastIndexOf (si[0]), 7);
RunTest (s0.StartsWith (""), true);
RunTest (s0.StartsWith ("a"), false);
RunTest (s1.StartsWith ('T'), true);
RunTest (s1.StartsWith ('U'), false);
RunTest (s1.StartsWith ("est"), false);
RunTest (s1.StartsWith ("Test"), true);
RunTest (s1.StartsWith (csts), true);
RunTest (s1.StartsWith ("TestString9"), false);
RunTest (s3.StartsWith ("TestString2"), false);
RunTest (s3.StartsWith ("TESTString2"), true);
RunTest (s3.StartsWith ("TestString2", Case_Insensitive), true);
RunTest (s3.StartsWith ("teststRING2", Case_Insensitive), true);
RunTest (s0.EndsWith (""), true);
RunTest (s0.EndsWith ("a"), false);
RunTest (s1.EndsWith ('1'), true);
RunTest (s1.EndsWith ('2'), false);
RunTest (s1.EndsWith ("ring"), false);
RunTest (s1.EndsWith ("ring1"), true);
RunTest (s1.EndsWith ("TestString1"), true);
RunTest (s1.EndsWith ("TestString9"), false);
RunTest (s3.EndsWith ("TestString"), false);
RunTest (s3.EndsWith ("ESTString2"), true);
RunTest (s3.EndsWith ("estString2", Case_Insensitive), true);
RunTest (s3.EndsWith ("eststRING2", Case_Insensitive), true);
RunTest (s0.Replace ("Q", "B"), "");
RunTest (s1.Replace ("Q", "B"), "TestString1");
RunTest (s1.Replace ("TestString1", "EelsInMyBath"), "EelsInMyBath");
RunTest (s1.Replace ("T", "B"), "BestString1");
RunTest (s1.Replace ("T", ""), "estString1");
RunTest (s1.Replace ("T", "Br"), "BrestString1");
RunTest (s1.Replace ("Test", ""), "String1");
RunTest (s1.Replace ("Test", "Dest"), "DestString1");
RunTest (s1.Replace ("Test", "Destination"), "DestinationString1");
RunTest (s1.Replace ("S", "Z"), "TestZtring1");
RunTest (s1.Replace ("S", ""), "Testtring1");
RunTest (s1.Replace ("S", "Fil"), "TestFiltring1");
RunTest (s1.Replace ("String", ""), "Test1");
RunTest (s1.Replace ("String", "Banana"), "TestBanana1");
RunTest (s1.Replace ("String", "Pantechnicon"), "TestPantechnicon1");
RunTest (s1.Replace ("1", "Z"), "TestStringZ");
RunTest (s1.Replace ("1", ""), "TestString");
RunTest (s1.Replace ("1", "Thing"), "TestStringThing");
RunTest (s1.Replace ("String1", ""), "Test");
RunTest (s1.Replace ("String1", "Banana"), "TestBanana");
RunTest (s1.Replace ("String", "Pantechnicon"), "TestPantechnicon1");
RunTest (s1.Replace ("t", "b", Replace_CaseInsensitive), "bestString1");
RunTest (s1.Replace ("strING", "", Replace_CaseInsensitive), "Test1");
RunTest (s1.Replace ("Q", "", Replace_AllOccurrences), "TestString1");
RunTest (s1.Replace ("String", "", Replace_AllOccurrences), "Test1");
RunTest (s1.Replace ("t", "", Replace_AllOccurrences), "TesSring1");
RunTest (s4.Replace ("A", "Har", Replace_AllOccurrences), "aaaa");
RunTest (s4.Replace ("a", "", Replace_AllOccurrences), "");
RunTest (s5.Replace ("0", "|=|", Replace_AllOccurrences), "|=|a|=|bc|=|def|=|ghij|=|klmno|=|pqrstu|=|vwxyz");
RunTest (s5.Replace ("0", "", Replace_AllOccurrences), "abcdefghijklmnopqrstuvwxyz");
RunTest (s5.Replace ("pqrstu", "-", Replace_AllOccurrences), "0a0bc0def0ghij0klmno0-0vwxyz");
RunTest (s5.Replace ("vwxyz", "-", Replace_AllOccurrences), "0a0bc0def0ghij0klmno0pqrstu0-");
RunTest (s5.Replace ("vwxyz", "", Replace_AllOccurrences), "0a0bc0def0ghij0klmno0pqrstu0");
RunTest (s5.Replace ("vwxyz", "123456789", Replace_AllOccurrences), "0a0bc0def0ghij0klmno0pqrstu0123456789");
RunTest (s1.Replace ("sTRIng", "", Replace_AllOccurrences | Replace_CaseInsensitive), "Test1");
RunTest (s1.Replace ("t", "Q", Replace_AllOccurrences | Replace_CaseInsensitive), "QesQSQring1");
RunTest (s1.Replace ("1", "", Replace_AllOccurrences | Replace_CaseInsensitive), "TestString");
RunTest (s1.Replace ("G1", "", Replace_AllOccurrences | Replace_CaseInsensitive), "TestStrin");
RunTest (s1.Replace ("G1", "dles", Replace_AllOccurrences | Replace_CaseInsensitive), "TestStrindles");
RunTest (s1.Replace (si, s4, Replace_AllOccurrences | Replace_CaseInsensitive), "TestStraaaang1");
RunTest (s4.Replace ("A", "H", Replace_AllOccurrences | Replace_CaseInsensitive), "HHHH");
RunTest (s4.Replace ("A", "Har", Replace_AllOccurrences | Replace_CaseInsensitive), "HarHarHarHar");
RunTest (s4.Replace ("A", "", Replace_AllOccurrences | Replace_CaseInsensitive), "");
RunTest (s0.Substring (0), "");
RunTest (s0.Substring (1), "");
RunTest (s1.Substring (0), csts);
RunTest (s1.Substring (1), "estString1");
RunTest (s1.Substring (10), "1");
RunTest (s1.Substring (11), "");
RunTest (s0.Substring (0, 1), "");
RunTest (s0.Substring (1, 1), "");
RunTest (s1.Substring (0, 2), "Te");
RunTest (s1.Substring (1, 2), "es");
RunTest (s1.Substring (10, 2), "1");
RunTest (s1.Substring (11, 2), "");
RunTest (s1.Substring (8, 88), "ng1");
RunTest (s0.Insert (0, s4), "aaaa");
RunTest (s1.Insert (0, s4), "aaaaTestString1");
RunTest (s1.Insert (1, s4), "TaaaaestString1");
RunTest (s1.Insert (10, s4), "TestStringaaaa1");
RunTest (s1.Insert (11, s4), "TestString1aaaa");
RunTest (s1.Remove (0), "");
RunTest (s1.Remove (1), "T");
RunTest (s1.Remove (2), "Te");
RunTest (s1.Remove (10), "TestString");
RunTest (s1.Remove (2, 0), "TestString1");
RunTest (s1.Remove (2, 8), "Te1");
RunTest (s1.Remove (2, 9), "Te");
RunTest (s1.Remove (2, 10), "Te");
RunTest (s1.Remove (2, 11), "Te");
RunTest (s1.Remove (2, 111), "Te");
RunTest (s1.Remove (10, 1), "TestString");
RunTest (s1.Remove (10, 2), "TestString");
tsc_String r0 = " ";
tsc_String r1 = "a ";
tsc_String r2 = " a ";
tsc_String r3 = " a";
RunTest (s0.TrimLeft(), "");
RunTest (r0.TrimLeft(), "");
RunTest (r1.TrimLeft(), "a ");
RunTest (r2.TrimLeft(), "a ");
RunTest (r3.TrimLeft(), "a");
RunTest (s0.TrimRight(), "");
RunTest (r0.TrimRight(), "");
RunTest (r1.TrimRight(), "a");
RunTest (r2.TrimRight(), " a");
RunTest (r3.TrimRight(), " a");
RunTest (s0.Trim(), "");
RunTest (r0.Trim(), "");
RunTest (r1.Trim(), "a");
RunTest (r2.Trim(), "a");
RunTest (r3.Trim(), "a");
tsc_String rc0 = "---";
tsc_String rc1 = "a--";
tsc_String rc2 = "-a-";
tsc_String rc3 = "--a";
RunTest (s0.TrimLeft ('-'), "");
RunTest (rc0.TrimLeft('-'), "");
RunTest (rc1.TrimLeft('-'), "a--");
RunTest (rc2.TrimLeft('-'), "a-");
RunTest (rc3.TrimLeft('-'), "a");
RunTest (s0.TrimRight ('-'), "");
RunTest (rc0.TrimRight('-'), "");
RunTest (rc1.TrimRight('-'), "a");
RunTest (rc2.TrimRight('-'), "-a");
RunTest (rc3.TrimRight('-'), "--a");
RunTest (s0.Trim ('-'), "");
RunTest (rc0.Trim('-'), "");
RunTest (rc1.Trim('-'), "a");
RunTest (rc2.Trim('-'), "a");
RunTest (rc3.Trim('-'), "a");
tsc_String sw1 = "First";
tsc_String sw2 = "Second";
tsc_String sw3;
sw1.Swap(sw2);
sw2.Swap(sw3);
RunTest (sw1, "Second");
RunTest (sw2, "");
RunTest (sw3, "First");
RunTest (s0.Split (',').Count(), 0);
tsc_String sp1 = "a,bb,ccc,dddd";
tsc_StringList spres1 = sp1.Split (',');
RunTest (spres1.Count(), 4);
RunTest (spres1[0], "a");
RunTest (spres1[1], "bb");
RunTest (spres1[2], "ccc");
RunTest (spres1[3], "dddd");
tsc_String sp2 = ",333,22,1,";
tsc_StringList spres2 = sp2.Split (',');
RunTest (spres2.Count(), 5);
RunTest (spres2[0], "");
RunTest (spres2[1], "333");
RunTest (spres2[2], "22");
RunTest (spres2[3], "1");
RunTest (spres2[4], "");
tsc_StringList spres3 = sp2.Split (',', Split_RemoveEmptyEntries);
RunTest (spres3.Count(), 3);
RunTest (spres3[0], "333");
RunTest (spres3[1], "22");
RunTest (spres3[2], "1");
tsc_String sp4 = ";333,=22.1=";
tsc_StringList spres4 = sp4.Split ("=.;,");
RunTest (spres4.Count(), 6);
RunTest (spres4[0], "");
RunTest (spres4[1], "333");
RunTest (spres4[2], "");
RunTest (spres4[3], "22");
RunTest (spres4[4], "1");
RunTest (spres4[5], "");
tsc_StringList spres5 = sp4.Split ("=.;,", Split_RemoveEmptyEntries);
RunTest (spres5.Count(), 3);
RunTest (spres5[0], "333");
RunTest (spres5[1], "22");
RunTest (spres5[2], "1");
tsc_String sp6 = ";one,'two.three'=four;";
tsc_StringList spres6 = sp6.Split ("=.;,", Split_RespectQuotes);
RunTest (spres6.Count(), 5);
RunTest (spres6[0], "");
RunTest (spres6[1], "one");
RunTest (spres6[2], "'two.three'");
RunTest (spres6[3], "four");
RunTest (spres6[4], "");
tsc_StringList spres7 = sp6.Split ("=.;,", tsc_SplitOptions(Split_RespectQuotes | Split_RemoveEmptyEntries));
RunTest (spres7.Count(), 3);
RunTest (spres7[0], "one");
RunTest (spres7[1], "'two.three'");
RunTest (spres7[2], "four");
tsc_String sp8 = "::one;two::::'::three::'four";
tsc_StringList strDelims ("", "::", ";");
tsc_StringList spres8 = sp8.Split (strDelims);
RunTest (spres8.Count(), 7);
RunTest (spres8[0], "");
RunTest (spres8[1], "one");
RunTest (spres8[2], "two");
RunTest (spres8[3], "");
RunTest (spres8[4], "'");
RunTest (spres8[5], "three");
RunTest (spres8[6], "'four");
tsc_StringList spres9 = sp8.Split (strDelims, Split_RemoveEmptyEntries);
RunTest (spres9.Count(), 5);
RunTest (spres9[0], "one");
RunTest (spres9[1], "two");
RunTest (spres9[2], "'");
RunTest (spres9[3], "three");
RunTest (spres9[4], "'four");
tsc_StringList spres10 = sp8.Split (strDelims, Split_RespectQuotes);
RunTest (spres10.Count(), 5);
RunTest (spres10[0], "");
RunTest (spres10[1], "one");
RunTest (spres10[2], "two");
RunTest (spres10[3], "");
RunTest (spres10[4], "'::three::'four");
tsc_StringList spres11 = sp8.Split (strDelims, tsc_SplitOptions(Split_RespectQuotes | Split_RemoveEmptyEntries));
RunTest (spres11.Count(), 3);
RunTest (spres11[0], "one");
RunTest (spres11[1], "two");
RunTest (spres11[2], "'::three::'four");
tsc_String sNoDelim = "None";
RunTest (sNoDelim.Split(',').Count(), 1);
RunTest (sNoDelim.Split(",.").Count(), 1);
RunTest (sNoDelim.Split(tsc_StringList(",")).Count(), 1);
tsc_String sArcMinutes = "6465, ABC, 16\",\"500\", FBE";
tsc_StringList spres12 = sArcMinutes.Split(',', Split_RespectQuotes);
RunTest (spres12.Count(), 5);
RunTest (spres12[2], " 16\"");
RunTest (spres12[3], "\"500\"");
#if PLATFORM_WINDOWS // Because clang thinks this is pointless!
tsc_String t1 = s1;
t1 = t1;
RunTest (t1, s1);
#endif
//=== Unicode strings ===
tsc_UniString w1 = s1;
tsc_String x1 = w1.ToString();
RunTest (x1, s1);
tsc_UniString w1b = s0;
tsc_String x1b = w1b.ToString();
RunTest (x1b, s0);
tsc_UniString w1c = w1;
tsc_String x1c = w1c.ToString();
RunTest (x1c, s1);
RunTest(tsc_UniString().IsEmpty(), true);
tsc_UniString uniStrTemp = tsc_UniString("temp");
RunTest(uniStrTemp.ToString(), "temp");
RunTest(uniStrTemp.Length(), 4);
RunTest(uniStrTemp.IsEmpty(), false);
RunTest(uniStrTemp[0], L't');
RunTest(uniStrTemp[1], L'e');
RunTest(uniStrTemp[2], L'm');
RunTest(uniStrTemp[3], L'p');
RunTest(uniStrTemp[4], L'\0');
tsc_UniString w2 = L"Unicode (usc2) string.";
const char* u2 = "Unicode (usc2) string.";
tsc_String x2 = w2.ToString();
RunTest (x2, u2);
// Do a round-trip to unicode and back to utf8.
tsc_UniString z2 = x2;
tsc_String y2 = z2.ToString();
RunTest (y2, u2);
// Empty unicode string.
tsc_UniString w3 = L"";
tsc_String x3 = w3.ToString();
RunTest (x3.Length(), 0);
tsc_UniString w4; // Empty constructor
RunTest (w4.ToString(), "");
tsc_UniString w5 (L'z'); // wchar constructor
RunTest (w5.ToString(), "z");
// === tsc_String::Format ===
tsc_String spf1;
tsc_String spf2 = "a";
tsc_String spf3 = "bc";
tsc_String spf4 = "def";
tsc_String spf5 = "ghij";
tsc_String spf6 = "klmno";
tsc_String spf7 = "pqrstu";
const char* spf8 = "";
const char* spf9 = "123";
const char* spf10 = "123456";
tsc_String fs1 = tsc_String::Format ("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s", spf1, spf2, spf3, spf4, spf5, spf6, spf7, spf8, spf9, spf10);
RunTest (fs1, ",a,bc,def,ghij,klmno,pqrstu,,123,123456");
char sfch = 'm';
short sfsh = 32767;
int sfsi = -54321;
unsigned int sfui = 891;
float sffl = -1.9876e12f;
double sfdb = 9.8765432109;
x_Code sfxc = X_Yes;
ToStringObject tsobj;
tsc_String fs2 = tsc_String::Format(
"%c;%hd;%d;%d;%g;%.12g;%s;%s",
sfch, sfsh, sfsi, sfui, sffl, sfdb, sfxc, tsobj);
RunTest (fs2, "m;32767;-54321;891;-1.9876e+12;9.8765432109;Yes;Greetings from ToStringObject");
// Test a very big Format...
tsc_String bigStr ('x', 65500);
tsc_String expect = "[-3.500000,123" + bigStr + "456]";
tsc_String result = tsc_String::Format ("[%f,%d%s%d]", -3.5, 123, bigStr, 456);
if (result != expect)
{
RunTest (tsc_String("[Big string 1]"), tsc_String("[Big string 2]")); // Strings too big to log.
}
else
{
this->Context.TotalTestCount++;
}
result = tsc_String::Format ("[%0.*f]", 1, 3.14159); RunTest (result, "[3.1]");
result = tsc_String::Format ("[%0.*f]", 2, 3.14159); RunTest (result, "[3.14]");
result = tsc_String::Format ("[%0.*f]", 3, 3.14159); RunTest (result, "[3.142]");
result = tsc_String::Format ("[%0*i]", 6, 123); RunTest (result, "[000123]");
result = tsc_String::Format ("[%0*.*f]", 7, 3, 3.1); RunTest (result, "[003.100]");
//==================== Unicode conversions...
tsc_StringList utf8Strings (
u8"👍",
u8"alkutäyttö-𐐷-😎👍",
u8"ab©",
u8"2H₂ + O₂ ⇌ 2H₂O, R = 4.7 kΩ, ⌀ 200 mm"
);
for (int i = 0; i < utf8Strings.Count(); i++)
{
tsc_String str8 {utf8Strings[i]};
tsc_UniString ustr {str8};
tsc_String rtrip {ustr.ToString()};
RunTest (rtrip, str8); // Test fails, utf32 not handled properly on Android.
}