/* 1.0.3 is buggy */ size_t u_cspn_chr_rev(const struct Ustr *s1, size_t off, char chr) { size_t f_pos = ustr_srch_chr_rev(s1, off, chr); if (!f_pos) return (ustr_len(s1) - off); return ((ustr_len(s1) - f_pos) - off); }
static void tst_ustr(const char *tst, size_t num, size_t rb, int exact) { struct Ustr *s1 = ustr_dupx_cstr(rb, exact, 0, tst); /* NOTE: Using system *printf, so can't use %zu as Solaris is retarded */ printf("\t Ustr%lu(%lu, %s) %s= (%8lu / %-8lu = %5.2f%% )\n", CLU(num), CLU(rb), exact ? "TRUE" : "FALSE", exact ? " " : "", CLU(ustr_overhead(s1) + ustr_size(s1)), CLU(ustr_overhead(s1) + ustr_len(s1)), (100. * (ustr_overhead(s1) + ustr_size(s1))) / ustr_len(s1)); ustr_sc_free(&s1); }
static const char * extract_context(Ustr *line) { const char whitespace[] = " \t\n"; size_t off, len; /* check for trailing whitespace */ off = ustr_spn_chrs_rev(line, 0, whitespace, strlen(whitespace)); /* find the length of the last field in line */ len = ustr_cspn_chrs_rev(line, off, whitespace, strlen(whitespace)); if (len == 0) return NULL; return ustr_cstr(line) + ustr_len(line) - (len + off); }
static int u_basename(struct Ustr **ps1) { size_t off = 0; size_t len = ustr_len(*ps1); size_t llen = 0; if (!len) return (ustr_set(ps1, USTR(""))); llen = ustr_cspn_chr_rev(*ps1, 0, '/'); if (!llen) { off = ustr_spn_chr_rev(*ps1, off, '/'); llen = u_cspn_chr_rev(*ps1, off, '/'); } if (!llen) return (ustr_set(ps1, USTR1(\1, "/"))); return (ustr_set_subustr(ps1, *ps1, (len - (off + llen)) + 1, llen)); }
static struct Ustr *u_dirname(const struct Ustr *s2) { size_t off = 0; size_t len = ustr_len(s2); if (!len) return (USTR1(\1, ".")); off = ustr_spn_chr_rev(s2, off, '/'); if (len == off) return (USTR1(\1, "/")); off += u_cspn_chr_rev(s2, off, '/'); if (len == off) return (USTR1(\1, ".")); off += ustr_spn_chr_rev(s2, off, '/'); if (len == off) return (USTR1(\1, "/")); return (ustr_dup_subustr(s2, 1, (len - off))); }
void lexicon_lexicon_command (AUstr *command_line) { Ustr *word; if (austr_length (command_line) < 2) printf ("NG \"%s\"\n", "No lexicon specified."); word = ustr_dup_empty (); if (!word) { printf ("NG \"%s\"\n", "Out of memory."); return; } lexicon_generate (&word, austr_i (command_line, 1)); if (ustr_len (word) == 0) printf ("NG \"%s\"\n", "Lexicon not found."); else printf ("OK %s\n", ustr_cstr (word)); ustr_sc_del (&word); }
int main(int argc, char *argv[]) { struct Ustr *s1 = USTR(""); static const char txt[] = "123456789 "; size_t beg = 0; size_t end = 6; size_t count = 0; if (argc == 3) { beg = atoi(argv[1]); end = atoi(argv[2]); } while (count < beg) { ustr_add_rep_chr(&s1, txt[count++ % 10], 1); } while (count < end) { ustr_add_rep_chr(&s1, txt[count++ % 10], 1); printf("String: %lu \"%s\"\n", CLU(ustr_len(s1)), ustr_cstr(s1)); tst_ustr(ustr_cstr(s1), 0, 0, USTR_TRUE); tst_ustr(ustr_cstr(s1), 1, 0, USTR_FALSE); tst_ustr(ustr_cstr(s1), 2, 1, USTR_TRUE); tst_ustr(ustr_cstr(s1), 3, 1, USTR_FALSE); tst_ustr(ustr_cstr(s1), 4, 2, USTR_TRUE); tst_ustr(ustr_cstr(s1), 5, 2, USTR_FALSE); tst_ustr(ustr_cstr(s1), 6, 4, USTR_TRUE); tst_ustr(ustr_cstr(s1), 7, 4, USTR_FALSE); if (USTR_CONF_HAVE_68bit_SIZE_MAX) { tst_ustr(ustr_cstr(s1), 8, 8, USTR_TRUE); tst_ustr(ustr_cstr(s1), 9, 8, USTR_FALSE); } printf("\t strdup() = (%8lu / %-8lu = %5.2f%% )\n", CLU(ustr_len(s1) + 1), CLU(ustr_len(s1)), 100. * ((1 + ustr_len(s1)) / ustr_len(s1))); printf("\t NM_Ustr = (%8lu / %-8lu = %5.2f%% )\n", CLU(sizeof(struct NM_Ustr) + ustr_len(s1) + 1), CLU(sizeof(struct NM_Ustr) + ustr_len(s1)), (100. * (sizeof(struct NM_Ustr) + ustr_len(s1) + 1)) / ustr_len(s1)); printf("\t NM_Ustr x2 = (%8lu / %-8lu = %5.2f%% )\n", CLU(sizeof(struct NM_Ustr) + min_pow(ustr_len(s1) + 1)), CLU(sizeof(struct NM_Ustr) + ustr_len(s1)), (100. * (sizeof(struct NM_Ustr) + min_pow(ustr_len(s1) + 1))) / ustr_len(s1)); printf("\t NM_Ustr xUstr = (%8lu / %-8lu = %5.2f%% )\n", CLU(sizeof(struct NM_Ustr) + min_size(ustr_len(s1) + 1)), CLU(sizeof(struct NM_Ustr) + ustr_len(s1)), (100. * (sizeof(struct NM_Ustr) + min_size(ustr_len(s1) + 1))) / ustr_len(s1)); } ustr_sc_free(&s1); return 0; }
int tst(void) { Ustr *s3 = ustr_dup_cstr("abcdefg xxxx xxxx xxxx xxxx1234yyyy yyyy yyyy zzz"); Ustr *s4 = NULL; Ustr *s5 = NULL; ASSERT(s3); ASSERT(ustr_spn_cstr_fwd(s3, 0, "abcd") == 4); ASSERT(ustr_spn_cstr_fwd(s3, 0, "abcdefg ") == 8); ASSERT(ustr_spn_cstr_fwd(s3, 0, "abcdefg x") == 27); ASSERT(ustr_cspn_cstr_fwd(s3, 0, "x") == 8); ASSERT(ustr_cspn_cstr_fwd(s3, 0, "21") == 27); ASSERT(ustr_spn_cstr_rev(s3, 0, "z") == 3); ASSERT(ustr_spn_cstr_rev(s3, 0, "az") == 3); ASSERT(ustr_spn_cstr_rev(s3, 0, "ayz") == 3); ASSERT(ustr_spn_cstr_rev(s3, 0, "ayz ") == 18); ASSERT(ustr_cspn_cstr_rev(s3, 0, "34") == 18); ASSERT(ustr_cspn_cstr_rev(s3, 0, " ") == 3); ASSERT(ustr_cspn_cstr_rev(s3, 0, "x") == 22); ASSERT(ustr_spn_cstr_fwd(s3, 0, " xayzafg1234bcde ") == ustr_len(s3)); ASSERT(ustr_spn_cstr_rev(s3, 0, "1234abcdefg xayz ") == ustr_len(s3)); ASSERT(ustr_cspn_cstr_fwd(s3, 0, "!") == ustr_len(s3)); ASSERT(ustr_cspn_cstr_rev(s3, 0, "!") == ustr_len(s3)); ASSERT(ustr_cspn_cstr_fwd(s3, 0, "!@#") == ustr_len(s3)); ASSERT(ustr_cspn_cstr_rev(s3, 0, "!@#") == ustr_len(s3)); ASSERT((s4 = ustr_dup_subustr(s3, 1, ustr_spn_cstr_fwd(s3, 0, "abcdefg ")))); ASSERT(ustr_del_subustr(&s3, 1, ustr_len(s4))); ASSERT((s5 = ustr_dup(s4))); ASSERT((s5 = ustr_dup(s4))); ASSERT(ustr_set_subustr(&s5, s3, 1, ustr_spn_cstr_fwd(s3, 0, "x "))); ASSERT(ustr_del_subustr(&s3, 1, ustr_len(s5))); ASSERT(ustr_spn_cstr_fwd(s4, 0, "abcd") == 4); ASSERT(ustr_spn_cstr_fwd(s4, 0, "abcdefg ") == 8); ASSERT(ustr_spn_cstr_fwd(s4, 1, "abcdefg ") == 7); ASSERT(ustr_spn_cstr_fwd(s4, 2, "abcdefg ") == 6); ASSERT(ustr_spn_cstr_fwd(s4, 3, "abcdefg ") == 5); ASSERT(ustr_spn_cstr_fwd(s4, 4, "abcdefg ") == 4); ASSERT(ustr_spn_cstr_fwd(s4, 5, "abcdefg ") == 3); ASSERT(ustr_spn_cstr_fwd(s4, 6, "abcdefg ") == 2); ASSERT(ustr_spn_cstr_fwd(s4, 7, "abcdefg ") == 1); ASSERT(ustr_spn_cstr_fwd(s4, 8, "abcdefg ") == 0); ASSERT(ustr_spn_cstr_rev(s3, 0, "z") == 3); ASSERT(ustr_spn_cstr_rev(s3, 4, "y") == 4); ASSERT(ustr_spn_cstr_rev(s3, 0, "az") == 3); ASSERT(ustr_spn_cstr_rev(s3, 0, "ayz") == 3); ASSERT(ustr_spn_cstr_rev(s3, 0, "ayz ") == 18); ASSERT(ustr_spn_cstr_rev(s3, 0, "y ") == 0); ASSERT(ustr_spn_cstr_rev(s3, 1, "y ") == 0); ASSERT(ustr_spn_cstr_rev(s3, 2, "y ") == 0); ASSERT(ustr_spn_cstr_rev(s3, 3, "y ") == 15); ASSERT(ustr_spn_cstr_rev(s3, 4, "y ") == 14); ASSERT(ustr_spn_cstr_rev(s3, 5, "y ") == 13); ASSERT(ustr_spn_cstr_rev(s3, 6, "y ") == 12); ASSERT(ustr_spn_cstr_rev(s3, 7, "y ") == 11); ASSERT(ustr_spn_cstr_rev(s3, 8, "y ") == 10); ASSERT(ustr_spn_cstr_rev(s3, 9, "y ") == 9); ASSERT(ustr_cspn_cstr_fwd(s4, 0, "e") == 4); ASSERT(ustr_cspn_cstr_fwd(s4, 0, "!-") == 8); ASSERT(ustr_cspn_cstr_fwd(s4, 1, "!-") == 7); ASSERT(ustr_cspn_cstr_fwd(s4, 2, "!-") == 6); ASSERT(ustr_cspn_cstr_fwd(s4, 3, "!-") == 5); ASSERT(ustr_cspn_cstr_fwd(s4, 4, "!-") == 4); ASSERT(ustr_cspn_cstr_fwd(s4, 5, "!-") == 3); ASSERT(ustr_cspn_cstr_fwd(s4, 6, "!-") == 2); ASSERT(ustr_cspn_cstr_fwd(s4, 7, "!-") == 1); ASSERT(ustr_cspn_cstr_fwd(s4, 8, "!-") == 0); ASSERT(ustr_cspn_cstr_fwd(s4, 0, "!") == 8); ASSERT(ustr_cspn_cstr_fwd(s4, 1, "!") == 7); ASSERT(ustr_cspn_cstr_fwd(s4, 2, "!") == 6); ASSERT(ustr_cspn_cstr_fwd(s4, 3, "!") == 5); ASSERT(ustr_cspn_cstr_fwd(s4, 4, "!") == 4); ASSERT(ustr_cspn_cstr_fwd(s4, 5, "!") == 3); ASSERT(ustr_cspn_cstr_fwd(s4, 6, "!") == 2); ASSERT(ustr_cspn_cstr_fwd(s4, 7, "!") == 1); ASSERT(ustr_cspn_cstr_fwd(s4, 8, "!") == 0); ASSERT(ustr_cspn_cstr_rev(s4, 0, "e") == 3); ASSERT(ustr_cspn_cstr_rev(s4, 0, "!-") == 8); ASSERT(ustr_cspn_cstr_rev(s4, 1, "!-") == 7); ASSERT(ustr_cspn_cstr_rev(s4, 2, "!-") == 6); ASSERT(ustr_cspn_cstr_rev(s4, 3, "!-") == 5); ASSERT(ustr_cspn_cstr_rev(s4, 4, "!-") == 4); ASSERT(ustr_cspn_cstr_rev(s4, 5, "!-") == 3); ASSERT(ustr_cspn_cstr_rev(s4, 6, "!-") == 2); ASSERT(ustr_cspn_cstr_rev(s4, 7, "!-") == 1); ASSERT(ustr_cspn_cstr_rev(s4, 8, "!-") == 0); ASSERT(ustr_cspn_cstr_rev(s4, 0, "!") == 8); ASSERT(ustr_cspn_cstr_rev(s4, 1, "!") == 7); ASSERT(ustr_cspn_cstr_rev(s4, 2, "!") == 6); ASSERT(ustr_cspn_cstr_rev(s4, 3, "!") == 5); ASSERT(ustr_cspn_cstr_rev(s4, 4, "!") == 4); ASSERT(ustr_cspn_cstr_rev(s4, 5, "!") == 3); ASSERT(ustr_cspn_cstr_rev(s4, 6, "!") == 2); ASSERT(ustr_cspn_cstr_rev(s4, 7, "!") == 1); ASSERT(ustr_cspn_cstr_rev(s4, 8, "!") == 0); ASSERT(ustr_cspn_cstr_rev(s3, 0, "34") == 18); ASSERT(ustr_cspn_cstr_rev(s3, 0, " ") == 3); ASSERT(ustr_cspn_cstr_rev(s3, 0, "x") == 22); ASSERT(ustr_spn_cstr_fwd(s3, 0, "1") == 1); ASSERT(ustr_spn_cstr_fwd(s3, 0, "12") == 2); ASSERT(ustr_spn_cstr_fwd(s3, 0, "123") == 3); ASSERT(ustr_spn_cstr_fwd(s3, 0, "1234") == 4); ASSERT(ustr_spn_cstr_fwd(s3, 0, "1234y") == 8); ASSERT(ustr_del_subustr(&s3, 4, 4)); ASSERT(ustr_spn_cstr_fwd(s3, 0, "1234y") == 4); ustr_free(s3); ustr_free(s4); /* leaked, see above */ ustr_free(s4); ustr_free(s5); return (EXIT_SUCCESS); }
int tst(void) { /* single char sep; do not keep sep; do keep blank strings */ Ustr *a = ustr_dup_cstr("blue,yellow,red,green,,orange"); const char *ans[] = {"blue","yellow","red","green","","orange"}; size_t off = 0; unsigned int flags = (USTR_FLAG_SPLIT_RET_NON); Ustr *sep = ustr_dup_cstr(","); Ustr *tok = NULL; Ustr *ret = NULL; size_t i = 0; const char *ans2[] = {"a==","b,c==","d,c==","==","==",",12==","xzy==","=="}; const char *ans3[] = {"a","bx","cx","dx","xx"}; const char *ans4[] = {"a,bx","cx","dx",",xx"}; const char *ans5[] = {"this","is","a","test","of","the","emergency","broadcast","system"}; const char *ans6[] = {" ", " ", "this ","is ","a ","test ","of ","the\t","emergency\n"," ","broadcast ","system"}; const char *ans7[] = {"a ","b ","c ","d "," "," "," "," "," "," "," "," "," ","e|","f|","g|","|","h"}; const char *ans8[] = {"a","b","c","d","e","f","g","h"}; const char *ans9[] = {"a","b","c","d","","","","","","","","","","e","f","g","","h"}; const char *ans10[] = {"a b c d e|f|g||h"}; const char *chrs = "\t\n "; while ((tok = ustr_split(a,&off,sep, NULL,flags))) { ASSERT(ustr_cmp_cstr_eq(tok,ans[i])); ++i; ustr_sc_free(&tok); } ASSERT(i == 6); ustr_sc_free(&a); ustr_sc_free(&sep); /* mult-char sep; keep sep; do not keep blank strings */ a = ustr_dup_cstr("a==b,c==d,c======,12==xzy===="); sep = ustr_dup_cstr("=="); flags = (USTR_FLAG_SPLIT_RET_SEP); i=0; off=0; while ((tok = ustr_split(a,&off,sep, NULL,flags))) { ASSERT(ustr_cmp_cstr_eq(tok,ans2[i])); ++i; ustr_sc_free(&tok); } ASSERT(i == 8); ustr_sc_free(&a); ustr_sc_free(&sep); /* single-char sep; skip sep, skip blank */ a = ustr_dup_cstr("a,bx,,,,cx,,dx,,,xx,,,,"); sep = ustr_dup_cstr(","); flags = 0; i=0; off=0; while ((tok = ustr_split(a,&off,sep, NULL,flags))) { ASSERT(ustr_cmp_cstr_eq(tok,ans3[i])); ++i; ustr_sc_free(&tok); } ustr_sc_free(&sep); /* multi-char sep; skip sep; skip blank */ sep = ustr_dup_cstr(",,"); i=0; off=0; while ((tok = ustr_split(a,&off,sep, NULL,flags))) { ASSERT(ustr_cmp_cstr_eq(tok,ans4[i])); ++i; ustr_sc_free(&tok); } ustr_sc_free(&sep); ASSERT(i==4); /* blank sep */ off = 0; sep = USTR(""); ASSERT((ret = ustr_split(a,&off,sep, NULL,flags)) == USTR_FALSE); ASSERT(off==0); ustr_sc_free(&ret); /* blank target */ sep = ustr_dup_cstr(","); ustr_sc_free(&a); a = USTR(""); ASSERT((ret = ustr_split(a,&off,sep, NULL,flags)) == USTR_FALSE); ASSERT(off==0); ustr_sc_free(&sep); ustr_sc_free(&ret); a = USTR1(\x15, "a||bx||||cx||dx||||xx"); i = 0; off = 0; while ((tok = ustr_split_cstr(a, &off, "||", NULL, USTR_FLAG_SPLIT_DEF))) { ASSERT(!ustr_sized(a)); ASSERT(!ustr_sized(tok)); ASSERT(ustr_cmp_cstr_eq(tok, ans3[i++])); ustr_sc_free(&tok); } ASSERT((a = ustr_dupx(1, 0, 0, 0, a))); off = 1; ASSERT((tok = ustr_split_cstr(a, &off, "||", NULL, USTR_FLAG_SPLIT_RET_NON))); ASSERT( ustr_sized(a)); ASSERT(!ustr_sized(tok)); ASSERT( ustr_ro(tok)); ASSERT(!ustr_len(tok)); ustr_sc_free(&tok); off = 1; ASSERT((tok = ustr_split_cstr(a, &off, "||", NULL, USTR_FLAG_SPLIT_RET_SEP))); ASSERT( ustr_sized(a)); ASSERT(!ustr_sized(tok)); ASSERT(!ustr_ro(tok)); ASSERT(ustr_cmp_cstr_eq(tok, "||")); ustr_sc_free(&tok); ASSERT((tok = ustr_split_cstr(a, &off, "||", NULL, USTR_FLAG_SPLIT_RET_SEP))); ASSERT( ustr_sized(a)); ASSERT(!ustr_sized(tok)); ASSERT(!ustr_ro(tok)); ASSERT(ustr_cmp_cstr_eq(tok, "bx||")); ustr_sc_free(&tok); i = 2; while ((tok = ustr_split_cstr(a, &off, "||", tok, USTR_FLAG_SPLIT_KEEP_CONF))) { ASSERT( ustr_sized(a)); ASSERT( ustr_sized(tok)); ASSERT(ustr_cmp_cstr_eq(tok, ans3[i++])); ustr_sc_free(&tok); } off = 0; i = 0; flags = USTR_FLAG_SPLIT_KEEP_CONF; while ((tok = ustr_split_cstr(a, &off, "||", tok, flags))) { ASSERT( ustr_sized(a)); ASSERT( ustr_sized(tok)); ASSERT(ustr_cmp_cstr_eq(tok, ans3[i++])); flags = USTR_FLAG_SPLIT_DEF; /* doesn't matter after first one */ } off = 0; ASSERT(!ustr_split(a, &off, a, NULL, 0)); ASSERT((off == ustr_len(a))); off = 0; ASSERT((tok = ustr_split(a, &off, a, NULL, (USTR_FLAG_SPLIT_RET_NON | USTR_FLAG_SPLIT_KEEP_CONF)))); ASSERT((off == ustr_len(a))); ASSERT( ustr_sized(tok)); ASSERT( ustr_cstr(tok)); ASSERT_EQ(tok, USTR("")); ustr_sc_free(&tok); off = 0; ASSERT((tok = ustr_split(a, &off, a, NULL, (USTR_FLAG_SPLIT_RET_SEP | USTR_FLAG_SPLIT_KEEP_CONF)))); ASSERT((off == ustr_len(a))); ASSERT_EQ(tok, a); ASSERT( ustr_sized(tok)); ASSERT( ustr_cstr(tok)); off = 0; ASSERT((tok = ustr_split(a, &off, a, tok, USTR_FLAG_SPLIT_RET_NON))); ASSERT( ustr_sized(tok)); ASSERT(!ustr_ro(tok)); ustr_sc_free(&tok); off = 0; ASSERT((tok = ustr_split(a, &off, a, tok, USTR_FLAG_SPLIT_RET_NON))); ASSERT(!ustr_sized(tok)); ASSERT( ustr_ro(tok)); off = 0; ASSERT((tok = ustr_dup_cstr("abcd"))); ASSERT(!ustr_split(a, &off, a, tok, 0)); /* does the free */ ustr_sc_free(&a); /* ustr_split_spn_cstr */ a = ustr_dup_cstr(" this is a test of the\temergency\n broadcast system"); off = 0; i=0; tok = USTR_NULL; while ((tok = ustr_split_spn_cstr(a,&off,chrs,USTR_NULL,0))) { ASSERT(ustr_cmp_cstr_eq(tok,ans5[i++])); ustr_sc_free(&tok); } ASSERT(i==9); i=0; off=0; while ((tok = ustr_split_spn_cstr(a,&off,chrs,USTR_NULL,USTR_FLAG_SPLIT_RET_SEP))) { ASSERT(ustr_cmp_cstr_eq(tok,ans6[i++])); ustr_sc_free(&tok); } ASSERT(i==12); i=0; off=0; while ((tok = ustr_split_spn_cstr(a,&off,chrs,USTR_NULL,USTR_FLAG_SPLIT_RET_SEP|USTR_FLAG_SPLIT_RET_NON))) { ASSERT(ustr_cmp_cstr_eq(tok,ans6[i++])); ustr_sc_free(&tok); } ASSERT(i==12); ustr_sc_free(&a); chrs = " |"; a = ustr_dup_cstr("a b c d e|f|g||h"); i=0; off=0; while ((tok = ustr_split_spn_cstr(a,&off,chrs,USTR_NULL,USTR_FLAG_SPLIT_RET_SEP|USTR_FLAG_SPLIT_RET_NON))) { ASSERT(ustr_cmp_cstr_eq(tok,ans7[i++])); ustr_sc_free(&tok); } ASSERT(i==18); i=0; off=0; while ((tok = ustr_split_spn_cstr(a,&off,chrs,USTR_NULL,0))) { ASSERT(ustr_cmp_cstr_eq(tok,ans8[i++])); ustr_sc_free(&tok); } ASSERT(i==8); i=0; off=0; while ((tok = ustr_split_spn_cstr(a,&off,chrs,USTR_NULL,USTR_FLAG_SPLIT_RET_NON))) { ASSERT(ustr_cmp_cstr_eq(tok,ans9[i++])); ustr_sc_free(&tok); } ASSERT(i==18); chrs = "xyz"; i=0; off=0; while ((tok = ustr_split_spn_cstr(a,&off,chrs,tok,USTR_FLAG_SPLIT_RET_NON))) { ASSERT(ustr_cmp_cstr_eq(tok,ans10[i++])); } off=0; chrs = "g"; ASSERT(i==1); ustr_sc_free(&a); a = USTR1(\x3, "agg"); ASSERT(ustr_assert_valid(a)); ASSERT( ustr_ro(a)); ASSERT(!ustr_sized(a)); tok = ustr_split_spn_cstr(a,&off,chrs,tok,USTR_FLAG_SPLIT_RET_NON); ASSERT(ustr_cmp_cstr_eq(tok,"a")); ASSERT(!ustr_ro(tok)); ASSERT(!ustr_sized(tok)); tok = ustr_split_spn_cstr(a,&off,chrs,tok,USTR_FLAG_SPLIT_RET_NON); ASSERT(ustr_cmp_cstr_eq(tok,"")); ASSERT( ustr_ro(tok)); ASSERT(!ustr_sized(tok)); ustr_sc_free(&tok); ASSERT((a = ustr_dupx(1, 0, 0, 0, a))); off=0; tok = ustr_split_spn(a,&off,USTR1(\1, "g"),tok,USTR_FLAG_SPLIT_KEEP_CONF); ASSERT(ustr_cmp_cstr_eq(tok,"a")); ASSERT(ustr_sized(a)); ASSERT(ustr_sized(tok)); ustr_sc_free(&a); ustr_sc_free(&tok); return (EXIT_SUCCESS); }
int tst(void) { size_t end = USTR__SIZE_MAX; size_t large = 1; size_t small = 4096; size_t num = 0; Ustr *s3 = USTR1(\3, "124"); ASSERT((s1 = ustr_dupx_empty(0, 0, 0, 0))); ASSERT(ustr_size_alloc(s1) > 0); /* max 32bit overhead */ while (num < (1 + 4 + sizeof(USTR_END_ALOCDx))) { ASSERT(!ustr_add_undef(&s1, end - num)); ASSERT(!ustr_dup_undef( end - num)); ASSERT(!ustr_ins_undef(&s3, 1, end - num)); ASSERT(!ustr_set_undef(&s1, end - num)); ++num; } while (num < (1 + 4 + sizeof(USTR_END_ALOCDx) + ustr_len(s3))) { ASSERT(!ustr_ins_undef(&s3, 1, end - num)); ++num; } num = (1 + 4 + sizeof(USTR_END_ALOCDx)); if (!USTR_CONF_HAVE_64bit_SIZE_MAX) { big_tst(small, end, USTR_TRUE, USTR_TRUE); big_tst( 0, end, USTR_TRUE, USTR_TRUE); return (EXIT_FAILED_OK); } /* max 64bit overhead */ while (num < (1 + 2 + 8 + 8 + sizeof(USTR_END_ALOCDx))) { ASSERT(!ustr_add_undef(&s1, end - num)); ASSERT(!ustr_dup_undef( end - num)); ASSERT(!ustr_ins_undef(&s3, 1, end - num)); ASSERT(!ustr_set_undef(&s1, end - num)); ++num; } while (num < (1 + 2 + 8 + 8 + sizeof(USTR_END_ALOCDx) + ustr_len(s3))) { ASSERT(!ustr_ins_undef(&s3, 1, end - num)); ++num; } ustr_free(s1); if (USTR_CONF_USE_DYNAMIC_CONF) { assert(ustr_cntl_opt(USTR_CNTL_OPT_SET_REF_BYTES, 8)); assert(ustr_cntl_opt(USTR_CNTL_OPT_SET_HAS_SIZE, 0)); ASSERT((s1 = ustr_dupx_undef(0, 8, 0, 0, 0))); ASSERT(ustr_ro(s1)); ASSERT((s1 = ustr_dupx_undef(1, 8, 0, 0, 0))); ASSERT(ustr_ro(s1)); } ASSERT((s1 = ustr_dupx_empty(0, 8, USTR_FALSE, USTR_FALSE))); ASSERT(ustr_sized(s1)); ASSERT(ustr_size_alloc(s1) >= 14); ASSERT(ustr_size_overhead(s1) >= 14); ustr_free(ustr_dup(s1)); big_tst(small, end, USTR_TRUE, USTR_TRUE); big_tst( 0, end, USTR_TRUE, USTR_TRUE); large *= 1024; large *= 1024; large *= 1024; large *= 9; return (big_tst(0, large, USTR_FALSE, USTR_FALSE)); }
/* Learns a new lexicon. */ static Lexicon * learn (Ustr const *fname) { Lexicon *rv; AUstr words; Ustr *line; FILE *lexicon; /* Initialize variables. */ rv = malloc (sizeof (Lexicon)); if (!rv) abort (); austr_init (&words); line = ustr_dup_empty (); if (!line) abort (); lexicon = fopen (ustr_cstr (fname), "r"); if (!lexicon) { perror ("galcry-backend, lexicon, learn"); abort (); } /* Clear histogram. */ memset ((void *) rv, 0, sizeof (Lexicon)); /* Learn words. */ while (errno = 0, ustr_sc_del (&line), ustr_io_getline (&line, lexicon)) { unsigned int i; /* Skip comment lines. */ if (ustr_cstr (line)[0] == '#') continue; wordsplit (&words, line); for (i = 0; i < austr_length (&words); ++i) { unsigned int l; char const *s; l = ustr_len (austr_i (&words, i)); if (l < 2) continue; s = ustr_cstr (austr_i (&words, i)); { unsigned int i; unsigned int c0, c1, c2; c0 = 0; c1 = (s[0] % 32) % 27; ++rv->histogram2[c0][c1]; for (i = 1; i < l + 1; ++i) { c2 = (s[i] % 32) % 27; ++rv->histogram3[c0][c1][c2]; ++rv->histogram2[c1][c2]; c0 = c1; c1 = c2; } } } } if (errno != 0) { perror ("galcry-backend, lexicon, learn, read"); abort (); } fclose (lexicon); ustr_sc_free (&line); austr_deinit (&words); return rv; }
int tst(void) { struct Ustr *s3 = NULL; struct Ustr *s4 = NULL; assert(!USTR_CONF_USE_DYNAMIC_CONF || ustr_cntl_opt(USTR_CNTL_OPT_SET_REF_BYTES, 1)); /* move to the new "default" conf */ ustr_sc_free2(&s2, ustr_dup_buf(ustr_cstr(s2), ustr_len(s2))); ASSERT(s2); s3 = ustr_dup_cstr("s3 abcd s2"); s4 = ustr_dup_rep_chr('x', 40); ASSERT(s3); ASSERT(s4); ASSERT(ustr_len(s1) == 0); ASSERT(ustr_len(s2) == 2); ASSERT(ustr_len(s3) == 10); ASSERT(ustr_len(s4) == 40); ASSERT(ustr_size(s1) == 0); if (!USTR_CONF_USE_EOS_MARK) ASSERT(ustr_size(s2) == 2); /* ustr__ns(1 + 1 + 1 + 2 + 1) - * 1 + 1 + 1 + 1 */ if (!USTR_CONF_USE_EOS_MARK) ASSERT(ustr_size(s3) == 12); /* ustr__ns(1 + 1 + 1 + 10 + 1) - * 1 + 1 + 1 + 1 */ if (!USTR_CONF_USE_EOS_MARK) ASSERT(ustr_size(s4) == 44); ASSERT(ustr_add_rep_chr(&s4, '-', 8)); ASSERT(ustr_len(s4) == 48); if (!USTR_CONF_USE_EOS_MARK) ASSERT(ustr_size(s4) == 60); ASSERT(ustr_del(&s4, 4)); ASSERT(ustr_len(s4) == 44); if (!USTR_CONF_USE_EOS_MARK) ASSERT(ustr_size(s4) == 44); ASSERT(ustr_del(&s4, 20)); ASSERT(ustr_len(s4) == 24); if (!USTR_CONF_USE_EOS_MARK) ASSERT(ustr_size(s4) == 28); ustr_sc_free(&s3); ASSERT((s3 = ustr_dup(s4))); ASSERT(ustr_len(s3) == 24); if (!USTR_CONF_USE_EOS_MARK) ASSERT(ustr_size(s3) == 28); ASSERT(ustr_len(s4) == 24); if (!USTR_CONF_USE_EOS_MARK) ASSERT(ustr_size(s4) == 28); ASSERT(ustr_del(&s4, 15)); ASSERT(ustr_len(s3) == 24); if (!USTR_CONF_USE_EOS_MARK) ASSERT(ustr_size(s3) == 28); ASSERT(ustr_len(s4) == 9); if (!USTR_CONF_USE_EOS_MARK) ASSERT(ustr_size(s4) == 12); ASSERT(ustr_cmp_cstr_eq(s3, "xxxxxxxxxxxxxxxxxxxxxxxx")); ASSERT(!strcmp(ustr_cstr(s3), "xxxxxxxxxxxxxxxxxxxxxxxx")); ASSERT(ustr_cmp_cstr_eq(s4, "xxxxxxxxx")); ASSERT(!strcmp(ustr_cstr(s4), "xxxxxxxxx")); ASSERT(!ustr_del(&s4, 15)); ASSERT(ustr_del(&s4, 8)); ASSERT(ustr_len(s4) == 1); if (!USTR_CONF_USE_EOS_MARK) ASSERT(ustr_size(s4) == 2); ASSERT(ustr_cmp_cstr_eq(s4, "x")); ASSERT(!strcmp(ustr_cstr(s4), "x")); ASSERT(ustr_add_rep_chr(&s4, '-', 8)); ASSERT(ustr_len(s4) == 9); if (!USTR_CONF_USE_EOS_MARK) ASSERT(ustr_size(s4) == 12); ASSERT(ustr_cmp_cstr_eq(s4, "x--------")); ASSERT(!strcmp(ustr_cstr(s4), "x--------")); ASSERT(ustr_set_cstr(&s4, "x12345678")); ustr_sc_free2(&s3, s4); ASSERT(ustr_cmp_cstr_eq(s4, "x12345678")); ASSERT( ustr_owner(s4)); ASSERT(!ustr_shared(s4)); ASSERT(s3 == s4); ASSERT( ustr_setf_share(s4)); ASSERT(!ustr_owner(s4)); ASSERT(ustr_del_subustr(&s4, 4, 4)); ASSERT(ustr_cmp_cstr_eq(s3, "x12345678")); ASSERT(ustr_cmp_cstr_eq(s4, "x1278")); ustr_sc_free_shared(&s3); ustr_sc_free(&s4); return (EXIT_SUCCESS); }
int tst(void) { struct Ustr *s3 = NULL; struct Ustr *s4 = NULL; int num = -1; assert(!USTR_CONF_USE_DYNAMIC_CONF || ustr_cntl_opt(USTR_CNTL_OPT_SET_REF_BYTES, 1)); /* move to the new "default" conf */ ustr_sc_free2(&s2, ustr_dup_buf(ustr_cstr(s2), ustr_len(s2))); s3 = ustr_dup_cstr("s3 abcd s2"); s4 = ustr_dup_empty(); /* always allocs */ ASSERT(s2); ASSERT(s3); ASSERT(s4); ASSERT(ustr_cmp_eq(s1, s4)); ASSERT(ustr_len(s1) == 0); ASSERT(ustr_len(s2) == 2); ASSERT(ustr_len(s3) == 10); ASSERT(ustr_len(s4) == 0); ASSERT(ustr_size(s1) == 0); if (!USTR_CONF_USE_EOS_MARK) ASSERT(ustr_size(s2) == 2); if (!USTR_CONF_USE_EOS_MARK) ASSERT(ustr_size(s3) == 12); if (!USTR_CONF_USE_EOS_MARK) ASSERT(ustr_size(s4) == 0); ASSERT(ustr_srch_fwd(s3, 0, s2) == 9); ASSERT(ustr_srch_rev(s3, 0, s2) == 9); ASSERT(ustr_srch_fwd(s2, 0, s3) == 0); ASSERT(ustr_srch_rev(s2, 0, s3) == 0); ASSERT(ustr_add_cstr(&s2, "x")); ASSERT(ustr_len(s2) == 3); if (!USTR_CONF_USE_EOS_MARK) ASSERT(ustr_size(s2) == 4); ASSERT(ustr_add_cstr(&s2, "y")); ASSERT(ustr_len(s2) == 4); if (!USTR_CONF_USE_EOS_MARK) ASSERT(ustr_size(s2) == 4); ASSERT(ustr_add_cstr(&s2, "z")); ASSERT(ustr_len(s2) == 5); if (!USTR_CONF_USE_EOS_MARK) ASSERT(ustr_size(s2) == 8); ASSERT(ustr_add_rep_chr(&s2, '-', 11)); ASSERT(ustr_len(s2) == 16); if (!USTR_CONF_USE_EOS_MARK) ASSERT(ustr_size(s2) == 20); ASSERT(ustr_cmp_cstr_eq(s2, "s2xyz-----------")); ASSERT(!strcmp(ustr_cstr(s2), "s2xyz-----------")); ASSERT(ustr_srch_fwd(s3, 0, s2) == 0); ASSERT(ustr_srch_rev(s3, 0, s2) == 0); ASSERT(ustr_srch_fwd(s2, 0, s3) == 0); ASSERT(ustr_srch_rev(s2, 0, s3) == 0); /* NOTE: Using system *printf, so can't use %zu as Solaris is retarded */ ASSERT(ustr_add_fmt(&s1, "%s abcd %13.100s %d %c %lu%n", "------abc------", "", 42, 0, (unsigned long)ustr_len(s3), &num) != -1); ASSERT((unsigned)num == ustr_len(s1)); ASSERT(42 == num); if (!USTR_CONF_USE_EOS_MARK) ASSERT(44 == ustr_size(s1)); ASSERT(ustr_add_fmt(&s4, "%2$d%1$u", 2, 4)); ASSERT(ustr_srch_cstr_fwd(s1, 0, "abcd") == 17); ASSERT(ustr_srch_cstr_rev(s1, 0, "abcd") == 17); ASSERT(ustr_srch_cstr_fwd(s1, 0, "abc") == 7); ASSERT(ustr_srch_cstr_rev(s1, 0, "abc") == 17); ASSERT(ustr_srch_cstr_fwd(s1, 0, "10") == 41); ASSERT(ustr_srch_cstr_rev(s1, 0, "10") == 41); ASSERT(ustr_srch_chr_fwd(s1, 0, 0) == 39); ASSERT(ustr_srch_chr_rev(s1, 0, 0) == 39); ASSERT(ustr_srch_fwd(s1, 0, s4) == 36); ASSERT(ustr_srch_cstr_fwd(s1, 1, "abcd") == 17); ASSERT(ustr_srch_cstr_rev(s1, 1, "abcd") == 17); ASSERT(ustr_srch_cstr_fwd(s1, 10, "abcd") == 17); ASSERT(ustr_srch_cstr_rev(s1, 10, "abcd") == 17); ASSERT(ustr_srch_cstr_fwd(s1, 0, " ") == 16); ASSERT(ustr_srch_cstr_fwd(s1, 10, " ") == 16); ASSERT(ustr_srch_cstr_fwd(s1, 16, " ") == 21); ASSERT(ustr_srch_cstr_fwd(s1, 20, " ") == 21); ASSERT(ustr_srch_cstr_fwd(s1, 21, " ") == 22); ASSERT(ustr_srch_rep_chr_fwd(s1, 21, ' ', 1) == 22); ASSERT(ustr_srch_cstr_fwd(s1, 21, " ") == 22); ASSERT(ustr_srch_rep_chr_fwd(s1, 21, ' ', 2) == 22); ASSERT(ustr_srch_cstr_fwd(s1, 21, " ") == 22); ASSERT(ustr_srch_rep_chr_fwd(s1, 21, ' ', 3) == 22); ASSERT(ustr_srch_cstr_fwd(s1, 21, " ") == 22); ASSERT(ustr_srch_rep_chr_fwd(s1, 21, ' ', 4) == 22); ASSERT(ustr_srch_cstr_fwd(s1, 21, " ") == 22); ASSERT(ustr_srch_rep_chr_fwd(s1, 21, ' ', 5) == 22); ASSERT(ustr_srch_cstr_fwd(s1, 0, "a") == 7); ASSERT(ustr_srch_cstr_fwd(s1, 6, "a") == 7); ASSERT(ustr_srch_cstr_fwd(s1, 7, "a") == 17); ASSERT(ustr_srch_cstr_fwd(s1, 16, "a") == 17); ASSERT(ustr_srch_cstr_fwd(s1, 17, "a") == 0); ASSERT(ustr_srch_cstr_rev(s1, 0, "a") == 17); ASSERT(ustr_srch_cstr_rev(s1, ustr_len(s1) - 17, "a") == 17); ASSERT(ustr_srch_cstr_rev(s1, ustr_len(s1) - 16, "a") == 7); ASSERT(ustr_srch_cstr_rev(s1, ustr_len(s1) - 7, "a") == 7); ASSERT(ustr_srch_cstr_rev(s1, ustr_len(s1) - 6, "a") == 0); ASSERT(ustr_srch_cstr_rev(s1, ustr_len(s1) - 1, "a") == 0); /* srch_case */ ASSERT(ustr_srch_case_cstr_fwd(s1, 0, " ") == 16); ASSERT(ustr_srch_case_cstr_fwd(s1, 10, " ") == 16); ASSERT(ustr_srch_case_cstr_fwd(s1, 16, " ") == 21); ASSERT(ustr_srch_case_cstr_fwd(s1, 20, " ") == 21); ASSERT(ustr_srch_case_cstr_fwd(s1, 21, " ") == 22); ASSERT(ustr_srch_case_rep_chr_fwd(s1, 21, ' ', 1) == 22); ASSERT(ustr_srch_case_cstr_fwd(s1, 21, " ") == 22); ASSERT(ustr_srch_case_rep_chr_fwd(s1, 21, ' ', 2) == 22); ASSERT(ustr_srch_case_cstr_fwd(s1, 21, " ") == 22); ASSERT(ustr_srch_case_rep_chr_fwd(s1, 21, ' ', 3) == 22); ASSERT(ustr_srch_case_cstr_fwd(s1, 21, " ") == 22); ASSERT(ustr_srch_case_rep_chr_fwd(s1, 21, ' ', 4) == 22); ASSERT(ustr_srch_case_cstr_fwd(s1, 21, " ") == 22); ASSERT(ustr_srch_case_rep_chr_fwd(s1, 21, ' ', 5) == 22); ASSERT(ustr_srch_case_cstr_fwd(s1, 0, "a") == 7); ASSERT(ustr_srch_case_cstr_fwd(s1, 6, "a") == 7); ASSERT(ustr_srch_case_cstr_fwd(s1, 7, "a") == 17); ASSERT(ustr_srch_case_cstr_fwd(s1, 16, "a") == 17); ASSERT(ustr_srch_case_cstr_fwd(s1, 17, "a") == 0); ASSERT(ustr_srch_case_cstr_fwd(s1, 0, "A") == 7); ASSERT(ustr_srch_case_cstr_fwd(s1, 6, "A") == 7); ASSERT(ustr_srch_case_cstr_fwd(s1, 7, "A") == 17); ASSERT(ustr_srch_case_cstr_fwd(s1, 16, "A") == 17); ASSERT(ustr_srch_case_cstr_fwd(s1, 17, "A") == 0); ASSERT(ustr_srch_case_cstr_rev(s1, 0, "a") == 17); ASSERT(ustr_srch_case_cstr_rev(s1, ustr_len(s1) - 17, "a") == 17); ASSERT(ustr_srch_case_cstr_rev(s1, ustr_len(s1) - 16, "a") == 7); ASSERT(ustr_srch_case_cstr_rev(s1, ustr_len(s1) - 7, "a") == 7); ASSERT(ustr_srch_case_cstr_rev(s1, ustr_len(s1) - 6, "a") == 0); ASSERT(ustr_srch_case_cstr_rev(s1, ustr_len(s1) - 1, "a") == 0); ASSERT(ustr_srch_case_cstr_rev(s1, 0, "A") == 17); ASSERT(ustr_srch_case_cstr_rev(s1, ustr_len(s1) - 17, "A") == 17); ASSERT(ustr_srch_case_cstr_rev(s1, ustr_len(s1) - 16, "A") == 7); ASSERT(ustr_srch_case_cstr_rev(s1, ustr_len(s1) - 7, "A") == 7); ASSERT(ustr_srch_case_cstr_rev(s1, ustr_len(s1) - 6, "A") == 0); ASSERT(ustr_srch_case_cstr_rev(s1, ustr_len(s1) - 1, "A") == 0); if (!USTR_DEBUG) ASSERT(ustr_srch_cstr_fwd(s1, ustr_len(s1), "a") == 0); if (!USTR_DEBUG) ASSERT(ustr_srch_cstr_rev(s1, ustr_len(s1), "a") == 0); /* puts(ustr_cstr(s4)); */ ustr_sc_free(&s3); ASSERT((s3 = ustr_dup(s4))); ASSERT(ustr_add_fmt(&s4, "x")); ustr_sc_free(&s4); ustr_sc_free(&s3); /* ASSERT(!ustr_assert_valid(USTR1(\x000F, "123456789 123456"))); ASSERT(!ustr_assert_valid(USTR1(\x000F, "123456789 1234\0xxx"))); */ ASSERT( ustr_assert_valid(USTR1(\x000F, "123456789 12345"))); /* ASSERT(!ustr_assert_valid(USTR1(\x000F, "123456789 12345\0xxx"))); */ s3 = ustr_dupx(0, 2, 0, 1, USTR1(\x000F, "123456789 12345")); ASSERT(ustr_cmp_cstr_eq(s3, "123456789 12345")); ASSERT(ustr_cmp_eq(s3, USTR1(\x000F, "123456789 12345"))); ASSERT(!ustr_ro(s3)); if (!USTR_CONF_USE_EOS_MARK) ASSERT(ustr_size(s3) == 19); ustr_sc_free(&s3); return (EXIT_SUCCESS); }