/* Helper function called via semanage_fcontext_iterate() */ static int fcontext_matches(const semanage_fcontext_t *fcontext, void *varg) { const char *oexpr = semanage_fcontext_get_expr(fcontext); fc_match_handle_t *handp = varg; struct Ustr *expr; regex_t re; int type, retval = -1; /* Only match ALL or DIR */ type = semanage_fcontext_get_type(fcontext); if (type != SEMANAGE_FCONTEXT_ALL && type != SEMANAGE_FCONTEXT_ALL) return 0; /* Convert oexpr into a Ustr and anchor it at the beginning */ expr = ustr_dup_cstr("^"); if (expr == USTR_NULL) goto done; if (!ustr_add_cstr(&expr, oexpr)) goto done; /* Strip off trailing ".+" or ".*" */ if (ustr_cmp_suffix_cstr_eq(expr, ".+") || ustr_cmp_suffix_cstr_eq(expr, ".*")) { if (!ustr_del(&expr, 2)) goto done; } /* Strip off trailing "(/.*)?" */ if (ustr_cmp_suffix_cstr_eq(expr, "(/.*)?")) { if (!ustr_del(&expr, 6)) goto done; } if (ustr_cmp_suffix_cstr_eq(expr, "/")) { if (!ustr_del(&expr, 1)) goto done; } /* Append pattern to eat up trailing slashes */ if (!ustr_add_cstr(&expr, "/*$")) goto done; /* Check dir against expr */ if (regcomp(&re, ustr_cstr(expr), REG_EXTENDED) != 0) goto done; if (regexec(&re, handp->dir, 0, NULL, 0) == 0) handp->matched = 1; regfree(&re); retval = 0; done: ustr_free(expr); return retval; }
void lexicon_init (void) { Ustr *lexicons_path; FILE *lexicons; Ustr *line; AUstr words; lexicons_path = ustr_dup (cmdline_pkgdatadir ()); if (!lexicons_path) abort (); if (!ustr_add_cstr (&lexicons_path, "/lexicons")) abort (); line = ustr_dup_empty (); if (!line) abort (); austr_init (&words); lexicons = fopen (ustr_cstr (lexicons_path), "r"); if (!lexicons) abort (); lexicon_list = 0; while (errno = 0, ustr_sc_del (&line), ustr_io_getline (&line, lexicons)) { unsigned int i; wordsplit (&words, line); for (i = 0; i < austr_length (&words); ++i) { LexiconList *nlist; nlist = malloc (sizeof (LexiconList)); if (!nlist) abort (); nlist->name = ustr_dup (austr_i (&words, i)); nlist->lexicon = 0; nlist->next = lexicon_list; lexicon_list = nlist; } } if (errno != 0) { perror ("galcry-backend, lexicons"); fclose (lexicons); abort (); } fclose (lexicons); austr_deinit (&words); ustr_sc_free (&lexicons_path); }
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); }
void lexicon_lexicons_command (AUstr *command_line) { AUstr lexes; unsigned int i; austr_init (&lexes); lexicon_get_languages (&lexes); printf ("OK "); for (i = 0; i < austr_length (&lexes); ++i) printf ("%s ", ustr_cstr (austr_i (&lexes, i))); printf ("\n"); austr_deinit (&lexes); }
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) { /* 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); }
static int big_tst(size_t fsz, size_t usz, int hack, int tst_add) { #ifdef __linux__ int fd = -1; void *ptr = NULL; Ustr *s3 = NULL; ASSERT(ustr_set_cstr(&s2, "/tmp/ustr-build-tst-XXXXXX")); if ((fd = mkstemp(ustr_wstr(s2))) == -1) goto fail_mkstemp; unlink(ustr_cstr(s2)); usz /= 2; usz += 60; ASSERT(usz); if (!fsz) fsz = usz; if (ftruncate(fd, fsz) == -1) goto fail_ftrunc; ptr = mmap(0, fsz, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if (ptr == MAP_FAILED) goto fail_mmap; memset(ptr, '-', ((fsz < 32) ? fsz : 32)); if (hack) { if (USTR_DEBUG) goto fail_init; #if ! USTR_CONF_INCLUDE_INTERNAL_HEADERS goto fail_init; #else /* massive hack, ustr_assert_valid() fails here, as we aren't terminating */ s3 = ptr; s3->data[0] = USTR__BIT_ALLOCD | USTR__BIT_HAS_SZ | USTR__BIT_NEXACT; if (!USTR_CONF_HAVE_64bit_SIZE_MAX) s3->data[0] |= (1 << 2) | 1; else s3->data[0] |= (2 << 2) | 2; ustr__sz_set(s3, usz); ustr__len_set(s3, usz - 59); ustr__ref_set(s3, 1); #endif } else { if (!(s3 = ustr_init_alloc(ptr, usz, 0, 0, 0, 0, usz - 59))) goto fail_init; } ASSERT(ustr_sized(s3)); if (tst_add) { ASSERT(!ustr_add(&s3, s3)); ASSERT(!ustr_replace(&s3, USTR1(\1, "-"), s3, 0)); ASSERT(!ustr_replace_rep_chr(&s3, '-', 1, 'x', usz, 0)); } munmap(ptr, fsz); close(fd); /* ignore it because it wasn't actually allocated from the system */ return (EXIT_SUCCESS); fail_init: fail_mmap: fail_ftrunc: close(fd); fail_mkstemp: return (EXIT_FAILED_OK); #endif }
/* 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); }