Example #1
0
ph_hook_point_t *ph_hook_point_get_cstr(const char *name, bool create)
{
  ph_string_t sstr, *str;
  ph_hook_point_t *hp;

  if (create) {
    str = ph_string_make_cstr(mt.string, name);
  } else {
    uint32_t len = strlen(name);
    ph_string_init_claim(&sstr, PH_STRING_STATIC, (char*)name, len, len);
    str = &sstr;
  }

  hp = ph_hook_point_get(str, create);

  if (str != &sstr) {
    ph_string_delref(str);
  }

  return hp;
}
Example #2
0
int main(int argc, char **argv)
{
  ph_string_t *str, *str2;

  ph_unused_parameter(argc);
  ph_unused_parameter(argv);

  ph_library_init();
  plan_tests(102);

  mt_misc = ph_memtype_register(&mt_def);

  stack_tests();

  // Tests reallocation
  str = ph_string_make_empty(mt_misc, 16);
  is(ph_string_append_cstr(str, "1234567890"), PH_OK);
  is(10, ph_string_len(str));
  is(ph_string_append_cstr(str, "1234567890"), PH_OK);
  is(20, ph_string_len(str));
  is(memcmp(str->buf, "12345678901234567890", 20), 0);
  ph_string_delref(str);

  // Tests reallocation and string formatting
  str = ph_string_make_empty(mt_misc, 4);
  is(ph_string_printf(str, "Hello %s", "world"), 11);
  is(ph_string_len(str), 11);

  str2 = ph_string_make_empty(mt_misc, 10);
  is(ph_string_printf(str2, "copy `Ps%p", (void*)str), 16);
  diag(":%.*s:", str2->len, str2->buf);
  is(ph_string_len(str2), 16);
  is(memcmp(str2->buf, "copy Hello world", 16), 0);
  ph_string_delref(str2);

  str2 = ph_string_make_empty(mt_misc, 10);
  is(ph_string_printf(str2, "copy `Ps%d%p", 5, (void*)str), 10);
  is(memcmp(str2->buf, "copy Hello", 10), 0);
  ok(!ph_string_equal(str, str2), "not same");
  ph_string_delref(str2);

  str2 = ph_string_make_empty(mt_misc, 10);
  ph_string_append_buf(str2, str->buf, str->len);
  ok(ph_string_equal(str, str2), "same");
  is(ph_string_compare(str, str2), 0);

  ph_string_delref(str);
  ph_string_delref(str2);

  str = ph_string_make_cstr(mt_misc, "abc");
  str2 = ph_string_make_cstr(mt_misc, "bbc");
  ok(ph_string_compare(str, str2) < 0, "abc < bbc");
  ok(ph_string_compare(str2, str) > 0, "abc < bbc");

  ph_string_delref(str2);
  str2 = ph_string_make_cstr(mt_misc, "abca");
  ok(ph_string_compare(str, str2) < 0, "abc < abca");
  ok(ph_string_compare(str2, str) > 0, "abc < abca");

  ph_string_delref(str2);
  str2 = ph_string_make_cstr(mt_misc, "ab");
  ok(ph_string_compare(str, str2) > 0, "abc > ab");
  ok(ph_string_compare(str2, str) < 0, "abc > ab");

  ph_string_delref(str2);
  ph_string_delref(str);

  str = ph_string_make_printf(mt_misc, 16, "Hello %d", 42);
  ok(ph_string_equal_cstr(str, "Hello 42"), "same");
  ph_string_delref(str);

  utf16_tests();

  string_stream_tests();

  return exit_status();
}