From 4875a3dd9b183dcd2256e2abfc4ccf7484c233b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Frings-F=C3=BCrst?= Date: Wed, 7 Dec 2022 13:17:14 +0100 Subject: New upstream version 4.0.2 --- src/examples/xb_ex_string.cpp | 385 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 385 insertions(+) create mode 100755 src/examples/xb_ex_string.cpp (limited to 'src/examples/xb_ex_string.cpp') diff --git a/src/examples/xb_ex_string.cpp b/src/examples/xb_ex_string.cpp new file mode 100755 index 0000000..3d582a1 --- /dev/null +++ b/src/examples/xb_ex_string.cpp @@ -0,0 +1,385 @@ +/* xb_ex_string.cpp + +XBase64 Software Library + +Copyright (c) 1997,2003,2014,2021,2022 Gary A Kunkel + +The xb64 software library is covered under the terms of the GPL Version 3, 2007 license. + +Email Contact: + + XDB-devel@lists.sourceforge.net + XDB-users@lists.sourceforge.net + +*/ + +// This demonstrates the string class + + +#include "xbase.h" + +using namespace xb; + +int main() +{ + + // create a string, assign a value, print it + xbString s1; + s1 = "Test String 1"; + fprintf( stdout, "s1 = [%s]\n", s1.Str()); + + // create another string, copy the value from s1 into it, print it + xbString s2; + s2 = s1; + std::cout << "s2 = [" << s2.Str() << "]" << std::endl; + + // create another string with and print it + xbString s3( 'X' ); + std::cout << "s3 = [" << s3.Str() << "]" << std::endl; + + // create another string with and print it and print it out yet another way + xbString s4( "Class constructor test 4" ); + printf( "s4 = [%s]\n", s4.Str() ); + + // create another string with a size limit and print it out + xbString s5( "Class constructor test 4", 7 ); + printf( "s5 = [%s]\n", s5.Str() ); + + // create another string from a string + xbString s6( s5 ); + printf( "s6 = [%s]\n", s6.Str() ); + + // create 100 byte string with nothing in it + xbString s7( (xbUInt32) 100 ); + printf( "s7 = [%s]\n", s7.Str() ); + + // Extract character from a particular position in the string + printf( "[] test -- Position 7 (starts from 1) from String 1 = [%c]\n", s1[7] ); + // or use the getCharacter method + printf( "getCharacter() test -- Position 7 (starts from 1) from String 1 = [%c]\n", s1.GetCharacter(7) ); + + // set string 7 to a character + s7 = 'Z'; + printf( "updated s7 = [%s]\n", s7.Str() ); + + // trim methods + s3 = " abc "; + s3.Ltrim(); + s3.Dump( "LTrim test" ); + + + s3 = " abc "; + s3.Rtrim(); + s3.Dump( "RTrim test" ); + + s3.Trim(); + s3.Dump( "Trim test" ); + printf( "s3 Len = [%d]\n", s3.Len() ); + + // Concatenation tests - I + s1 = "Concatenation test1 part 1 "; + s1 += "Concatenation test1 part 2 "; + s2 = " s2 data "; + s1 += s2; + s1 += 'z'; + + // Concatenation tests - II + s1 = "Concatenation test1 part 1 "; + s1 -= "Concatenation test1 part 2 "; + s1 -= 'X'; + s1 -= s2; + s1.Dump( "Concatenation test 2 " ); + + + // Concatenation tests - III + s1 = "s1data "; + s2 = "s2data "; + + s3 = s1 - s2; + s3.Dump( "Concatenation test 3a" ); + + s3 = s1 + s2; + s3.Dump( "Concatenation test 3b" ); + + s3 = s1 + " char * data "; + s3.Dump( "Concatenation test 3c" ); + + // s3 = " char * data " + s2; + // s3.Dump( "Concatenation test 3d" ); + + s3 = s1 + 'Z'; + s3.Dump( "Concatenation test 3e" ); + +// s3 = 'A' + s1; + s3 = 'A'; + + std::cout << s3.Str() << std::endl; + s3 += s1; + + std::cout << s3.Str() << std::endl; + + + s3 = 'A' + s1; + + + return 0; + + // s3.Dump( "Concatenation test 3f" ); + + std::cout << std::endl << "== operator tests" << std::endl; + if( s1 == s2 ) + std::cout << s1.Str() << " == " << s2.Str() << std::endl; + else + std::cout << s1.Str() << " != " << s2.Str() << std::endl; + + s1 = s2; + if( s1 == s2 ) + std::cout << s1.Str() << " == " << s2.Str() << std::endl; + else + std::cout << s1.Str() << " != " << s2.Str() << std::endl; + + if( s1 == "sometestdata" ) + std::cout << s1.Str() << " == sometestdata" << s2.Str() << std::endl; + else + std::cout << s1.Str() << " != sometestdata" << s2.Str() << std::endl; + + std::cout << std::endl << "!= operator tests" << std::endl; + s2 = "abc123"; + s1.Dump( "s1" ); + s2.Dump( "s2" ); + if( s1 != s2 ) + std::cout << s1.Str() << " != " << s2.Str() << std::endl; + else + std::cout << s1.Str() << " == " << s2.Str() << std::endl; + + s1 = s2; + if( s1 != s2 ) + std::cout << s1.Str() << " != " << s2.Str() << std::endl; + else + std::cout << s1.Str() << " == " << s2.Str() << std::endl; + + if( s1 != "sometestdata" ) + std::cout << s1.Str() << " != [sometestdata]" << std::endl; + else + std::cout << s1.Str() << " == [sometestdata]" << std::endl; + + std::cout << std::endl << "< operator tests" << std::endl; + s1 = "AAA"; + s2 = "BBB"; + + if( s1 < s2 ) + std::cout << s1.Str() << " < " << s2.Str() << std::endl; + else + std::cout << s1.Str() << " >= " << s2.Str() << std::endl; + + s1 = "BBB"; + if( s1 < s2 ) + std::cout << s1.Str() << " < " << s2.Str() << std::endl; + else + std::cout << s1.Str() << " >= " << s2.Str() << std::endl; + + s1 = "CCC"; + if( s1 < s2 ) + std::cout << s1.Str() << " < " << s2.Str() << std::endl; + else + std::cout << s1.Str() << " >= " << s2.Str() << std::endl; + + std::cout << std::endl << "> operator tests" << std::endl; + s1 = "AAA"; + s2 = "BBB"; + + if( s1 > s2 ) + std::cout << s1.Str() << " > " << s2.Str() << std::endl; + else + std::cout << s1.Str() << " <= " << s2.Str() << std::endl; + + s1 = "BBB"; + if( s1 > s2 ) + std::cout << s1.Str() << " > " << s2.Str() << std::endl; + else + std::cout << s1.Str() << " <= " << s2.Str() << std::endl; + + s1 = "CCC"; + if( s1 > s2 ) + std::cout << s1.Str() << " > " << s2.Str() << std::endl; + else + std::cout << s1.Str() << " <= " << s2.Str() << std::endl; + + std::cout << std::endl << "<= operator tests" << std::endl; + s1 = "AAA"; + s2 = "BBB"; + + if( s1 <= s2 ) + std::cout << s1.Str() << " <= " << s2.Str() << std::endl; + else + std::cout << s1.Str() << " > " << s2.Str() << std::endl; + + s1 = "BBB"; + if( s1 <= s2 ) + std::cout << s1.Str() << " <= " << s2.Str() << std::endl; + else + std::cout << s1.Str() << " > " << s2.Str() << std::endl; + + s1 = "CCC"; + if( s1 <= s2 ) + std::cout << s1.Str() << " <= " << s2.Str() << std::endl; + else + std::cout << s1.Str() << " > " << s2.Str() << std::endl; + + std::cout << std::endl << ">= operator tests" << std::endl; + s1 = "AAA"; + s2 = "BBB"; + + if( s1 >= s2 ) + std::cout << s1.Str() << " >= " << s2.Str() << std::endl; + else + std::cout << s1.Str() << " < " << s2.Str() << std::endl; + + s1 = "BBB"; + if( s1 >= s2 ) + std::cout << s1.Str() << " >= " << s2.Str() << std::endl; + else + std::cout << s1.Str() << " < " << s2.Str() << std::endl; + + s1 = "CCC"; + if( s1 >= s2 ) + std::cout << s1.Str() << " >= " << s2.Str() << std::endl; + else + std::cout << s1.Str() << " < " << s2.Str() << std::endl; + + std::cout << "(const char *) " << (const char *) s2.Str() << std::endl; + + std::cout << std::endl << "CountChar() test" << std::endl; + s1 = "ABADFDSGA"; + xbUInt32 i = s1.CountChar( 'A' ); + std::cout << "There are " << i << " 'A's in " << s1.Str() << std::endl; + + s1.Ltrunc( 4 ); + std::cout << "lTunc(4) test s1 = [" << s1.Str() << "]" << std::endl; + + std::cout << std::endl << "PutAt() test" << std::endl; + s1.PutAt( 3, 'Z' ); + std::cout << "Third char should be a 'Z' = " << s1.Str() << std::endl; + + std::cout << std::endl << "AddBackSlash() test" << std::endl; + s1.AddBackSlash( 'Z' ); + std::cout << "Should be a backslash before the 'Z' = " << s1.Str() << std::endl; + + std::cout << std::endl << "Assign() test" << std::endl; + s2 = "1234567890"; + std::cout << "s2 = " << s2.Str() << std::endl; + s1.Assign( s2, 4, 5 ); + std::cout << "assign( s2, 4, 5 ) results = " << s1.Str() << std::endl; + s1.Assign( s2, 4, 15 ); + std::cout << "assign( s2, 4, 15 ) results = " << s1.Str() << std::endl; + + s1.Assign( s2, 5 ); + std::cout << "Assign( s2, 5 ) results = " << s1.Str() << std::endl; + s1.Assign( s2, 15 ); + std::cout << "Assign( s2, 15 ) results = " << s1.Str() << std::endl; + + std::cout << std::endl << "s1.copy() test" << std::endl; + s1 = "ABC"; + std::cout << "s1 = " << s1.Str() << std::endl; + std::cout << "s2 = " << s2.Str() << std::endl; + + s1 = s2.Copy(); + s1.Dump( "s1.Copy() results" ); + + s1 = "0x35"; + char hexChar; + s1.CvtHexChar( hexChar ); + std::cout << "CvtHexChar test [" << s1.Str() << "] converts to [" << hexChar << "]" << std::endl; + s1 = "0x65"; + s1.CvtHexChar( hexChar ); + std::cout << "cvHexChar test [" << s1.Str() << "] converts to [" << hexChar << "]" << std::endl; + + s1 = "0x610x620x630x640x65"; + s1.CvtHexString( s2 ); + std::cout << "CvtHexString [" << s1.Str() << "] converts to [" << s2.Str() << "]" << std::endl; + + s1.ExtractElement( "aaaa|bbbb|cccc|dddd", '|', 2, 0 ); + s1.Dump( "ExtractElement() " ); + + s1 = "123"; + s2 = "ABC"; + std::cout << "HasAlphaChars( " << s1.Str() << " ) = " << s1.HasAlphaChars() << std::endl; + std::cout << "HasAlphaChars( " << s2.Str() << " ) = " << s2.HasAlphaChars() << std::endl; + + s2 = ""; + std::cout << "IsEmpty( " << s1.Str() << " ) = " << s1.IsEmpty() << std::endl; + std::cout << "IsEmpty( " << s2.Str() << " ) = " << s2.IsEmpty() << std::endl; + + s1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + std::cout << s1.Str() << " s1.Mid( 3,5 ) = [" << s1.Mid( 3, 5 ).Str() << "]" << std::endl; + std::cout << s1.Str() << " s1.Mid( 25, 10 ) = [" << s1.Mid( 25, 10 ).Str() << "]" << std::endl; + + std::cout << s1.Str() << " s1.Pos('G') = " << s1.Pos( 'G' ) << std::endl; + std::cout << s1.Str() << " s1.Pos(\"JKL\") = " << s1.Pos( "JKL" ) << std::endl; + + s1.Dump( "Remove( 3, 5 ) before " ); + std::cout << s1.Str() << " s1.Remove( 3, 5 ) = [" << s1.Remove( 3, 5 ).Str() << "]" << std::endl; + s1.Dump( "Remove( 3, 5 ) after " ); + s1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + + std::cout << "s1.Remove( 20, 10 ) = [" << s1.Remove( 20, 10 ).Str() << "]" << std::endl; + s1.Dump( "Remove( 20, 10 ) " ); + + s1.Sprintf( "%d", 12345 ); + s1.Dump( "Sprintf( %d, 12345 ) " ); + + s1.SetNum( (long) 123456 ); + std::cout << "s1.SetNum( 123456 ) = " << s1.Str() << std::endl; + + s1.Set( "Yet another way to set a string value" ); + s1.Dump( "Set" ); + + s1 = "ABCABCABZ"; + s1.Dump( "SwapChars( 'A', '9' ) before" ); + s1.SwapChars( 'A', '9' ); + s1.Dump( "SwapChars( 'A', '9' ) after"); + + s1.ToLowerCase(); + s1.Dump( "ToLowerCase" ); + + s1.ToUpperCase(); + s1.Dump( "ToUpperCase" ); + + s1.ZapChar( '9' ); + s1.Dump( "ZapChar( '9' )" ); + + s1.ZapLeadingChar( 'B' ); + s1.Dump( "ZapLeadingChar( 'B' )" ); + + s1.ZapTrailingChar( 'Z' ); + s1.Dump( "ZapLeadingChar( 'Z' )" ); + + s1 = "123"; + s1.PadLeft( '0', 9 ); + s1.Dump( "s1.PadLeft('0', 9 ) "); + + s1 = "abc"; + s1.PadRight( 'Z', 9 ); + s1.Dump( "s1.PadRight('Z', 9 ) "); + + xbString sNullString; + if( sNullString.IsNull()) + std::cout << "sNullString is null" << std::endl; + else + std::cout << "sNullString is not null" << std::endl; + + xbString tstS( "ZZZZZZZZZ" ); + tstS = s1.Left( 5 ); + + std::cout << "tstS = " << tstS.Str() << "\n"; + std::cout << "s1 = " << s1.Str() << "\n"; + + tstS = "1234567890"; + std::cout << "mid result = " << tstS.Mid( 3, 3 ).Str() << std::endl; + + tstS = "1234567890"; + std::cout << "left result = " << tstS.Left( 3 ).Str() << std::endl; + + + return 0; +} -- cgit v1.2.3