string.cpp

An example of the string class.

00001 // Copyright (C) 2006-2010 David Sugar, Tycho Softworks.
00002 //
00003 // This file is part of GNU uCommon C++.
00004 //
00005 // GNU uCommon C++ is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU Lesser General Public License as published
00007 // by the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // GNU uCommon C++ is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU Lesser General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU Lesser General Public License
00016 // along with GNU uCommon C++.  If not, see <http://www.gnu.org/licenses/>.
00017 
00018 #ifndef DEBUG
00019 #define DEBUG
00020 #endif
00021 
00022 #include <ucommon/ucommon.h>
00023 
00024 #include <stdio.h>
00025 
00026 using namespace UCOMMON_NAMESPACE;
00027 
00028 static string_t testing("second test");
00029 
00030 extern "C" int main()
00031 {
00032     char buff[33];
00033     char *tokens = NULL;
00034     unsigned count = 0;
00035     const char *tp;
00036     const char *array[5];
00037 
00038     assert(max(3, 2) == 3);
00039 
00040     String::fill(buff, 32, ' ');
00041     stringbuf<128> mystr;
00042     mystr = (string_t)"hello" + (string_t)" this is a test";
00043     assert(eq_case("hello this is a test", *mystr));
00044     assert(eq_case("second test", *testing));
00045     assert(eq_case(" Is a test", mystr(-10)));
00046     mystr = "  abc 123 \n  ";
00047     assert(eq_case("abc 123", String::strip(mystr.c_mem(), " \n")));
00048     String::set(buff, sizeof(buff), "this is \"a test\"");
00049     while(NULL != (tp = String::token(buff, &tokens, " ", "\"\"")) && count < 4)
00050         array[count++] = tp;
00051     assert(count == 3);
00052     assert(eq_case(array[1], "is"));
00053     assert(eq_case(array[2], "a test"));
00054 
00055     unsigned char core[4] = {0x01, 0x10, 0x2f, 0x45};
00056     char hexbuf[12];
00057 
00058     assert(String::hexdump(core, hexbuf, "3-1") == 9);
00059     assert(eq(hexbuf, "01102f-45"));
00060 
00061     unsigned char hcore[4];
00062 
00063     String::hexpack(hcore, hexbuf, "3-1");
00064     assert(String::hexdump(hcore, hexbuf, "3-1") == 9);
00065     assert(eq(hexbuf, "01102f-45"));
00066 
00067     String numstr = "-33.5,25";
00068     Real num1;
00069     Unsigned num2;
00070 
00071     numstr % num1 % "," % num2;
00072     assert(num1 == -33.5);
00073     assert(num2 == 25);
00074     assert(numstr.len() == 0);
00075 
00076     char *test = strdup(str("hello") + " test" + str((short)13));
00077     assert(eq(test, "hello test13"));
00078 
00079     char *cdup = dup<char>(test[6]);
00080     assert(eq(cdup, "test13"));
00081 
00082     return 0;
00083 }

Generated on 14 Aug 2013 for UCommon by  doxygen 1.4.7