* reindentation, fix mu_str_ascii_xapian_escape_in_place for '..'

This commit is contained in:
Dirk-Jan C. Binnema 2011-05-22 10:27:12 +03:00
parent 000805ae1f
commit e478eeee6d
2 changed files with 252 additions and 243 deletions

View File

@ -1,5 +1,7 @@
/* -*-mode: c; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-*/
/* /*
** Copyright (C) 2010 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl> ** Copyright (C) 2008-2011 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
** **
** This program is free software; you can redistribute it and/or modify ** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by ** it under the terms of the GNU General Public License as published by
@ -179,7 +181,7 @@ cleanup_contact (char *contact)
* good to cleanup corporate contact address spam... */ * good to cleanup corporate contact address spam... */
c = g_strstr_len (contact, -1, "("); c = g_strstr_len (contact, -1, "(");
if (c && c - contact > 5) if (c && c - contact > 5)
*c = '\0'; *c = '\0';
g_strstrip (contact); g_strstrip (contact);
} }
@ -289,37 +291,37 @@ is_xapian_prefix (const char *q, const char *colon)
time_t time_t
mu_str_date_parse_hdwmy (const char* str) mu_str_date_parse_hdwmy (const char* str)
{ {
long int num; long int num;
char *end; char *end;
time_t now, delta; time_t now, delta;
time_t never = (time_t)-1; time_t never = (time_t)-1;
g_return_val_if_fail (str, never); g_return_val_if_fail (str, never);
num = strtol (str, &end, 10); num = strtol (str, &end, 10);
if (num <= 0 || num > 9999) if (num <= 0 || num > 9999)
return never; return never;
if (!end || end[1] != '\0') if (!end || end[1] != '\0')
return never; return never;
switch (end[0]) { switch (end[0]) {
case 'h': /* hour */ case 'h': /* hour */
delta = num * 60 * 60; break; delta = num * 60 * 60; break;
case 'd': /* day */ case 'd': /* day */
delta = num * 24 * 60 * 60; break; delta = num * 24 * 60 * 60; break;
case 'w': /* week */ case 'w': /* week */
delta = num * 7 * 24 * 60 * 60; break; delta = num * 7 * 24 * 60 * 60; break;
case 'm': case 'm':
delta = num * 30 * 24 * 60 * 60; break; delta = num * 30 * 24 * 60 * 60; break;
case 'y': case 'y':
delta = num * 365 * 24 * 60 * 60; break; delta = num * 365 * 24 * 60 * 60; break;
default: default:
return never; return never;
} }
now = time(NULL); now = time(NULL);
return delta <= now ? now - delta : never; return delta <= now ? now - delta : never;
} }
guint64 guint64
@ -341,13 +343,17 @@ mu_str_size_parse_kmg (const char* str)
case 'b': return num; /* bytes */ case 'b': return num; /* bytes */
case 'k': return num * 1000; /* kilobyte */ case 'k': return num * 1000; /* kilobyte */
case 'm': return num * 1000 * 1000; /* megabyte */ case 'm': return num * 1000 * 1000; /* megabyte */
/* case 'g': return num * 1000 * 1000 * 1000; /\* gigabyte *\/ */ /* case 'g': return num * 1000 * 1000 * 1000; /\* gigabyte *\/ */
default: default:
return G_MAXUINT64; return G_MAXUINT64;
} }
} }
/*
* Xapian treats various characters such as '@', '-', ':' and '.'
* specially; function below is an ugly hack to make it DWIM in most
* cases...*/
char* char*
mu_str_ascii_xapian_escape_in_place (char *query) mu_str_ascii_xapian_escape_in_place (char *query)
{ {
@ -361,26 +367,25 @@ mu_str_ascii_xapian_escape_in_place (char *query)
replace_dot = (g_strstr_len(query, -1, "@") != NULL); replace_dot = (g_strstr_len(query, -1, "@") != NULL);
for (cur = query; *cur; ++cur) { for (cur = query; *cur; ++cur) {
*cur = tolower(*cur);
if (*cur == '@' || *cur == '-') if (*cur == '@' || *cur == '-')
*cur = '_'; *cur = '_';
else if (*cur == '.') { else if (*cur == '.') {
/* don't replace a final cur */ /* don't replace a final cur */
if (cur[1] == '.' || cur[1]== ' ' || cur[1]=='\t') if (cur[1]== ' ' || cur[1]=='\t' || cur[1] == '\0' ||
cur += 2; cur[1]== '.')
else if (cur[1] == '\0')
++cur; ++cur;
else else
*cur = '_'; *cur = '_';
} else if (*cur == ':') { } else if (*cur == ':') {
/* if there's a registered xapian prefix before the /* if there's a registered xapian prefix before the
* ':', don't touch it. Otherwise replace ':' with * ':', don't touch it. Otherwise replace ':' with
* a space'... ugly... * a space'... ugh yuck ugly...
*/ */
if (!is_xapian_prefix (query, cur)) if (!is_xapian_prefix (query, cur))
*cur = '_'; *cur = '_';
} else }
*cur = tolower(*cur);
} }
return query; return query;

View File

@ -1,3 +1,5 @@
/* -*-mode: c; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-*/
/* /*
** Copyright (C) 2008-2010 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl> ** Copyright (C) 2008-2010 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
** **
@ -35,26 +37,26 @@
static void static void
test_mu_str_date_01 (void) test_mu_str_date_01 (void)
{ {
struct tm *tmbuf; struct tm *tmbuf;
char buf[64]; char buf[64];
gchar *tmp; gchar *tmp;
time_t some_time; time_t some_time;
some_time = 1234567890; some_time = 1234567890;
tmbuf = localtime (&some_time); tmbuf = localtime (&some_time);
strftime (buf, 64, "%x", tmbuf); strftime (buf, 64, "%x", tmbuf);
/* $ date -ud@1234567890; Fri Feb 13 23:31:30 UTC 2009 */ /* $ date -ud@1234567890; Fri Feb 13 23:31:30 UTC 2009 */
g_assert_cmpstr (mu_str_date_s ("%x", some_time), ==, buf); g_assert_cmpstr (mu_str_date_s ("%x", some_time), ==, buf);
/* date -ud@987654321 Thu Apr 19 04:25:21 UTC 2001 */ /* date -ud@987654321 Thu Apr 19 04:25:21 UTC 2001 */
some_time = 987654321; some_time = 987654321;
tmbuf = localtime (&some_time); tmbuf = localtime (&some_time);
strftime (buf, 64, "%c", tmbuf); strftime (buf, 64, "%c", tmbuf);
tmp = mu_str_date ("%c", some_time); tmp = mu_str_date ("%c", some_time);
g_assert_cmpstr (tmp, ==, buf); g_assert_cmpstr (tmp, ==, buf);
g_free (tmp); g_free (tmp);
} }
@ -62,22 +64,22 @@ test_mu_str_date_01 (void)
static void static void
test_mu_str_size_01 (void) test_mu_str_size_01 (void)
{ {
struct lconv *lc; struct lconv *lc;
char *tmp2; char *tmp2;
lc = localeconv(); lc = localeconv();
tmp2 = g_strdup_printf ("0%s0 kB", lc->decimal_point); tmp2 = g_strdup_printf ("0%s0 kB", lc->decimal_point);
g_assert_cmpstr (mu_str_size_s (0), ==, tmp2); g_assert_cmpstr (mu_str_size_s (0), ==, tmp2);
g_free (tmp2); g_free (tmp2);
tmp2 = g_strdup_printf ("100%s0 kB", lc->decimal_point); tmp2 = g_strdup_printf ("100%s0 kB", lc->decimal_point);
g_assert_cmpstr (mu_str_size_s (100000), ==, tmp2); g_assert_cmpstr (mu_str_size_s (100000), ==, tmp2);
g_free (tmp2); g_free (tmp2);
tmp2 = g_strdup_printf ("1%s1 MB", lc->decimal_point); tmp2 = g_strdup_printf ("1%s1 MB", lc->decimal_point);
g_assert_cmpstr (mu_str_size_s (1100*1000), ==, tmp2); g_assert_cmpstr (mu_str_size_s (1100*1000), ==, tmp2);
g_free (tmp2); g_free (tmp2);
} }
@ -85,17 +87,17 @@ test_mu_str_size_01 (void)
static void static void
test_mu_str_size_02 (void) test_mu_str_size_02 (void)
{ {
struct lconv *lc; struct lconv *lc;
char *tmp1, *tmp2; char *tmp1, *tmp2;
lc = localeconv(); lc = localeconv();
tmp2 = g_strdup_printf ("1%s0 MB", lc->decimal_point); tmp2 = g_strdup_printf ("1%s0 MB", lc->decimal_point);
tmp1 = mu_str_size (999999); tmp1 = mu_str_size (999999);
g_assert_cmpstr (tmp1, !=, tmp2); g_assert_cmpstr (tmp1, !=, tmp2);
g_free (tmp1); g_free (tmp1);
g_free (tmp2); g_free (tmp2);
} }
@ -103,26 +105,26 @@ test_mu_str_size_02 (void)
static void static void
test_mu_str_prio_01 (void) test_mu_str_prio_01 (void)
{ {
g_assert_cmpstr(mu_msg_prio_name(MU_MSG_PRIO_LOW), ==, "low"); g_assert_cmpstr(mu_msg_prio_name(MU_MSG_PRIO_LOW), ==, "low");
g_assert_cmpstr(mu_msg_prio_name(MU_MSG_PRIO_NORMAL), ==, "normal"); g_assert_cmpstr(mu_msg_prio_name(MU_MSG_PRIO_NORMAL), ==, "normal");
g_assert_cmpstr(mu_msg_prio_name(MU_MSG_PRIO_HIGH), ==, "high"); g_assert_cmpstr(mu_msg_prio_name(MU_MSG_PRIO_HIGH), ==, "high");
} }
static gboolean static gboolean
ignore_error (const char* log_domain, GLogLevelFlags log_level, ignore_error (const char* log_domain, GLogLevelFlags log_level,
const gchar* msg, gpointer user_data) const gchar* msg, gpointer user_data)
{ {
return FALSE; /* don't abort */ return FALSE; /* don't abort */
} }
static void static void
test_mu_str_prio_02 (void) test_mu_str_prio_02 (void)
{ {
/* this must fail */ /* this must fail */
g_test_log_set_fatal_handler ((GTestLogFatalFunc)ignore_error, NULL); g_test_log_set_fatal_handler ((GTestLogFatalFunc)ignore_error, NULL);
g_assert_cmpstr (mu_msg_prio_name(666), ==, NULL); g_assert_cmpstr (mu_msg_prio_name(666), ==, NULL);
} }
@ -130,93 +132,95 @@ test_mu_str_prio_02 (void)
static void static void
test_mu_str_normalize_01 (void) test_mu_str_normalize_01 (void)
{ {
int i; int i;
struct { struct {
const char* word; const char* word;
const char* norm; const char* norm;
} words [] = { } words [] = {
{ "dantès", "dantes"}, { "dantès", "dantes"},
{ "foo", "foo" }, { "foo", "foo" },
{ "Föö", "foo" }, { "Föö", "foo" },
{ "číslo", "cislo" }, { "číslo", "cislo" },
{ "hÆvý mëÐal ümláõt", "haevy medal umlaot"} { "hÆvý mëÐal ümláõt", "haevy medal umlaot"}
}; };
for (i = 0; i != G_N_ELEMENTS(words); ++i) { for (i = 0; i != G_N_ELEMENTS(words); ++i) {
gchar *str; gchar *str;
str = mu_str_normalize (words[i].word, TRUE); str = mu_str_normalize (words[i].word, TRUE);
g_assert_cmpstr (str, ==, words[i].norm); g_assert_cmpstr (str, ==, words[i].norm);
g_free (str); g_free (str);
} }
} }
static void static void
test_mu_str_normalize_02 (void) test_mu_str_normalize_02 (void)
{ {
int i; int i;
struct { struct {
const char* word; const char* word;
const char* norm; const char* norm;
} words [] = { } words [] = {
{ "DantèS", "DanteS"}, { "DantèS", "DanteS"},
{ "foo", "foo" }, { "foo", "foo" },
{ "Föö", "Foo" }, { "Föö", "Foo" },
{ "číslO", "cislO" }, { "číslO", "cislO" },
{ "hÆvý mëÐal ümláõt", "hAevy meDal umlaot"} { "hÆvý mëÐal ümláõt", "hAevy meDal umlaot"}
}; };
for (i = 0; i != G_N_ELEMENTS(words); ++i) { for (i = 0; i != G_N_ELEMENTS(words); ++i) {
gchar *str; gchar *str;
str = mu_str_normalize (words[i].word, FALSE); str = mu_str_normalize (words[i].word, FALSE);
g_assert_cmpstr (str, ==, words[i].norm); g_assert_cmpstr (str, ==, words[i].norm);
g_free (str); g_free (str);
} }
} }
static void static void
test_mu_str_ascii_xapian_escape (void) test_mu_str_ascii_xapian_escape (void)
{ {
int i; int i;
struct { struct {
const char* word; const char* word;
const char* esc; const char* esc;
} words [] = { } words [] = {
{ "aap@noot.mies", "aap_noot_mies"}, { "aap@noot.mies", "aap_noot_mies"},
{ "Foo..Bar", "foo..bar" }, { "Foo..Bar", "foo..bar" },
{ "subject:test@foo", "subject:test_foo" }, { "Foo.Bar", "foo_bar" },
{ "xxx:test@bar", "xxx_test_bar" }, { "Foo. Bar", "foo. bar" },
}; { "subject:test@foo", "subject:test_foo" },
{ "xxx:test@bar", "xxx_test_bar" },
};
for (i = 0; i != G_N_ELEMENTS(words); ++i) { for (i = 0; i != G_N_ELEMENTS(words); ++i) {
gchar *a = g_strdup (words[i].word); gchar *a = g_strdup (words[i].word);
mu_str_ascii_xapian_escape_in_place (a); mu_str_ascii_xapian_escape_in_place (a);
g_assert_cmpstr (a, ==, words[i].esc); g_assert_cmpstr (a, ==, words[i].esc);
g_free (a); g_free (a);
} }
} }
static void static void
test_mu_str_display_contact (void) test_mu_str_display_contact (void)
{ {
int i; int i;
struct { struct {
const char* word; const char* word;
const char* disp; const char* disp;
} words [] = { } words [] = {
{ "\"Foo Bar\" <aap@noot.mies>", "Foo Bar"}, { "\"Foo Bar\" <aap@noot.mies>", "Foo Bar"},
{ "Foo Bar <aap@noot.mies>", "Foo Bar" }, { "Foo Bar <aap@noot.mies>", "Foo Bar" },
{ "<aap@noot.mies>", "aap@noot.mies" }, { "<aap@noot.mies>", "aap@noot.mies" },
{ "foo@bar.nl", "foo@bar.nl" } { "foo@bar.nl", "foo@bar.nl" }
}; };
for (i = 0; i != G_N_ELEMENTS(words); ++i) for (i = 0; i != G_N_ELEMENTS(words); ++i)
g_assert_cmpstr (mu_str_display_contact_s (words[i].word), ==, g_assert_cmpstr (mu_str_display_contact_s (words[i].word), ==,
words[i].disp); words[i].disp);
} }
@ -224,78 +228,78 @@ test_mu_str_display_contact (void)
static void static void
test_mu_str_date_parse_hdwmy (void) test_mu_str_date_parse_hdwmy (void)
{ {
time_t diff; time_t diff;
diff = time(NULL) - mu_str_date_parse_hdwmy ("3h"); diff = time(NULL) - mu_str_date_parse_hdwmy ("3h");
g_assert (diff > 0); g_assert (diff > 0);
g_assert_cmpuint (3 * 60 * 60 - diff, <=, 1); g_assert_cmpuint (3 * 60 * 60 - diff, <=, 1);
diff = time(NULL) - mu_str_date_parse_hdwmy ("5y"); diff = time(NULL) - mu_str_date_parse_hdwmy ("5y");
g_assert (diff > 0); g_assert (diff > 0);
g_assert_cmpuint (5 * 365 * 24 * 60 * 60 - diff, <=, 1); g_assert_cmpuint (5 * 365 * 24 * 60 * 60 - diff, <=, 1);
diff = time(NULL) - mu_str_date_parse_hdwmy ("3m"); diff = time(NULL) - mu_str_date_parse_hdwmy ("3m");
g_assert (diff > 0); g_assert (diff > 0);
g_assert_cmpuint (3 * 30 * 24 * 60 * 60 - diff, <=, 1); g_assert_cmpuint (3 * 30 * 24 * 60 * 60 - diff, <=, 1);
diff = time(NULL) - mu_str_date_parse_hdwmy ("21d"); diff = time(NULL) - mu_str_date_parse_hdwmy ("21d");
g_assert (diff > 0); g_assert (diff > 0);
g_assert_cmpuint (21 * 24 * 60 * 60 - diff, <=, 1); g_assert_cmpuint (21 * 24 * 60 * 60 - diff, <=, 1);
diff = time(NULL) - mu_str_date_parse_hdwmy ("2w"); diff = time(NULL) - mu_str_date_parse_hdwmy ("2w");
g_assert (diff > 0); g_assert (diff > 0);
g_assert_cmpuint (2 * 7 * 24 * 60 * 60 - diff, <=, 1); g_assert_cmpuint (2 * 7 * 24 * 60 * 60 - diff, <=, 1);
g_assert_cmpint (mu_str_date_parse_hdwmy("-1y"),==, (time_t)-1); g_assert_cmpint (mu_str_date_parse_hdwmy("-1y"),==, (time_t)-1);
} }
static void static void
test_mu_str_guess_first_name (void) test_mu_str_guess_first_name (void)
{ {
int i; int i;
struct { struct {
char *src, *exp; char *src, *exp;
} tests[] = { } tests[] = {
{ "Richard M. Stallman", "Richard M." }, { "Richard M. Stallman", "Richard M." },
{ "John Rambo", "John" }, { "John Rambo", "John" },
{ "Ivanhoe", "Ivanhoe" }, { "Ivanhoe", "Ivanhoe" },
{ "", "" } { "", "" }
}; };
for (i = 0; i != G_N_ELEMENTS(tests); ++i) { for (i = 0; i != G_N_ELEMENTS(tests); ++i) {
gchar *s; gchar *s;
s = mu_str_guess_first_name (tests[i].src); s = mu_str_guess_first_name (tests[i].src);
g_assert_cmpstr (s, ==, tests[i].exp); g_assert_cmpstr (s, ==, tests[i].exp);
g_free (s); g_free (s);
} }
} }
static void static void
test_mu_str_guess_last_name (void) test_mu_str_guess_last_name (void)
{ {
int i; int i;
struct { struct {
char *src, *exp; char *src, *exp;
} tests[] = { } tests[] = {
{ "Richard M. Stallman", "Stallman" }, { "Richard M. Stallman", "Stallman" },
{ "John Rambo", "Rambo" }, { "John Rambo", "Rambo" },
{ "Ivanhoe", "" }, { "Ivanhoe", "" },
{ "", "" } { "", "" }
}; };
for (i = 0; i != G_N_ELEMENTS(tests); ++i) { for (i = 0; i != G_N_ELEMENTS(tests); ++i) {
gchar *s; gchar *s;
s = mu_str_guess_last_name (tests[i].src); s = mu_str_guess_last_name (tests[i].src);
g_assert_cmpstr (s, ==, tests[i].exp); g_assert_cmpstr (s, ==, tests[i].exp);
g_free (s); g_free (s);
} }
} }
@ -303,24 +307,24 @@ test_mu_str_guess_last_name (void)
static void static void
test_mu_str_guess_nick (void) test_mu_str_guess_nick (void)
{ {
int i; int i;
struct { struct {
char *src, *exp; char *src, *exp;
} tests[] = { } tests[] = {
{ "Richard M. Stallman", "RichardMS" }, { "Richard M. Stallman", "RichardMS" },
{ "John Rambo", "JohnR" }, { "John Rambo", "JohnR" },
{ "Ivanhoe", "Ivanhoe" }, { "Ivanhoe", "Ivanhoe" },
{ "", "" } { "", "" }
}; };
for (i = 0; i != G_N_ELEMENTS(tests); ++i) { for (i = 0; i != G_N_ELEMENTS(tests); ++i) {
gchar *s; gchar *s;
s = mu_str_guess_nick (tests[i].src); s = mu_str_guess_nick (tests[i].src);
g_assert_cmpstr (s, ==, tests[i].exp); g_assert_cmpstr (s, ==, tests[i].exp);
g_free (s); g_free (s);
} }
} }
@ -331,53 +335,53 @@ test_mu_str_guess_nick (void)
int int
main (int argc, char *argv[]) main (int argc, char *argv[])
{ {
g_test_init (&argc, &argv, NULL); g_test_init (&argc, &argv, NULL);
/* mu_str_date */ /* mu_str_date */
g_test_add_func ("/mu-str/mu-str-date", g_test_add_func ("/mu-str/mu-str-date",
test_mu_str_date_01); test_mu_str_date_01);
/* mu_str_size */ /* mu_str_size */
g_test_add_func ("/mu-str/mu-str-size-01", g_test_add_func ("/mu-str/mu-str-size-01",
test_mu_str_size_01); test_mu_str_size_01);
g_test_add_func ("/mu-str/mu-str-size-02", g_test_add_func ("/mu-str/mu-str-size-02",
test_mu_str_size_02); test_mu_str_size_02);
/* mu_str_prio */ /* mu_str_prio */
g_test_add_func ("/mu-str/mu-str-prio-01", g_test_add_func ("/mu-str/mu-str-prio-01",
test_mu_str_prio_01); test_mu_str_prio_01);
g_test_add_func ("/mu-str/mu-str-prio-02", g_test_add_func ("/mu-str/mu-str-prio-02",
test_mu_str_prio_02); test_mu_str_prio_02);
/* mu_str_normalize */ /* mu_str_normalize */
g_test_add_func ("/mu-str/mu-str-normalize-01", g_test_add_func ("/mu-str/mu-str-normalize-01",
test_mu_str_normalize_01); test_mu_str_normalize_01);
g_test_add_func ("/mu-str/mu-str-normalize-02", g_test_add_func ("/mu-str/mu-str-normalize-02",
test_mu_str_normalize_02); test_mu_str_normalize_02);
g_test_add_func ("/mu-str/mu-str-ascii-xapian-escape", g_test_add_func ("/mu-str/mu-str-ascii-xapian-escape",
test_mu_str_ascii_xapian_escape); test_mu_str_ascii_xapian_escape);
g_test_add_func ("/mu-str/mu-str-display_contact", g_test_add_func ("/mu-str/mu-str-display_contact",
test_mu_str_display_contact); test_mu_str_display_contact);
g_test_add_func ("/mu-str/mu-str_date_parse_hdwmy", g_test_add_func ("/mu-str/mu-str_date_parse_hdwmy",
test_mu_str_date_parse_hdwmy); test_mu_str_date_parse_hdwmy);
g_test_add_func ("/mu-str/mu-str_guess_first_name", g_test_add_func ("/mu-str/mu-str_guess_first_name",
test_mu_str_guess_first_name); test_mu_str_guess_first_name);
g_test_add_func ("/mu-str/mu-str_guess_last_name", g_test_add_func ("/mu-str/mu-str_guess_last_name",
test_mu_str_guess_last_name); test_mu_str_guess_last_name);
g_test_add_func ("/mu-str/mu-str_guess_nick", g_test_add_func ("/mu-str/mu-str_guess_nick",
test_mu_str_guess_nick); test_mu_str_guess_nick);
/* FIXME: add tests for mu_str_flags; but note the /* FIXME: add tests for mu_str_flags; but note the
* function simply calls mu_msg_field_str */ * function simply calls mu_msg_field_str */
g_log_set_handler (NULL, g_log_set_handler (NULL,
G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION, G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION,
(GLogFunc)black_hole, NULL); (GLogFunc)black_hole, NULL);
return g_test_run (); return g_test_run ();
} }