6 #define ALTSTRING_SEPERATOR1 ";"
7 #define ALTSTRING_SEPERATOR2 ":"
8 #define ALTSTRING_ESCAPE "\\"
10 void() Util_AltString_UnitTest =
14 local zoned lTestCount;
16 lTestCount = String_Zone( "normal;use escape \\; ;another escape \\; ;" );
17 lCount = Util_GetAltStringCount( lTestCount );
18 print( "'", lTestCount, "' <- item count: ", ftos( lCount ), "\n" );
22 for( lIndex = 0 ; lIndex <= lCount ; lIndex++ ) {
24 lStart = Util_GetAltStringItemStart( lTestCount, lIndex );
25 print( "Start of item ", ftos( lIndex ), " at ", ftos( lStart ), "\n" );
31 for( lIndex = 0 ; lIndex <= lCount ; lIndex++ ) {
33 lItem = Util_GetAltStringItem( lTestCount, lIndex );
34 print( "Item #", ftos( lIndex ), ": ", lItem, "\n" );
39 String_Free( lTestCount );
46 lTest = String_Zone( "alt1;alt2;alt3;" );
49 for( lIndex = 0 ; lIndex <= lCount * 2; lIndex = lIndex + 2 ) {
50 lTest = Util_InsAltStringItem( lTest, lIndex, ftos( lIndex ) );
51 print( lTestCount, "\n" );
54 for( lIndex = 0 ; lIndex <= lCount * 2; lIndex = lIndex + 2 ) {
55 lTest = Util_SetAltStringItem( lTest, lIndex, strcat( "#", ftos( lIndex ) ) );
56 print( lTestCount, "\n" );
59 for( lIndex = 0 ; lIndex <= lCount ; lIndex = lIndex + 1 ) {
60 lTest = Util_DelAltStringItem( lTest, lIndex );
61 print( lTestCount, "\n" );
65 print( "\nTest AltString(Un)Prepare\n" );
69 lTest = "\\;\\a\\b\\\\ \\;";
70 print( "'", lTest, "'" );
71 lTest = Util_AltStringPrepare( lTest );
72 print( "->'", lTest, "'" );
73 lTest = Util_AltStringUnprepare( String_Normal( lTest ) );
74 print( "\n->'", lTest, "'\n" );
78 // TODO: add stack unit test
81 float( zoned pAlt ) Util_GetAltStringCount =
83 local float lPosition, lLength;
87 for( lPosition = 0, lLength = strlen( pAlt ) ; lPosition < lLength ; lPosition++ ) {
89 lChar = substring( pAlt, lPosition, 1 );
90 if( lChar == ALTSTRING_SEPERATOR1 || lChar == ALTSTRING_SEPERATOR2 ) {
92 } else if( lChar == ALTSTRING_ESCAPE ) {
100 float( zoned pAlt, float pIndex ) Util_GetAltStringItemStart =
102 local float lPosition, lLength;
106 for( lPosition = 0, lLength = strlen( pAlt ) ; lPosition < lLength && lCount < pIndex ; lPosition++ ) {
108 lChar = substring( pAlt, lPosition, 1 );
109 if( lChar == ALTSTRING_SEPERATOR1 || lChar == ALTSTRING_SEPERATOR2 ) {
111 } else if( lChar == ALTSTRING_ESCAPE ) {
118 zoned( zoned pAlt, float pIndex ) Util_GetAltStringItem =
120 local float lStart, lEnd;
123 lStart = Util_GetAltStringItemStart( pAlt, pIndex );
124 lEnd = Util_GetAltStringItemStart( pAlt, pIndex + 1 ) - 2;
126 lItem = substring( pAlt, lStart, lEnd - lStart + 1 );
127 return Util_AltStringUnprepare( lItem );
130 zoned( zoned pAlt, float pIndex, string pSet ) Util_SetAltStringItem =
132 local float lStart, lEnd;
136 pSet = String_Zone( pSet );
138 lStart = Util_GetAltStringItemStart( pAlt, pIndex );
139 lEnd = Util_GetAltStringItemStart( pAlt, pIndex + 1 ) - 1; // include the seperator
141 lResult = String_Substring( pAlt, 0, lStart - 1 + 1 );
143 lItem = String_Normal( Util_AltStringPrepare( pSet ) );
144 lResult = String_Append( lResult, lItem );
146 lResult = String_Append( lResult, substring( pAlt, lEnd, INFINITY ) );
150 return String_Set( pAlt, String_Normal( lResult ) );
153 zoned( zoned pAlt, float pIndex ) Util_DelAltStringItem =
155 local float lStart, lEnd;
158 lStart = Util_GetAltStringItemStart( pAlt, pIndex );
159 lEnd = Util_GetAltStringItemStart( pAlt, pIndex + 1 );
161 lResult = String_Substring( pAlt, 0, lStart - 1 + 1 );
162 lResult = String_Append( lResult, substring( pAlt, lEnd, INFINITY ) );
164 return String_Set( pAlt, String_Normal( lResult ) );
167 // insert a new item after the indexed (to insert an item at the front use -1)
168 zoned( zoned pAlt, float pIndex, string pSet ) Util_InsAltStringItem =
174 pSet = String_Zone( pSet );
176 lStart = Util_GetAltStringItemStart( pAlt, pIndex );
177 lResult = String_Substring( pAlt, 0, lStart - 1 + 1 );
179 lItem = String_Normal( Util_AltStringPrepare( pSet ) );
180 lResult = String_Append( lResult, lItem );
182 lResult = String_Append( lResult, ";" );
183 lResult = String_Append( lResult, substring( pAlt, lStart, INFINITY ) );
187 return String_Set( pAlt, lResult );
190 zoned( string pString ) Util_AltStringPrepare =
192 local float lPosition, lLength;
196 lIn = String_Zone( pString );
198 for( lPosition = 0, lLength = strlen( pString ) ; lPosition < lLength ; lPosition++ ) {
200 lChar = substring( lIn, lPosition, 1 );
202 if( lChar == ALTSTRING_SEPERATOR1 || lChar == ALTSTRING_SEPERATOR2 || lChar == ALTSTRING_ESCAPE ) {
203 lOut = strcat( lOut, "\\", lChar );
205 lOut = strcat( lOut, lChar );
210 return String_Zone( lOut );
213 zoned( string pString ) Util_AltStringUnprepare =
215 local float lPosition, lLength;
219 lIn = String_Zone( pString );
221 for( lPosition = 0, lLength = strlen( pString ) ; lPosition < lLength ; lPosition++ ) {
223 lChar = substring( lIn, lPosition, 1 );
225 if( lChar == ALTSTRING_ESCAPE ) {
227 lChar = substring( lIn, lPosition, 1 );
230 lOut = strcat( lOut, lChar );
234 return String_Zone( lOut );
237 zoned( zoned pAlt, string pPush ) Util_AltStringPush =
239 local zoned lPrepared;
241 lPrepared = Util_AltStringPrepare( pPush );
242 pAlt = String_Set( pAlt, strcat( lPrepared, ";", pAlt ) );
243 String_Free( lPrepared );
248 zoned( zoned pAlt, string pPush ) Util_AltStringPushBack =
250 local zoned lPrepared;
252 lPrepared = Util_AltStringPrepare( pPush );
253 pAlt = String_Set( pAlt, strcat( pAlt, pPush, ";" ) );
254 String_Free( lPrepared );
259 zoned( zoned pAlt ) Util_GetAltStringTop =
261 return Util_GetAltStringItem( pAlt, 0 );
264 zoned( zoned pAlt ) Util_AltStringPop =
267 lCount = Util_GetAltStringCount( pAlt );
268 return Util_DelAltStringItem( pAlt, lCount - 1 );
275 Util_GetAltStringCount
278 float( string pStr ) Util_GetAltStringCount =
282 local float lPosition;
283 local string lLetter;
285 lPosition = lCount = 0;
286 for( lLength = strlen( pStr ) ; lPosition < lLength ; lPosition++ ) {
287 lLetter = substring( pStr, lPosition, 1 );
290 else if( lLetter == "\\" )
294 return floor( lCount / 2 );
299 Util_GetAltStringItem
302 string( string pStr, float pCount ) Util_GetAltStringItem =
305 local float lPosition;
307 local string lLetter;
310 pCount = pCount*2 + 1; // number of ' until item starts
311 lCount = lPosition = 0;
312 for( lLength = strlen( pStr ) ; lPosition < lLength && lCount < pCount ; lPosition++ ) {
313 lLetter = substring( pStr, lPosition, 1 );
317 else if( lLetter == "\\" )
321 if( lCount != pCount )
322 return String_Create();
324 for( lOut = "" ; lPosition < lLength ; lPosition++ ) {
325 lLetter = substring( pStr, lPosition, 1 );
329 else if( lLetter == "\\" ) {
331 if( lPosition >= lLength )
333 lLetter = substring( pStr, lPosition, 1 );
336 // no need for strzone since there are 16 buffers and we use 2 in the for
337 lOut = strcat( lOut, lLetter );
340 return String_Zone( lOut ); // return a strzoned string
343 float( string pStr ) Util_GetAltStringCount =
345 return altstr_count( pStr );
347 string( string pStr, float pCount ) Util_GetAltStringItem =
349 return String_Zone( altstr_get( pStr, pCount ) );
356 Util_SetAltStringItem
360 string( string pAlt, float pIndex, string pSet ) Util_SetAltStringItem =
363 local float lPosition;
366 local string lLetter;
369 pSet = String_Zone( pSet );
371 pIndex = pIndex*2 + 1; // number of ' until item starts
372 lCount = lPosition = 0;
373 for( lLength = strlen( pAlt ) ; lPosition < lLength && lCount < pIndex ; lPosition = lPosition + 1 ) {
374 lLetter = substring( pAlt, lPosition, 1 );
378 else if( lLetter == "\\" )
379 lPosition = lPosition + 1;
382 if( lCount != pIndex )
386 // find the end of it
387 for( ; lPosition < lLength ; lPosition++ ) {
388 lLetter = substring( pAlt, lPosition, 1 );
392 else if( lLetter == "\\" )
393 lPosition = lPosition + 1;
396 lOut = String_Substring( pAlt, 0, lStart );
397 pSet = String_Normal( Util_AltStringPrepare( String_Normal( pSet ) ) );
398 lOut = strcat( lOut, pSet, substring( pAlt, lPosition, lLength - lPosition ) );
400 return String_Set( pAlt, lOut );
403 string( string pAlt, float pIndex, string pSet ) Util_SetAltStringItem =
405 return String_Set( pAlt, altstr_set( pAlt, pIndex, pSet ) );
411 Util_DelAltStringItem
414 string( string pAlt, float pIndex ) Util_DelAltStringItem =
417 local float lPosition;
420 local string lLetter;
423 pIndex = pIndex*2 + 1; // number of ' until item starts
424 lCount = lPosition = 0;
425 for( lLength = strlen( pAlt ) ; lPosition < lLength && lCount < pIndex ; lPosition = lPosition + 1 ) {
426 lLetter = substring( pAlt, lPosition, 1 );
430 else if( lLetter == "\\" )
431 lPosition = lPosition + 1;
434 if( lCount != pIndex )
438 // find the end of it
439 for( ; lPosition < lLength ; lPosition++ ) {
440 lLetter = substring( pAlt, lPosition, 1 );
444 else if( lLetter == "\\" )
445 lPosition = lPosition + 1;
449 lOut = substring( pAlt, 0, lStart - 1 );
450 if( lPosition < lLength - 1)
451 lOut = strcat( lOut, substring( pAlt, lPosition + 1, lLength - lPosition - 1) );
453 return String_Set( pAlt, lOut );
458 Util_InsAltStringItem
462 string( string pAlt, float pIndex, string pSet ) Util_InsAltStringItem =
465 local float lPosition;
467 local string lLetter;
470 pSet = String_Zone( pSet );
472 pIndex = pIndex*2 + 2; // number of ' until item starts
473 lCount = lPosition = 0;
474 for( lLength = strlen( pAlt ) ; lPosition < lLength && lCount < pIndex ; lPosition = lPosition + 1 ) {
475 lLetter = substring( pAlt, lPosition, 1 );
479 else if( lLetter == "\\" )
480 lPosition = lPosition + 1;
483 if( lCount != pIndex )
486 lOut = String_Substring( pAlt, 0, lPosition );
487 pSet = String_Normal( Util_AltStringPrepare( String_Normal( pSet ) ) );
488 lOut = strcat( lOut, "'", pSet, "'", substring( pAlt, lPosition, lLength - lPosition ) );
490 return String_Set( pAlt, lOut );
493 string( string pAlt, float pIndex, string pSet ) Util_InsAltStringItem =
495 return String_Set( pAlt, altstr_ins( pAlt, pIndex, pSet ) );
501 Util_AltStringPrepare
505 string( string pString ) Util_AltStringPrepare =
507 local string lOut, lChar;
508 local float lPos, lLength;
510 pString = String_Zone( pString );
513 lLength = strlen( pString );
514 for( lPos = 0; lPos < lLength ; lPos = lPos + 1 ) {
515 lChar = substring( pString, lPos, 1 );
518 lOut = strcat( lOut, lChar );
521 String_Free( pString );
522 return String_Zone( lOut );
525 string( string pString ) Util_AltStringPrepare =
527 return String_Zone( altstr_prepare( pString ) );
536 string( string pAlt, string pPush ) Util_AltStringPush =
538 return String_Set( pAlt, strcat( "'", altstr_prepare( pPush ), "'", pAlt ) );
543 Util_AltStringPushBack
546 string( string pAlt, string pPush ) Util_AltStringPushBack =
548 return String_Set( pAlt, strcat( pAlt, "'", altstr_prepare( pPush ), "'" ) );
556 string( string pAlt ) Util_GetAltStringTop =
558 return Util_GetAltStringItem( pAlt, 0 );
566 string( string pAlt ) Util_AltStringPop =
568 local float lPos, lCount, lLength;
572 lLength = strlen( pAlt );
573 for( lPos = 0 ; lPos < lLength && lCount < 2 ; lPos++) {
574 lChar = substring( pAlt, lPos, 1 );
577 else if( lChar == "'" )
581 return String_Set( pAlt, substring( pAlt, lPos, lLength - lPos ) ); // no - 1 because this time we dont break after the '