mirror of
https://github.com/labwc/labwc.git
synced 2026-03-11 05:33:49 -04:00
string-helpers: add str_endswith_ignore_case()
This commit is contained in:
parent
41aa7e1a7d
commit
10c6abb36a
2 changed files with 37 additions and 3 deletions
|
|
@ -73,6 +73,17 @@ char *str_join(const char *const parts[],
|
||||||
*/
|
*/
|
||||||
bool str_endswith(const char *const string, const char *const suffix);
|
bool str_endswith(const char *const string, const char *const suffix);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* str_endswith_ignore_case - indicate whether a string ends with a given suffix
|
||||||
|
* @string: string to test
|
||||||
|
* @suffix: suffix to expect in string
|
||||||
|
*
|
||||||
|
* If suffix is "" or NULL, this method always returns true; otherwise, this
|
||||||
|
* method returns true if and only if the full suffix exists at the end of the
|
||||||
|
* string.
|
||||||
|
*/
|
||||||
|
bool str_endswith_ignore_case(const char *const string, const char *const suffix);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* str_starts_with - indicate whether a string starts with a given character
|
* str_starts_with - indicate whether a string starts with a given character
|
||||||
* @string: string to test
|
* @string: string to test
|
||||||
|
|
|
||||||
|
|
@ -2,11 +2,18 @@
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <strings.h>
|
||||||
#include "common/mem.h"
|
#include "common/mem.h"
|
||||||
#include "common/string-helpers.h"
|
#include "common/string-helpers.h"
|
||||||
|
|
||||||
|
enum str_flags {
|
||||||
|
STR_FLAG_NONE = 0,
|
||||||
|
STR_FLAG_IGNORE_CASE,
|
||||||
|
};
|
||||||
|
|
||||||
bool
|
bool
|
||||||
string_null_or_empty(const char *s)
|
string_null_or_empty(const char *s)
|
||||||
{
|
{
|
||||||
|
|
@ -154,8 +161,8 @@ str_join(const char *const parts[],
|
||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
static bool
|
||||||
str_endswith(const char *const string, const char *const suffix)
|
_str_endswith(const char *const string, const char *const suffix, uint32_t flags)
|
||||||
{
|
{
|
||||||
size_t len_str = string ? strlen(string) : 0;
|
size_t len_str = string ? strlen(string) : 0;
|
||||||
size_t len_sfx = suffix ? strlen(suffix) : 0;
|
size_t len_sfx = suffix ? strlen(suffix) : 0;
|
||||||
|
|
@ -168,7 +175,23 @@ str_endswith(const char *const string, const char *const suffix)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return strcmp(string + len_str - len_sfx, suffix) == 0;
|
if (flags & STR_FLAG_IGNORE_CASE) {
|
||||||
|
return strcasecmp(string + len_str - len_sfx, suffix) == 0;
|
||||||
|
} else {
|
||||||
|
return strcmp(string + len_str - len_sfx, suffix) == 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
str_endswith(const char *const string, const char *const suffix)
|
||||||
|
{
|
||||||
|
return _str_endswith(string, suffix, STR_FLAG_NONE);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
str_endswith_ignore_case(const char *const string, const char *const suffix)
|
||||||
|
{
|
||||||
|
return _str_endswith(string, suffix, STR_FLAG_IGNORE_CASE);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue