Пример #1
0
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;
}
Пример #2
0
static int write_replacements(genhomedircon_settings_t * s, FILE * out,
			      const semanage_list_t * tpl,
			      const replacement_pair_t *repl)
{
	Ustr *line = USTR_NULL;

	for (; tpl; tpl = tpl->next) {
		line = replace_all(tpl->data, repl);
		if (!line)
			goto fail;
		if (check_line(s, line) == STATUS_SUCCESS) {
			if (!ustr_io_putfileline(&line, out))
				goto fail;
		}
		ustr_sc_free(&line);
	}
	return STATUS_SUCCESS;

      fail:
	ustr_sc_free(&line);
	return STATUS_ERR;
}
Пример #3
0
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);
}
Пример #4
0
void
lexicon_deinit (void)
{
  while (lexicon_list)
    {
      LexiconList *n;
      n = lexicon_list->next;
      ustr_sc_free (&lexicon_list->name);
      if (lexicon_list->lexicon)
	free (lexicon_list->lexicon);
      free (lexicon_list);
      lexicon_list = n;
    }
}
Пример #5
0
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);
}
Пример #6
0
static Ustr *replace_all(const char *str, const replacement_pair_t * repl)
{
	Ustr *retval = USTR_NULL;
	int i;

	if (!str || !repl)
		goto done;
	if (!(retval = ustr_dup_cstr(str)))
		goto done;

	for (i = 0; repl[i].search_for; i++) {
		ustr_replace_cstr(&retval, repl[i].search_for,
				  repl[i].replace_with, 0);
	}
	if (ustr_enomem(retval))
		ustr_sc_free(&retval);

      done:
	return retval;
}
Пример #7
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);
}
Пример #8
0
void
lexicon_generate (Ustr **s, Ustr const *language)
{
  LexiconList *sp;
  char ch[2];

  ustr_sc_del (s);
  ch[0] = 0;
  ch[1] = 0;

  /* Linear search.  Simple and we don't expect dozens of lexicons
     anyway.  */
  sp = lexicon_list;
  while (sp && !ustr_cmp_eq (language, sp->name))
    sp = sp->next;
  if (!sp)
    return;

  /* Load lexicon if not existent yet.  */
  if (!sp->lexicon)
    {
      Ustr *path;

      path = ustr_dup (cmdline_pkgdatadir ());
      if (!path)
	abort ();
      if (!ustr_add_cstr (&path, "/lexicon/"))
	abort ();
      if (!ustr_add (&path, sp->name))
	abort ();

      sp->lexicon = learn (path);

      ustr_sc_free (&path);
    }

  /* Generate.  */
  {
    unsigned int c0, c1, c2;
    unsigned long int sel;
    unsigned long int lim;
    unsigned int i;
    Lexicon *l;

    c0 = 0;
    l = sp->lexicon;

    /* Generate first char.  */
    for (i = 1, lim = 0; i < 27; ++i)
      lim += l->histogram2[0][i];
    /* Random select.  */
    sel = random () % lim;
    /* Find histogram area that random selection got in.  */
    for (c1 = 1; sel >= l->histogram2[0][c1]; ++c1)
      sel -= l->histogram2[0][c1];
    /* Add random selection.  */
    ch[0] = 96 + c1;
    if (!ustr_add_cstr (s, ch))
      abort ();

    /* Generate subsequent chars.  */
    do
      {
	/* Random select.  */
	sel = random () % l->histogram2[c0][c1];
	for (c2 = 0; sel >=l->histogram3[c0][c1][c2]; ++c2)
	  sel -= l->histogram3[c0][c1][c2];
	if (c2 != 0)
	  {
	    ch[0] = 96 + c2;
	    if (!ustr_add_cstr (s, ch))
	      abort ();
	  }
	c0 = c1;
	c1 = c2;
      }
    while (c2 != 0);
  }
}
Пример #9
0
/* 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;
}
Пример #10
0
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);
}
Пример #11
0
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);
}