From 413aa06d8f4037579cb6718e51bd7488a153f7e9 Mon Sep 17 00:00:00 2001 From: Cyrille Bagard Date: Thu, 25 Oct 2018 18:54:44 +0200 Subject: Extended the facilities to search for prefixes or suffixes in strings. --- src/common/extstr.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++--- src/common/extstr.h | 8 +++++++- 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) -- cgit v0.11.2-87-g4458