From bb96b80067a78ef5b3125011184ae00dde35d901 Mon Sep 17 00:00:00 2001 From: Mikachu2333 Date: Wed, 1 Oct 2025 15:45:59 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20xy=5Fstr=5Ffind?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/xy.h | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/lib/xy.h b/lib/xy.h index 4d3eaad1..92118131 100644 --- a/lib/xy.h +++ b/lib/xy.h @@ -9,7 +9,7 @@ * | BingChunMoLi * | * Created On : <2023-08-28> - * Last Modified : <2025-08-27> + * Last Modified : <2025-09-29> * * * xy: 襄阳、咸阳 @@ -609,6 +609,42 @@ xy_str_strip (const char *str) return new; } +typedef struct +{ + bool found; + size_t begin; + size_t end; +} +XyStrFindResult_t; + +/** + * @brief 查找子串,返回是否命中以及子串在原串中的起止位置(0 基,end 为闭区间) + */ +static XyStrFindResult_t +xy_str_find (const char *str, const char *substr) +{ + XyStrFindResult_t result = { .found = false, .begin = 0, .end = 0 }; + + if (!str || !substr) + return result; + + if ('\0' == substr[0]) + { + result.found = true; + return result; + } + + const char *pos = strstr (str, substr); + if (!pos) + return result; + + result.found = true; + result.begin = (size_t) (pos - str); + result.end = result.begin + strlen (substr) - 1; + return result; +} + + /****************************************************** * Logging @@ -1564,4 +1600,5 @@ xy_map_each ( } } + #endif From 4d0aa33d3f00bf1b1b4e545ffedf145c2beb3f6f Mon Sep 17 00:00:00 2001 From: Mikachu2333 Date: Wed, 1 Oct 2025 15:46:36 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20xy=5Fstr=5Ftake=5Funti?= =?UTF-8?q?l=5Fnewline?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/xy.h | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/lib/xy.h b/lib/xy.h index 92118131..0619b5f2 100644 --- a/lib/xy.h +++ b/lib/xy.h @@ -644,6 +644,32 @@ xy_str_find (const char *str, const char *substr) return result; } +/** + * @brief 获取字符串下一行的内容 + * @note 将忽略开头的换行,截取至下一个换行前(不含换行符) + */ +static char * +xy_str_take_until_newline (const char *str) +{ + if (!str) + return xy_strdup (""); + + const char *cur = str; + while (*cur == '\n') + cur++; + + if ('\0' == *cur) + return xy_strdup (""); + + const char *newline = strchr (cur, '\n'); + size_t len = newline ? (size_t) (newline - cur) : strlen (cur); + + char *ret = xy_malloc0 (len + 1); + strncpy (ret, cur, len); + ret[len] = '\0'; + return ret; +} + /******************************************************