summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCyrille Bagard <nocbos@gmail.com>2018-10-25 16:54:44 (GMT)
committerCyrille Bagard <nocbos@gmail.com>2018-10-25 16:54:44 (GMT)
commit413aa06d8f4037579cb6718e51bd7488a153f7e9 (patch)
tree5e4ef13849c8131bdef306ad688963e4181d3793
parent5acd4ab2dd059c81a333ed630686f6aa78f38a1e (diff)
Extended the facilities to search for prefixes or suffixes in strings.
-rw-r--r--src/common/extstr.c54
-rw-r--r--src/common/extstr.h8
2 files changed, 58 insertions, 4 deletions
diff --git a/src/common/extstr.c b/src/common/extstr.c
index 65826f2..4cb42a3 100644
--- a/src/common/extstr.c
+++ b/src/common/extstr.c
@@ -423,7 +423,47 @@ char *ellipsis(char *input, size_t max)
/******************************************************************************
* *
* Paramètres : str = chaîne à analyser. *
+* prefix = chaîne à retrouver en extrémité éventuellement. *
+* start = premier caractère hors préfixe. [OUT] *
+* *
+* Description : Détermine si une chaîne débute par une autre. *
+* *
+* Retour : true si le préfixe a été identifié, ou false sinon. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+bool _startswith(const char *str, const char *prefix, const char **start)
+{
+ bool result; /* Bilan à faire remonter */
+ size_t len; /* Taille de la chaîne soumise */
+ size_t preflen; /* Taille du préfixe */
+
+ result = false;
+
+ len = strlen(str);
+ preflen = strlen(prefix);
+
+ if (len > preflen)
+ {
+ result = (strncmp(str, prefix, preflen) == 0);
+
+ if (result && start != NULL)
+ *start = &str[preflen];
+
+ }
+
+ return result;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : str = chaîne à analyser. *
* suffix = chaîne à retrouver en extrémité éventuellement. *
+* end = premier caractère du préfixe. [OUT] *
* *
* Description : Détermine si une chaîne se termine par une autre. *
* *
@@ -433,19 +473,27 @@ char *ellipsis(char *input, size_t max)
* *
******************************************************************************/
-bool endswith(const char *str, const char *suffix)
+bool _endswith(const char *str, const char *suffix, const char **end)
{
bool result; /* Bilan à faire remonter */
size_t len; /* Taille de la chaîne soumise */
size_t suflen; /* Taille du suffixe */
+ const char *tmp; /* Stockage temporaire */
result = false;
len = strlen(str);
suflen = strlen(suffix);
- if (len > suflen && strncmp(&str[len - suflen], suffix, suflen) == 0)
- result = true;
+ if (len > suflen)
+ {
+ if (end == NULL) end = &tmp;
+
+ *end = &str[len - suflen];
+
+ result = (strncmp(*end, suffix, suflen) == 0);
+
+ }
return result;
diff --git a/src/common/extstr.h b/src/common/extstr.h
index 5af572b..0a89639 100644
--- a/src/common/extstr.h
+++ b/src/common/extstr.h
@@ -64,8 +64,14 @@ char *escape_crlf(char *);
/* Borne la taille d'une chaîne à une valeur donnée. */
char *ellipsis(char *, size_t);
+/* Détermine si une chaîne débute par une autre. */
+bool _startswith(const char *, const char *, const char **);
+
/* Détermine si une chaîne se termine par une autre. */
-bool endswith(const char *, const char *);
+bool _endswith(const char *, const char *, const char **);
+
+#define startswith(str, prefix) _startswith(str, prefix, NULL)
+#define endswith(str, suffix) _endswith(str, suffix, NULL)