From 6094a21e5e20c1a3a6a859a40219db59c6f77482 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Thu, 2 Sep 2010 07:19:59 +0000 Subject: [PATCH] added a few string functions --- qse/include/qse/cmn/str.h | 32 +++++++++++++++++-- qse/lib/cmn/str_bas.c | 65 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 94 insertions(+), 3 deletions(-) diff --git a/qse/include/qse/cmn/str.h b/qse/include/qse/cmn/str.h index c1b9900d..a70b16f1 100644 --- a/qse/include/qse/cmn/str.h +++ b/qse/include/qse/cmn/str.h @@ -1,5 +1,5 @@ /* - * $Id: str.h 341 2010-08-04 07:25:48Z hyunghwan.chung $ + * $Id: str.h 353 2010-09-01 13:19:59Z hyunghwan.chung $ * Copyright 2006-2009 Chung, Hyung-Hwan. This file is part of QSE. @@ -319,7 +319,16 @@ int qse_strxncmp ( qse_size_t len2 ); -int qse_strcasecmp (const qse_char_t* s1, const qse_char_t* s2); +int qse_strcasecmp ( + const qse_char_t* s1, + const qse_char_t* s2 +); + +int qse_strxcasecmp ( + const qse_char_t* s1, + qse_size_t len, + const qse_char_t* s2 +); /** * The qse_strxncasecmp() function compares characters at the same position @@ -461,6 +470,25 @@ qse_char_t* qse_strxrchr ( qse_cint_t c ); +/** + * The qse_strxword() function finds a whole word in a string. + */ +const qse_char_t* qse_strxword ( + const qse_char_t* str, + qse_size_t len, + const qse_char_t* word +); + +/** + * The qse_strxcaseword() function finds a whole word in a string + * case-insensitively. + */ +const qse_char_t* qse_strxcaseword ( + const qse_char_t* str, + qse_size_t len, + const qse_char_t* word +); + /** * The qse_strbeg() function checks if the a string begins with a substring. * @return the pointer to a beginning of a matching beginning, diff --git a/qse/lib/cmn/str_bas.c b/qse/lib/cmn/str_bas.c index b88f4545..9a2ead3b 100644 --- a/qse/lib/cmn/str_bas.c +++ b/qse/lib/cmn/str_bas.c @@ -1,5 +1,5 @@ /* - * $Id: str_bas.c 326 2010-05-09 13:44:39Z hyunghwan.chung $ + * $Id: str_bas.c 353 2010-09-01 13:19:59Z hyunghwan.chung $ * Copyright 2006-2009 Chung, Hyung-Hwan. This file is part of QSE. @@ -544,6 +544,22 @@ int qse_strcasecmp (const qse_char_t* s1, const qse_char_t* s2) return (QSE_TOUPPER(*s1) > QSE_TOUPPER(*s2))? 1: -1; } +int qse_strxcasecmp (const qse_char_t* s1, qse_size_t len, const qse_char_t* s2) +{ + qse_char_t c1, c2; + const qse_char_t* end = s1 + len; + + c1 = QSE_TOUPPER(*s1); c2 = QSE_TOUPPER(*s2); + while (s1 < end && c2 != QSE_T('\0') && c1 == c2) + { + s1++; s2++; + c1 = QSE_TOUPPER(*s1); c2 = QSE_TOUPPER(*s2); + } + if (s1 == end && c2 == QSE_T('\0')) return 0; + if (c1 == c2) return (s1 < end)? 1: -1; + return (c1 > c2)? 1: -1; +} + int qse_strxncasecmp ( const qse_char_t* s1, qse_size_t len1, const qse_char_t* s2, qse_size_t len2) @@ -836,6 +852,53 @@ qse_char_t* qse_strxrchr (const qse_char_t* str, qse_size_t len, qse_cint_t c) return QSE_NULL; } +const qse_char_t* qse_strxword ( + const qse_char_t* str, qse_size_t len, const qse_char_t* word) +{ + /* find a full word in a string */ + + const qse_char_t* ptr = str; + const qse_char_t* end = str + len; + const qse_char_t* s; + + do + { + while (ptr < end && QSE_ISSPACE(*ptr)) ptr++; + if (ptr >= end) return QSE_NULL; + + s = ptr; + while (ptr < end && !QSE_ISSPACE(*ptr)) ptr++; + + if (qse_strxcmp (s, ptr-s, word) == 0) return s; + } + while (ptr < end); + + return QSE_NULL; +} + +const qse_char_t* qse_strxcaseword ( + const qse_char_t* str, qse_size_t len, const qse_char_t* word) +{ + const qse_char_t* ptr = str; + const qse_char_t* end = str + len; + const qse_char_t* s; + + do + { + while (ptr < end && QSE_ISSPACE(*ptr)) ptr++; + if (ptr >= end) return QSE_NULL; + + s = ptr; + while (ptr < end && !QSE_ISSPACE(*ptr)) ptr++; + + if (qse_strxcasecmp (s, ptr-s, word) == 0) return s; + } + while (ptr < end); + + return QSE_NULL; +} + + qse_char_t* qse_strbeg (const qse_char_t* str, const qse_char_t* sub) { while (*sub != QSE_T('\0'))