+// returns "not found" for npos, value for all others
+static wxString PosToString(size_t res)
+{
+ wxString s = res == wxString::npos ? wxString(_T("not found"))
+ : wxString::Format(_T("%u"), res);
+ return s;
+}
+
+static void TestStringFind()
+{
+ puts("*** Testing wxString find() functions ***");
+
+ static const wxChar *strToFind = _T("ell");
+ static const struct StringFindTest
+ {
+ const wxChar *str;
+ size_t start,
+ result; // of searching "ell" in str
+ } findTestData[] =
+ {
+ { _T("Well, hello world"), 0, 1 },
+ { _T("Well, hello world"), 6, 7 },
+ { _T("Well, hello world"), 9, wxString::npos },
+ };
+
+ for ( size_t n = 0; n < WXSIZEOF(findTestData); n++ )
+ {
+ const StringFindTest& ft = findTestData[n];
+ size_t res = wxString(ft.str).find(strToFind, ft.start);
+
+ printf(_T("Index of '%s' in '%s' starting from %u is %s "),
+ strToFind, ft.str, ft.start, PosToString(res).c_str());
+
+ size_t resTrue = ft.result;
+ if ( res == resTrue )
+ {
+ puts(_T("(ok)"));
+ }
+ else
+ {
+ printf(_T("(ERROR: should be %s)\n"),
+ PosToString(resTrue).c_str());
+ }
+ }
+
+ puts("");
+}
+
+// replace TABs with \t and CRs with \n
+static wxString MakePrintable(const wxChar *s)
+{
+ wxString str(s);
+ (void)str.Replace(_T("\t"), _T("\\t"));
+ (void)str.Replace(_T("\n"), _T("\\n"));
+ (void)str.Replace(_T("\r"), _T("\\r"));
+
+ return str;
+}
+
+static void TestStringTokenizer()
+{
+ puts("*** Testing wxStringTokenizer ***");
+
+ static const wxChar *modeNames[] =
+ {
+ _T("default"),
+ _T("return empty"),
+ _T("return all empty"),
+ _T("with delims"),
+ _T("like strtok"),
+ };
+
+ static const struct StringTokenizerTest
+ {
+ const wxChar *str; // string to tokenize
+ const wxChar *delims; // delimiters to use
+ size_t count; // count of token
+ wxStringTokenizerMode mode; // how should we tokenize it
+ } tokenizerTestData[] =
+ {
+ { _T(""), _T(" "), 0 },
+ { _T("Hello, world"), _T(" "), 2 },
+ { _T("Hello, world "), _T(" "), 2 },
+ { _T("Hello, world"), _T(","), 2 },
+ { _T("Hello, world!"), _T(",!"), 2 },
+ { _T("Hello,, world!"), _T(",!"), 3 },
+ { _T("Hello, world!"), _T(",!"), 3, wxTOKEN_RET_EMPTY_ALL },
+ { _T("username:password:uid:gid:gecos:home:shell"), _T(":"), 7 },
+ { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS, 4 },
+ { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS, 6, wxTOKEN_RET_EMPTY },
+ { _T("1 \t3\t4 6 "), wxDEFAULT_DELIMITERS, 9, wxTOKEN_RET_EMPTY_ALL },
+ { _T("01/02/99"), _T("/-"), 3 },
+ { _T("01-02/99"), _T("/-"), 3, wxTOKEN_RET_DELIMS },
+ };
+
+ for ( size_t n = 0; n < WXSIZEOF(tokenizerTestData); n++ )
+ {
+ const StringTokenizerTest& tt = tokenizerTestData[n];
+ wxStringTokenizer tkz(tt.str, tt.delims, tt.mode);
+
+ size_t count = tkz.CountTokens();
+ printf(_T("String '%s' has %u tokens delimited by '%s' (mode = %s) "),
+ MakePrintable(tt.str).c_str(),
+ count,
+ MakePrintable(tt.delims).c_str(),
+ modeNames[tkz.GetMode()]);
+ if ( count == tt.count )
+ {
+ puts(_T("(ok)"));
+ }
+ else
+ {
+ printf(_T("(ERROR: should be %u)\n"), tt.count);
+
+ continue;
+ }
+
+ // if we emulate strtok(), check that we do it correctly
+ wxChar *buf, *s, *last;
+
+ if ( tkz.GetMode() == wxTOKEN_STRTOK )
+ {
+ buf = new wxChar[wxStrlen(tt.str) + 1];
+ wxStrcpy(buf, tt.str);
+
+ s = wxStrtok(buf, tt.delims, &last);
+ }
+ else
+ {
+ buf = NULL;
+ }
+
+ // now show the tokens themselves
+ size_t count2 = 0;
+ while ( tkz.HasMoreTokens() )
+ {
+ wxString token = tkz.GetNextToken();
+
+ printf(_T("\ttoken %u: '%s'"),
+ ++count2,
+ MakePrintable(token).c_str());
+
+ if ( buf )
+ {
+ if ( token == s )
+ {
+ puts(" (ok)");
+ }
+ else
+ {
+ printf(" (ERROR: should be %s)\n", s);
+ }
+
+ s = wxStrtok(NULL, tt.delims, &last);
+ }
+ else
+ {
+ // nothing to compare with
+ puts("");
+ }
+ }
+
+ if ( count2 != count )
+ {
+ puts(_T("\tERROR: token count mismatch"));
+ }
+
+ delete [] buf;
+ }
+
+ puts("");
+}
+