📄 strfuncs.c
字号:
res = g_ascii_strcasecmp ("FroboZZ", "frobozz"); g_assert_cmpint (res, ==, 0); res = g_ascii_strcasecmp ("frobozz", "frobozz"); g_assert_cmpint (res, ==, 0); res = g_ascii_strcasecmp ("frobozz", "FROBOZZ"); g_assert_cmpint (res, ==, 0); res = g_ascii_strcasecmp ("FROBOZZ", "froboz"); g_assert_cmpint (res, !=, 0); res = g_ascii_strcasecmp ("", ""); g_assert_cmpint (res, ==, 0); res = g_ascii_strcasecmp ("!#%&/()", "!#%&/()"); g_assert_cmpint (res, ==, 0); res = g_ascii_strcasecmp ("a", "b"); g_assert_cmpint (res, <, 0); res = g_ascii_strcasecmp ("a", "B"); g_assert_cmpint (res, <, 0); res = g_ascii_strcasecmp ("A", "b"); g_assert_cmpint (res, <, 0); res = g_ascii_strcasecmp ("A", "B"); g_assert_cmpint (res, <, 0); res = g_ascii_strcasecmp ("b", "a"); g_assert_cmpint (res, >, 0); res = g_ascii_strcasecmp ("b", "A"); g_assert_cmpint (res, >, 0); res = g_ascii_strcasecmp ("B", "a"); g_assert_cmpint (res, >, 0); res = g_ascii_strcasecmp ("B", "A"); g_assert_cmpint (res, >, 0);}static voiddo_test_strchug (const gchar *str, const gchar *expected){ gchar *tmp; gboolean res; tmp = g_strdup (str); g_strchug (tmp); res = (strcmp (tmp, expected) == 0); g_free (tmp); g_assert_cmpint (res, ==, TRUE);}static voidtest_strchug (void){ if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR)) { g_strchug (NULL); } g_test_trap_assert_failed (); do_test_strchug ("", ""); do_test_strchug (" ", ""); do_test_strchug ("\t\r\n ", ""); do_test_strchug (" a", "a"); do_test_strchug (" a", "a"); do_test_strchug ("a a", "a a"); do_test_strchug (" a a", "a a");}static voiddo_test_strchomp (const gchar *str, const gchar *expected){ gchar *tmp; gboolean res; tmp = g_strdup (str); g_strchomp (tmp); res = (strcmp (tmp, expected) == 0); g_free (tmp); g_assert_cmpint (res, ==, TRUE);}static voidtest_strchomp (void){ if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR)) { g_strchomp (NULL); } g_test_trap_assert_failed (); do_test_strchomp ("", ""); do_test_strchomp (" ", ""); do_test_strchomp (" \t\r\n", ""); do_test_strchomp ("a ", "a"); do_test_strchomp ("a ", "a"); do_test_strchomp ("a a", "a a"); do_test_strchomp ("a a ", "a a");}static voidtest_strreverse (void){ gchar *str; gchar *p; if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR)) { str = g_strreverse (NULL); } g_test_trap_assert_failed (); str = p = g_strdup ("abcde"); str = g_strreverse (str); g_assert (str != NULL); g_assert (p == str); g_assert_cmpstr (str, ==, "edcba"); g_free (str);}static voidtest_strstr (void){ gchar *haystack; gchar *res; haystack = g_strdup ("FooBarFooBarFoo"); /* strstr_len */ res = g_strstr_len (haystack, 6, "xxx"); g_assert (res == NULL); res = g_strstr_len (haystack, 6, "FooBarFooBarFooBar"); g_assert (res == NULL); res = g_strstr_len (haystack, 3, "Bar"); g_assert (res == NULL); res = g_strstr_len (haystack, 6, ""); g_assert (res == haystack); g_assert_cmpstr (res, ==, "FooBarFooBarFoo"); res = g_strstr_len (haystack, 6, "Bar"); g_assert (res == haystack + 3); g_assert_cmpstr (res, ==, "BarFooBarFoo"); res = g_strstr_len (haystack, -1, "Bar"); g_assert (res == haystack + 3); g_assert_cmpstr (res, ==, "BarFooBarFoo"); /* strrstr */ res = g_strrstr (haystack, "xxx"); g_assert (res == NULL); res = g_strrstr (haystack, "FooBarFooBarFooBar"); g_assert (res == NULL); res = g_strrstr (haystack, ""); g_assert (res == haystack); g_assert_cmpstr (res, ==, "FooBarFooBarFoo"); res = g_strrstr (haystack, "Bar"); g_assert (res == haystack + 9); g_assert_cmpstr (res, ==, "BarFoo"); /* strrstr_len */ res = g_strrstr_len (haystack, 14, "xxx"); g_assert (res == NULL); res = g_strrstr_len (haystack, 14, "FooBarFooBarFooBar"); g_assert (res == NULL); res = g_strrstr_len (haystack, 3, "Bar"); g_assert (res == NULL); res = g_strrstr_len (haystack, 14, "BarFoo"); g_assert (res == haystack + 3); g_assert_cmpstr (res, ==, "BarFooBarFoo"); res = g_strrstr_len (haystack, 15, "BarFoo"); g_assert (res == haystack + 9); g_assert_cmpstr (res, ==, "BarFoo"); res = g_strrstr_len (haystack, -1, "BarFoo"); g_assert (res == haystack + 9); g_assert_cmpstr (res, ==, "BarFoo"); /* test case for strings with \0 in the middle */ *(haystack + 7) = '\0'; res = g_strstr_len (haystack, 15, "BarFoo"); g_assert (res == NULL); g_free (haystack);}static voidtest_has_prefix (void){ gboolean res; if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR)) { res = g_str_has_prefix ("foo", NULL); } g_test_trap_assert_failed (); if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR)) { res = g_str_has_prefix (NULL, "foo"); } g_test_trap_assert_failed (); res = g_str_has_prefix ("foo", "bar"); g_assert_cmpint (res, ==, FALSE); res = g_str_has_prefix ("foo", "foobar"); g_assert_cmpint (res, ==, FALSE); res = g_str_has_prefix ("foobar", "bar"); g_assert_cmpint (res, ==, FALSE); res = g_str_has_prefix ("foobar", "foo"); g_assert_cmpint (res, ==, TRUE); res = g_str_has_prefix ("foo", ""); g_assert_cmpint (res, ==, TRUE); res = g_str_has_prefix ("foo", "foo"); g_assert_cmpint (res, ==, TRUE); res = g_str_has_prefix ("", ""); g_assert_cmpint (res, ==, TRUE);}static voidtest_has_suffix (void){ gboolean res; if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR)) { res = g_str_has_suffix ("foo", NULL); } g_test_trap_assert_failed (); if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR)) { res = g_str_has_suffix (NULL, "foo"); } g_test_trap_assert_failed (); res = g_str_has_suffix ("foo", "bar"); g_assert_cmpint (res, ==, FALSE); res = g_str_has_suffix ("bar", "foobar"); g_assert_cmpint (res, ==, FALSE); res = g_str_has_suffix ("foobar", "foo"); g_assert_cmpint (res, ==, FALSE); res = g_str_has_suffix ("foobar", "bar"); g_assert_cmpint (res, ==, TRUE); res = g_str_has_suffix ("foo", ""); g_assert_cmpint (res, ==, TRUE); res = g_str_has_suffix ("foo", "foo"); g_assert_cmpint (res, ==, TRUE); res = g_str_has_suffix ("", ""); g_assert_cmpint (res, ==, TRUE);}static voidstrv_check (gchar **strv, ...){ gboolean ok = TRUE; gint i = 0; va_list list; va_start (list, strv); while (ok) { const gchar *str = va_arg (list, const char *); if (strv[i] == NULL) { g_assert (str == NULL); break; } if (str == NULL) { ok = FALSE; } else { g_assert_cmpstr (strv[i], ==, str); } i++; } va_end (list); g_strfreev (strv);}static voidtest_strsplit (void){ strv_check (g_strsplit ("", ",", 0), NULL); strv_check (g_strsplit ("x", ",", 0), "x", NULL); strv_check (g_strsplit ("x,y", ",", 0), "x", "y", NULL); strv_check (g_strsplit ("x,y,", ",", 0), "x", "y", "", NULL); strv_check (g_strsplit (",x,y", ",", 0), "", "x", "y", NULL); strv_check (g_strsplit (",x,y,", ",", 0), "", "x", "y", "", NULL); strv_check (g_strsplit ("x,y,z", ",", 0), "x", "y", "z", NULL); strv_check (g_strsplit ("x,y,z,", ",", 0), "x", "y", "z", "", NULL); strv_check (g_strsplit (",x,y,z", ",", 0), "", "x", "y", "z", NULL); strv_check (g_strsplit (",x,y,z,", ",", 0), "", "x", "y", "z", "", NULL); strv_check (g_strsplit (",,x,,y,,z,,", ",", 0), "", "", "x", "", "y", "", "z", "", "", NULL); strv_check (g_strsplit (",,x,,y,,z,,", ",,", 0), "", "x", "y", "z", "", NULL); strv_check (g_strsplit ("", ",", 1), NULL); strv_check (g_strsplit ("x", ",", 1), "x", NULL); strv_check (g_strsplit ("x,y", ",", 1), "x,y", NULL); strv_check (g_strsplit ("x,y,", ",", 1), "x,y,", NULL); strv_check (g_strsplit (",x,y", ",", 1), ",x,y", NULL); strv_check (g_strsplit (",x,y,", ",", 1), ",x,y,", NULL); strv_check (g_strsplit ("x,y,z", ",", 1), "x,y,z", NULL); strv_check (g_strsplit ("x,y,z,", ",", 1), "x,y,z,", NULL); strv_check (g_strsplit (",x,y,z", ",", 1), ",x,y,z", NULL); strv_check (g_strsplit (",x,y,z,", ",", 1), ",x,y,z,", NULL); strv_check (g_strsplit (",,x,,y,,z,,", ",", 1), ",,x,,y,,z,,", NULL); strv_check (g_strsplit (",,x,,y,,z,,", ",,", 1), ",,x,,y,,z,,", NULL); strv_check (g_strsplit ("", ",", 2), NULL); strv_check (g_strsplit ("x", ",", 2), "x", NULL); strv_check (g_strsplit ("x,y", ",", 2), "x", "y", NULL); strv_check (g_strsplit ("x,y,", ",", 2), "x", "y,", NULL); strv_check (g_strsplit (",x,y", ",", 2), "", "x,y", NULL); strv_check (g_strsplit (",x,y,", ",", 2), "", "x,y,", NULL); strv_check (g_strsplit ("x,y,z", ",", 2), "x", "y,z", NULL); strv_check (g_strsplit ("x,y,z,", ",", 2), "x", "y,z,", NULL); strv_check (g_strsplit (",x,y,z", ",", 2), "", "x,y,z", NULL); strv_check (g_strsplit (",x,y,z,", ",", 2), "", "x,y,z,", NULL); strv_check (g_strsplit (",,x,,y,,z,,", ",", 2), "", ",x,,y,,z,,", NULL); strv_check (g_strsplit (",,x,,y,,z,,", ",,", 2), "", "x,,y,,z,,", NULL);}static voidtest_strsplit_set (void){ strv_check (g_strsplit_set ("", ",/", 0), NULL); strv_check (g_strsplit_set (":def/ghi:", ":/", -1), "", "def", "ghi", "", NULL); strv_check (g_strsplit_set ("abc:def/ghi", ":/", -1), "abc", "def", "ghi", NULL); strv_check (g_strsplit_set (",;,;,;,;", ",;", -1), "", "", "", "", "", "", "", "", "", NULL); strv_check (g_strsplit_set (",,abc.def", ".,", -1), "", "", "abc", "def", NULL); strv_check (g_strsplit_set (",x.y", ",.", 0), "", "x", "y", NULL); strv_check (g_strsplit_set (".x,y,", ",.", 0), "", "x", "y", "", NULL); strv_check (g_strsplit_set ("x,y.z", ",.", 0), "x", "y", "z", NULL); strv_check (g_strsplit_set ("x.y,z,", ",.", 0), "x", "y", "z", "", NULL); strv_check (g_strsplit_set (",x.y,z", ",.", 0), "", "x", "y", "z", NULL); strv_check (g_strsplit_set (",x,y,z,", ",.", 0), "", "x", "y", "z", "", NULL); strv_check (g_strsplit_set (",.x,,y,;z..", ".,;", 0), "", "", "x", "", "y", "", "z", "", "", NULL); strv_check (g_strsplit_set (",,x,,y,,z,,", ",,", 0), "", "", "x", "", "y", "", "z", "", "", NULL); strv_check (g_strsplit_set ("x,y.z", ",.", 1), "x,y.z", NULL); strv_check (g_strsplit_set ("x.y,z,", ",.", 1), "x.y,z,", NULL); strv_check (g_strsplit_set (",x,y,z", ",.", 1), ",x,y,z", NULL); strv_check (g_strsplit_set (",x,y.z,", ",.", 1), ",x,y.z,", NULL); strv_check (g_strsplit_set (",,x,.y,,z,,", ",.", 1), ",,x,.y,,z,,", NULL); strv_check (g_strsplit_set (",.x,,y,,z,,", ",,..", 1), ",.x,,y,,z,,", NULL); strv_check (g_strsplit_set ("", ",", 0), NULL); strv_check (g_strsplit_set ("x", ",", 0), "x", NULL); strv_check (g_strsplit_set ("x,y", ",", 0), "x", "y", NULL); strv_check (g_strsplit_set ("x,y,", ",", 0), "x", "y", "", NULL); strv_check (g_strsplit_set (",x,y", ",", 0), "", "x", "y", NULL); strv_check (g_strsplit_set (",x,y,", ",", 0), "", "x", "y", "", NULL); strv_check (g_strsplit_set ("x,y,z", ",", 0), "x", "y", "z", NULL); strv_check (g_strsplit_set ("x,y,z,", ",", 0), "x", "y", "z", "", NULL); strv_check (g_strsplit_set (",x,y,z", ",", 0), "", "x", "y", "z", NULL); strv_check (g_strsplit_set (",x,y,z,", ",", 0), "", "x", "y", "z", "", NULL); strv_check (g_strsplit_set (",,x,,y,,z,,", ",", 0), "", "", "x", "", "y", "", "z", "", "", NULL); strv_check (g_strsplit_set ("", ",", 1), NULL); strv_check (g_strsplit_set ("x", ",", 1), "x", NULL); strv_check (g_strsplit_set ("x,y", ",", 1), "x,y", NULL); strv_check (g_strsplit_set ("x,y,", ",", 1), "x,y,", NULL); strv_check (g_strsplit_set (",x,y", ",", 1), ",x,y", NULL); strv_check (g_strsplit_set (",x,y,", ",", 1), ",x,y,", NULL); strv_check (g_strsplit_set ("x,y,z", ",", 1), "x,y,z", NULL); strv_check (g_strsplit_set ("x,y,z,", ",", 1), "x,y,z,", NULL); strv_check (g_strsplit_set (",x,y,z", ",", 1), ",x,y,z", NULL); strv_check (g_strsplit_set (",x,y,z,", ",", 1), ",x,y,z,", NULL); strv_check (g_strsplit_set (",,x,,y,,z,,", ",", 1), ",,x,,y,,z,,", NULL); strv_check (g_strsplit_set (",,x,,y,,z,,", ",,", 1), ",,x,,y,,z,,", NULL); strv_check (g_strsplit_set ("", ",", 2), NULL); strv_check (g_strsplit_set ("x", ",", 2), "x", NULL); strv_check (g_strsplit_set ("x,y", ",", 2), "x", "y", NULL); strv_check (g_strsplit_set ("x,y,", ",", 2), "x", "y,", NULL); strv_check (g_strsplit_set (",x,y", ",", 2), "", "x,y", NULL); strv_check (g_strsplit_set (",x,y,", ",", 2), "", "x,y,", NULL); strv_check (g_strsplit_set ("x,y,z", ",", 2), "x", "y,z", NULL); strv_check (g_strsplit_set ("x,y,z,", ",", 2), "x", "y,z,", NULL);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -