summaryrefslogtreecommitdiffstats
path: root/source/helper.c
diff options
context:
space:
mode:
authorMoritz Maxeiner <moritz@ucworks.org>2017-01-26 19:46:46 +0100
committerDave Davenport <DaveDavenport@users.noreply.github.com>2017-01-26 19:46:46 +0100
commit6b9dc1d0819fbde20b35c8ab20592f57a445611c (patch)
tree7dbcbcfb331dbe4d0c43f8cfdbc5365f977b2156 /source/helper.c
parent9941efa5f411348585a63efdacb1be382ab065ee (diff)
Combi mode: Bang mode prefixes with len>1 (#542)
* Combi mode: Bang mode prefixes with len>1 This is required to match on modes that share a prefix. Let 'power' and 'pass' be such modes for the following explanation: Previously, only the first character of after the bang was compared, so '!p' would always be matched to the 'pass' mode and there was no way to limit selection in combi mode to the 'power' mode. Now we can use prefixes of arbitrary length following the bang such as '!po' (matches 'power' mode), or '!pa' (matches 'pass' mode). Prefixes of length 1 are unchanged compared to the previous behaviour, i.e. '!p' will still match 'pass'. * Combi-mode prefixes should be utf-8 aware
Diffstat (limited to 'source/helper.c')
-rw-r--r--source/helper.c23
1 files changed, 23 insertions, 0 deletions
diff --git a/source/helper.c b/source/helper.c
index ef4a8a49..6e3fc67a 100644
--- a/source/helper.c
+++ b/source/helper.c
@@ -889,3 +889,26 @@ int rofi_scorer_fuzzy_evaluate ( const char *pattern, glong plen, const char *st
g_free ( dp );
return -lefts;
}
+
+/**
+ * @param a UTF-8 string to compare
+ * @param b UTF-8 string to compare
+ * @param n Maximum number of characters to compare
+ *
+ * Compares the `G_NORMALIZE_ALL_COMPOSE` forms of the two strings.
+ *
+ * @returns less than, equal to, or greater than zero if the first `n` characters (not bytes) of `a`
+ * are found, respectively, to be less than, to match, or be greater than the first `n`
+ * characters (not bytes) of `b`.
+ */
+int utf8_strncmp ( const char* a, const char* b, size_t n )
+{
+ char *na = g_utf8_normalize ( a, -1, G_NORMALIZE_ALL_COMPOSE );
+ char *nb = g_utf8_normalize ( b, -1, G_NORMALIZE_ALL_COMPOSE );
+ *g_utf8_offset_to_pointer ( na, n ) = '\0';
+ *g_utf8_offset_to_pointer ( nb, n ) = '\0';
+ int r = g_utf8_collate ( na, nb );
+ g_free ( na );
+ g_free ( nb );
+ return r;
+}