Skip to content

Commit da003f5

Browse files
Mikachu2333ccmywish
authored andcommitted
增加 xy_str_take_until_newline
1 parent 027c40c commit da003f5

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

lib/xy.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,32 @@ xy_str_find (const char *str, const char *substr)
644644
return result;
645645
}
646646

647+
/**
648+
* @brief 获取字符串下一行的内容
649+
* @note 将忽略开头的换行,截取至下一个换行前(不含换行符)
650+
*/
651+
static char *
652+
xy_str_take_until_newline (const char *str)
653+
{
654+
if (!str)
655+
return xy_strdup ("");
656+
657+
const char *cur = str;
658+
while (*cur == '\n')
659+
cur++;
660+
661+
if ('\0' == *cur)
662+
return xy_strdup ("");
663+
664+
const char *newline = strchr (cur, '\n');
665+
size_t len = newline ? (size_t) (newline - cur) : strlen (cur);
666+
667+
char *ret = xy_malloc0 (len + 1);
668+
strncpy (ret, cur, len);
669+
ret[len] = '\0';
670+
return ret;
671+
}
672+
647673

648674

649675
/**

0 commit comments

Comments
 (0)