14 #define pr_str( NAME ) printf( "(%d)->" #NAME " == \"%s\"[%d]\n" ,\
15 __LINE__, (char*)(NAME), strlen(NAME) )
16 #define pr_num( NAME ) printf( "(%d)->" #NAME " == %d\n" , __LINE__, NAME )
20 int eqstr(
const char* a,
const char* b ) {
21 return 0==strcmp(a,b);
29 ztrcpy(
sizeof(s10),s10 ,
"123456789.abcdefghi.");
32 #define ZS(x) sizeof(x),x
34 ztrcpy(
ZS( s10 ),
"123456789.abcdefghi.");
39 if (
"BLOCKIFY this to avoid problems when MOVING code elsewhere") {
42 #define ZS(x) sizeof(x),x
46 #define ztrcpy( dest, src) strcpy(dest,src)
48 #define ztrcat( dest, src) strcat(dest,src)
60 ztrcpy(
sizeof(s1),s1,
"12...." );
65 ztrcpy(
sizeof(s2),s2,
"12...." );
70 ztrcpy(
sizeof(s3),s3,
"12...." );
75 ztrcpy(
sizeof(s5),s5,
"12345...." );
80 ztrcpy(
sizeof(s5),s5,
"123" );
85 ztrcpy(
sizeof(s5),s5,
"12" );
90 ztrcpy(
sizeof(s5),s5,
"1" );
95 ztrcpy(
sizeof(s15),s15,
"12345...." );
100 ztrcpy( 5,s15,
"12345...." );
109 char s10[9+1]; s10[0] = 0;
110 ztrcat(
sizeof(s10),s10 ,
"123456789.abcdefghi.");
113 s10[3] =
'.'; s10[3+1] = 0;
115 ztrcat(
sizeof(s10),s10 ,
"123456789.abcdefghi.");
118 #define ZS(x) sizeof(x),x
120 s10[3] =
'.'; s10[3+1] = 0;
122 ztrcat(
ZS(s10),
"123456789.abcdefghi.");
129 ztrcpy(
sizeof(s5),s5,
"1234" );
130 ztrcat(
sizeof(s5),s5,
"56" );
135 ztrcpy(
sizeof(s5),s5,
"123" );
136 ztrcat(
sizeof(s5),s5,
"456" );
141 ztrcpy(
sizeof(s5),s5,
"12" );
142 ztrcat(
sizeof(s5),s5,
"3456" );
147 ztrcpy(
sizeof(s5),s5,
"1" );
148 ztrcat(
sizeof(s5),s5,
"23456" );
153 ztrcpy(
sizeof(s5),s5,
"" );
154 ztrcat(
sizeof(s5),s5,
"123456" );
159 ztrcpy(
sizeof(s15),s15,
"" );
160 ztrcat( 5 ,s15,
"123456" );
162 ztrcpy(
sizeof(s15),s15,
"abcdefghijlmnopqrstuvwxyz" );
163 ztrcat( 5 ,s15,
"123456" );
172 ztrcpy(
sizeof(s30),s30,
"====!-----+.........+" );
173 { {
ztrins(
sizeof(s30),s30, 4,
"_2_4_"); } }
176 assertTrue( 26 == strlen(
"====_2_4_!-----+.........+") );
177 { { {
assertTrue( 21+strlen(
"_2_4_") == strlen(s30) ); } } }
180 char *p;
char poem[] =
"JIM, JIM, JIM ... Where are you?";
181 while ( 0!=(p=strstr(poem,
"JIM")) ) {
182 strdel( p, strlen(
"JIM") );
183 ztrins(
sizeof(poem),poem, p-poem,
"ROMEO" );
188 assertTrue( strlen(
"ROMEO, ROMEO, ROMEO ... Where ar") ==
189 strlen(
"JIM, JIM, JIM ... Where are you?") );
192 ztrcpy(
sizeof(s30),s30,
"====!-----+.........+" );
193 { {
ztrins(
sizeof(s30),s30, 00,
"________18________"); } }
197 ztrcpy(
sizeof(s30),s30,
"0123456789" );
198 { {
ztrins( 1,s30, 0,
"" ); } }
204 char *p;
char poem[] =
"ROMEO, ROMEO, ROMEO ... Where are you?";
205 while ( 0!=(p=strstr(poem,
"ROMEO")) ) {
206 strdel( p, strlen(
"ROMEO") );
207 ztrins(
sizeof(poem),poem, p-poem,
"JIM" );
213 ztrcpy(
sizeof(s30),s30,
"====!-----+.........+" );
214 { {
ztrins(
sizeof(s30),s30, 20,
"________18________"); } }
218 ztrcpy(
sizeof(s30),s30,
"====!-----+.........+" );
219 { {
ztrins( 1,s30, 0,
"" ); } }
222 ztrcpy(
sizeof(s30),s30,
"====!-----+.........+" );
223 { {
ztrins( 1,s30, 00,
"________18________"); } }
226 ztrcpy(
sizeof(s30),s30,
"====!-----+.........+" );
227 { {
ztrins( 1,s30, 0,
"" ); } }
232 ztrcpy(
sizeof(s30),s30,
"0123!-----+.........+" );
233 {{{
ztrins(
sizeof(s30),s30, 00,
"_2__5_"); }}}
238 {{{
ztrins(
sizeof(s30),s30, 10,
"" ); }}}
241 {{{
ztrins(
sizeof(s30),s30, strlen(s30),
"?"); }}}
246 ztrcpy(
sizeof(s30),s30,
"0123!-----+.........+" );
247 {{{
ztrins(
sizeof(s30),s30, 00,
"________18________"); }}}
254 char str[2];
char s30[30];
char t30[30];
size_t i;
258 for ( i=0; i<30-1; ++i ) {
260 ztrcat(
sizeof(s30),s30, str);
261 ztrins(
sizeof(t30),t30, strlen(t30), str );
269 ztrcpy(
sizeof(s1),s1,
"12" );
270 ztrins(
sizeof(s1),s1, 0,
"a..." );
272 ztrins(
sizeof(s1),s1, 1,
"a..." );
274 ztrins(
sizeof(s1),s1, 7,
"a..." );
281 ztrcpy(
sizeof(s2),s2,
"12" );
282 ztrins(
sizeof(s2),s2, 0,
"a..." );
286 ztrcpy(
sizeof(s2),s2,
"12" );
288 ztrins(
sizeof(s2),s2, 1,
"a..." );
292 ztrcpy(
sizeof(s2),s2,
"12" );
293 ztrins(
sizeof(s2),s2, 2,
"a..." );
300 ztrcpy(
sizeof(s3),s3,
"12" );
302 ztrins(
sizeof(s3),s3, 0,
"a" );
306 ztrcpy(
sizeof(s3),s3,
"12" );
307 ztrins(
sizeof(s3),s3, 0,
"ab" );
311 ztrcpy(
sizeof(s3),s3,
"12" );
312 ztrins(
sizeof(s3),s3, 0,
"ab...." );
316 ztrcpy(
sizeof(s3),s3,
"12" );
317 ztrins(
sizeof(s3),s3, 1,
"b" );
321 ztrcpy(
sizeof(s3),s3,
"12" );
322 ztrins(
sizeof(s3),s3, 1,
"bc" );
326 ztrcpy(
sizeof(s3),s3,
"12" );
327 ztrins(
sizeof(s3),s3, 1,
"bc...." );
331 char s15[15];
size_t i;
332 ztrcpy(
sizeof(s15),s15,
"" );
333 ztrins( 5 ,s15, 2,
"123456" );
335 ztrins( 5 ,s15, 1,
"123456" );
337 ztrins( 5 ,s15, 0,
"123456" );
340 ztrcpy(
sizeof(s15),s15,
"abcdefghijklmnopqrstuvwxyz" );
344 ztrcpy(
sizeof(s15),s15,
"abcdefghijklmnopqrstuvwxyz" );
346 ztrins( 5 ,s15, 2,
"123456" );
349 ztrcpy(
sizeof(s15),s15,
"abcdefghijklmnopqrstuvwxyz" );
351 ztrins( 5 ,s15, 22,
"123456" );
354 for ( i=0; i<35; ++i ) {
355 ztrcpy(
sizeof(s15),s15,
"abcdefghijklmnopqrstuvwxyz" );
357 ztrins( 5 ,s15, (5-1)+i,
"123456" );
364 char *p;
char s20[20];
365 ztrcpy(
sizeof(s20),s20,
"string INS tail" );
366 p = strstr( s20,
"rin");
369 {{{
ztrins(
sizeof(s20),s20, p-s20 , p ); }}}
373 ztrsub(
sizeof(t20),t20, p, UINT_MAX );
374 {{{
ztrins(
sizeof(s20),s20, p-s20 , p ); }}}
376 assertTrue( 19==strlen(
"string INS tailring") );
385 ztrcpy(
sizeof(s20),s20,
"string INS tail" );
386 p = strstr( s20,
"INS");
389 {{{
ztrins(
sizeof(s20),s20, p-s20 , p ); }}}
391 assertTrue( 19==strlen(
"string INS tailINS ") );
398 char t[55];
char sub[55];
399 char ABC[] =
"abcdefghijklmno123456789";
400 size_t N,i,j; N = strlen( ABC );
401 ztrcpy(
sizeof(s),s, ABC );
403 for ( i=0; i<N; ++i ) {
404 for ( j=0; j<N; ++j ) {
405 ztrcpy(
sizeof(s),s, ABC );
406 ztrcpy(
sizeof(t),t, ABC );
408 ztrsub(
sizeof(sub),sub, p , UINT_MAX );
409 {{{
ztrins(
sizeof(s),s, j , p ); }}}
410 {{{
ztrins(
sizeof(t),t, j , sub ); }}}
414 for ( j=0; j<N; ++j ) {
415 ztrcpy( strlen(ABC)/2,s, ABC );
416 ztrcpy( strlen(ABC)/2,t, ABC );
418 ztrsub(
sizeof(sub),sub, p , UINT_MAX );
419 {{{
ztrins(
sizeof(s),s, j , p ); }}}
420 {{{
ztrins(
sizeof(t),t, j , sub ); }}}
426 #define ZS(x) sizeof(x),x
428 size_t i,j, N=
sizeof(s30);
429 for ( i=0 ; i<N; ++i ) {
431 for ( j=0 ; j<i; ++j ) {
432 ztrins(
ZS(s30), strlen(s30),
"?" );
437 for ( j=0 ; j<i; ++j ) {
443 for ( j=0 ; j<i/2; ++j ) {
444 ztrins(
ZS(s30), strlen(s30)/3,
"?" );
445 ztrins(
ZS(s30), strlen(s30)/5,
"!" );
450 for ( j=0 ; j<i/3; ++j ) {
451 ztrins(
ZS(s30), strlen(s30)/7,
"?" );
452 ztrins(
ZS(s30), strlen(s30)/2,
"&!" );
461 ztrcpy(
sizeof(s12),s12,
"mismo");
464 ztrins(
sizeof(s12),s12, 0, s );
465 if ( strlen(s12) ==
sizeof(s12)-1 ) {
break; }
473 char s10[9+1];
char *p;
assertTrue(
sizeof(s10)>9 );
475 ztrcpy(
sizeof(s10),s10,
"123456789.123456789.abcde" );
481 ztrcpy(
sizeof(s10),s10,
"1234.abcd.1234.abcd" );
482 if ( NULL!=(p=strchr(s10,
'.')) ) { {
strdel( s10 , 1+(p-s10) ); } }
488 ztrcpy(
sizeof(s10),s10,
"0123..678" );
489 { {
strdel( s10+4 , 2 ); } }
496 ztrcpy(
sizeof(s10),s10,
"123456789.123456789.abcde" );
501 ztrcpy(
sizeof(s10),s10,
"123456789.123456789.abcde" );
506 ztrcpy(
sizeof(s10),s10,
"123456789.123456789.abcde" );
507 strdel( s10, strlen(s10) );
513 ztrcpy(
sizeof(s10),s10, NULL );
523 char str[] =
".123456789.abcdefghi.";
525 assertTrue( strlen(str) == 21 &&
sizeof(sub)==22 );
527 ztrsub(
sizeof(sub),sub , str,5 );
531 ztrsub(
sizeof(sub),sub , str,UINT_MAX );
535 ztrsub(
sizeof(sub),sub , str+10,7 );
541 ztrsub(
sizeof(sub),sub , str,000 );
548 ztrsub(
sizeof(s3),s3,
".123456789.abcdefghi."+(7), 800 );
556 char s[] =
" la mona es una loca ";
559 char t[] =
"XXXla mona es una loca ";
565 char s[] =
"la mona loca";
574 char s[] =
" la mona es una loca ";
577 char t[] =
" la mona es una loca XXX";
583 char t[] =
" la mona es una loca ";
602 char s[] =
" "; s[0] = 0;
617 char s[] =
" "; s[0]=0;
632 char s[] =
" "; s[0]=0;
646 char s[] =
"1234567" ;
663 char s[] =
" la mona es una loca ";
671 char m30[30];
size_t n;
672 strcpy( m30,
"-!--!--!-" );
assertTrue( strlen(m30)==9 );
674 n =
memczap(strlen(m30),m30,
'-' );
676 assertTrue( 0==memcmp(m30,
"!!!", strlen(
"!!!") ) );
682 strcpy( m30,
"(*:**-*)" );
assertTrue( strlen(m30)==8 );
684 n =
memczap(strlen(m30),m30,
'*');
686 assertTrue( 0==memcmp(m30,
"(:-)", strlen(
"(:-)") ) );
687 assertTrue( 0==memcmp(m30,
"(:-)*-*)", strlen(m30) ) );
689 strcpy( m30,
"*:**-*" );
assertTrue( strlen(m30)==6 );
691 n =
memczap(strlen(m30),m30,
'*');
693 assertTrue( 0==memcmp(m30,
":-", strlen(
":-") ) );
700 strcpy( m30,
"*:**-*" );
703 memset( m30,
'!',
sizeof(m30) );
704 assertTrue( 0==memcmp(m30,
"!!!" "!!!",3+3) );
709 assertTrue( 0==memcmp(m30,
" \0!" "!!!",3+3) );
714 assertTrue( 0==memcmp(m30,
" \0" "!!!",3+3) );
716 memset( m30, 0,
sizeof(m30) );
719 char m[] =
"123456789" "\0" "123456789" "\0";
722 memcpy( m30, m,
sizeof(m) );
725 memcpy( m30, m,
sizeof(m) );
757 size_t leading_digits =
strrspn(
"123456789.123456789.",
'0',
'9' );
769 char Atilde[] =
"ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ";
770 char Asimple[] =
"AAAAAAECEEEEIIIIDNOOOOOx0UUUUYPsaaaaaaeceeeeiiiidnooooo/0uuuuypy";
772 char Astr[
sizeof(Atilde) ];
773 strcpy( Astr, Atilde );
778 char Atilde[] =
"ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ";
779 char Asimple[] =
"AAAAAAECEEEEIIIIDNOOOOOx0UUUUYPsaaaaaaeceeeeiiiidnooooo/0uuuuypy";
792 const char *Atilde =
"ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ";
793 const char *Asimple =
"AAAAAAECEEEEIIIIDNOOOOOx0UUUUYPsaaaaaaeceeeeiiiidnooooo/0uuuuypy";
795 char Astr[ 255-192+1 ]; *Astr = 0;
796 strcpy( Astr, Atilde );
812 const char *Atilde =
"ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ";
813 const char *Asimple =
"AAAAAAECEEEEIIIIDNOOOOOx0UUUUYPsaaaaaaeceeeeiiiidnooooo/0uuuuypy";
816 char Astr[ 255-192+1 ];
818 strcpy( Astr, Atilde );
836 ztrcpy(
sizeof(dest),dest,
"012345" );
838 ztrins( 1,dest, 0,
"abc" );
842 ztrcpy(
sizeof(dest),dest,
"012345" );
844 ztrins( 3,dest, 0,
"abc" );
851 ztrcpy(
sizeof(dest),dest,
"012345" );
853 ztrcpy( (001)*(001),dest,
"abc" );
857 ztrcat(
sizeof(dest),dest,
"012345" );
859 ztrcat( (001)*(001),dest,
"abc" );
863 ztrcpy(
sizeof(dest),dest,
"012345" );
865 ztrins( (001)*(001),dest, 0,
"abc" );
869 ztrcpy(
sizeof(dest),dest,
"012345" );
871 ztrsub( (001)*(001),dest,
"abc",0 );
878 s1[0]=
'?';
ztrcpy(
sizeof(s1),s1,
"1234" );
880 s1[0]=
'?';
ztrcat(
sizeof(s1),s1,
"1234" );
883 s1[0]=
'?';
ztrins(
sizeof(s1),s1, 0,
"1234" );
886 s1[0]=
'?';
ztrsub(
sizeof(s1),s1,
"1234",17 );
892 memset( s2,
'?',
sizeof(s2) );
893 ztrcpy(
sizeof(s2),s2,
"1234" );
896 memset( s2,
'?',
sizeof(s2) );
897 ztrcat(
sizeof(s2),s2,
"1234" );
900 memset( s2,
'?',
sizeof(s2) );
901 ztrins(
sizeof(s2),s2, 0,
"1234" );
904 memset( s2,
'?',
sizeof(s2) );
905 ztrsub(
sizeof(s2),s2,
"1234",17 );
911 memset( s3,
'?',
sizeof(s3) );
912 ztrcpy(
sizeof(s3),s3,
"1234" );
915 memset( s3,
'?',
sizeof(s3) );
916 ztrcat(
sizeof(s3),s3,
"1234" );
919 memset( s3,
'?',
sizeof(s3) );
920 ztrins(
sizeof(s3),s3, 0,
"1234" );
923 memset( s3,
'?',
sizeof(s3) );
924 ztrsub(
sizeof(s3),s3,
"1234",17 );
930 memset( s7,
'?',
sizeof(s7) );
931 ztrcpy(
sizeof(s7),s7,
"1234abcd" );
934 memset( s7,
'?',
sizeof(s7) );
935 ztrcat(
sizeof(s7),s7,
"1234abcd" );
938 memset( s7,
'?',
sizeof(s7) );
939 ztrins(
sizeof(s7),s7, 0,
"1234abcd" );
942 memset( s7,
'?',
sizeof(s7) );
943 ztrsub(
sizeof(s7),s7,
"1234abcd",17 );
963 char CHK[] =
"Chuck";
964 char chk[] = {
'C',
'h',
'u',
'c',
'k', 0 };
969 #define DIM(V) (sizeof(V) / sizeof(*V))
978 printf(
"test_ztring.c [uUnit.h] with CHAR_BIT==%d\n", CHAR_BIT );
980 #define do_test( NAME ) printf( "test->" #NAME "()\n" ); test_##NAME()
size_t memczap(size_t size, void *mem, int ch)
Removes every ocurrence of 'ch' from 'mem'.
void test_strdel()
test->strdel()
char * strtrim(char *str, char tr)
return strrtrim( strltrim(s,tr),tr ).
char * strxacct(char *str)
Uses strxltn1() to convert all letters in 'str'.
#define assertTrue(cond)
(cond ? () : cout << "cond" )
void test_strrtrim()
test->strrtrim()
void test_strtrim()
test->strtrim()
int strsffx(const char *str, const char *suffix)
Returns '1' if 'suffix' is a suffix of 'str'.
#define ztrcat(dest, src)
void test_ztrins()
test->ztrins()
size_t strrspn(const char *str, char a, char z)
Get span until character in character range '[a..z]'.
A few string functions to enhance C's <string.h> library.
int eqstr(const char *a, const char *b)
a==b?
void test_ztrcpy()
test->ztrcpy()
#define assertFalse(cond)
(!(cond) ? () : cout << "!" << (cond)" )
char * strrtrim(char *str, char tr)
Removes from 'str' all trailing characters that are equal to 'tr'.
void test_strxltn1()
test->strxltn1()
void test_ztrcat()
test->ztrcat()
void test_strsffx()
test->strsffx()
void test_memczap()
Test->memczap()
void test_subindex()
test -> "string"[0] == 's' ???
char * strdel(char *dest, size_t len)
Deletes the leading 'len' characters from 'str'.
char * strltrim(const char *str, char tr)
Returns a pointer to the first character in 'str' different from 'tr'.
char * ztrsub(size_t size, char *dest, const char *src, size_t len)
Copies the first 'len' characters from 'src' to 'dest'.
void test_zsize1()
test -> (size==1) ==> *dest=0 forall( ztr???() )
void test_strpfx()
test->strpfx()
int strpfx(const char *str, const char *prefix)
Returns '1' if 'prefix' is a prefix of 'str'.
void test_strltrim()
test->strltrim()
char strxltn1(char accented_latin_1)
Translates characters in range [192<–>192+63] into letters or ASCII symbols that look similar...
#define ztrcpy(dest, src)
char * ztrins(size_t size, char *dest, size_t i, const char *insert)
Inserts string 'insert' into 'dest' at position 'n'.
void test_strrspn()
test->strrspn()
[u]Micro module for [Unit] program testing.
void test_ztrsub()
test->ztrsub()