Skip to content

Commit 027c40c

Browse files
Mikachu2333ccmywish
authored andcommitted
增加 xy_str_find
1 parent bb2d3af commit 027c40c

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

lib/xy.h

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* | BingChunMoLi <[email protected]>
1010
* |
1111
* Created On : <2023-08-28>
12-
* Last Modified : <2025-08-27>
12+
* Last Modified : <2025-09-29>
1313
*
1414
*
1515
* xy: 襄阳、咸阳
@@ -609,6 +609,42 @@ xy_str_strip (const char *str)
609609
return new;
610610
}
611611

612+
typedef struct
613+
{
614+
bool found;
615+
size_t begin;
616+
size_t end;
617+
}
618+
XyStrFindResult_t;
619+
620+
/**
621+
* @brief 查找子串,返回是否命中以及子串在原串中的起止位置(0 基,end 为闭区间)
622+
*/
623+
static XyStrFindResult_t
624+
xy_str_find (const char *str, const char *substr)
625+
{
626+
XyStrFindResult_t result = { .found = false, .begin = 0, .end = 0 };
627+
628+
if (!str || !substr)
629+
return result;
630+
631+
if ('\0' == substr[0])
632+
{
633+
result.found = true;
634+
return result;
635+
}
636+
637+
const char *pos = strstr (str, substr);
638+
if (!pos)
639+
return result;
640+
641+
result.found = true;
642+
result.begin = (size_t) (pos - str);
643+
result.end = result.begin + strlen (substr) - 1;
644+
return result;
645+
}
646+
647+
612648

613649
/**
614650
* @brief 读取文件内容并返回字符串,失败时返回空字符串
@@ -1617,4 +1653,5 @@ xy_map_each (
16171653
}
16181654
}
16191655

1656+
16201657
#endif

0 commit comments

Comments
 (0)