summaryrefslogtreecommitdiff
path: root/src/examples/xb_ex_string.cpp
blob: 3d582a10e44b18acf16bc61af080f6a7dd75b3c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
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;
}