From da003f5fd19531228596d694da89183be322cf3d Mon Sep 17 00:00:00 2001 From: Mikachu2333 Date: Wed, 1 Oct 2025 15:46:36 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20xy=5Fstr=5Ftake=5Funtil=5F?= =?UTF-8?q?newline?= 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 7d464a3..85e5747 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; +} + /**