Skip to content

Commit 14569e4

Browse files
committed
feat(utilities): 添加utf8_contains函数以检查UTF-8字符串中是否包含子字符串
1 parent 95f1400 commit 14569e4

3 files changed

Lines changed: 54 additions & 1 deletion

File tree

hikyuu_cpp/hikyuu/utilities/arithmetic.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,4 +339,54 @@ bool HKU_UTILS_API utf8_fold_equal(const std::string &s1, const std::string &s2)
339339
}
340340
}
341341

342+
/* UTF-8字符串包含子字符串 */
343+
bool HKU_UTILS_API utf8_contains(const std::string &s, const std::string &sub) noexcept {
344+
if (sub.empty()) {
345+
return true;
346+
}
347+
if (s.empty()) {
348+
return false;
349+
}
350+
351+
const uint8_t *str = reinterpret_cast<const uint8_t *>(s.data());
352+
utf8proc_ssize_t len = s.size();
353+
const uint8_t *sub_str = reinterpret_cast<const uint8_t *>(sub.data());
354+
utf8proc_ssize_t sub_len = sub.size();
355+
356+
utf8proc_int32_t codepoint;
357+
utf8proc_ssize_t pos = 0;
358+
359+
while ((pos = utf8proc_iterate(str, len, &codepoint)) > 0) {
360+
const uint8_t *tmp_str = str;
361+
utf8proc_ssize_t tmp_len = len;
362+
const uint8_t *tmp_sub = sub_str;
363+
utf8proc_ssize_t tmp_sub_len = sub_len;
364+
365+
utf8proc_int32_t cp1, cp2;
366+
utf8proc_ssize_t p1, p2;
367+
368+
bool match = true;
369+
while ((p2 = utf8proc_iterate(tmp_sub, tmp_sub_len, &cp2)) > 0) {
370+
p1 = utf8proc_iterate(tmp_str, tmp_len, &cp1);
371+
if (p1 <= 0 || cp1 != cp2) {
372+
match = false;
373+
break;
374+
}
375+
tmp_str += p1;
376+
tmp_len -= p1;
377+
tmp_sub += p2;
378+
tmp_sub_len -= p2;
379+
}
380+
381+
if (match) {
382+
return true;
383+
}
384+
385+
str += pos;
386+
len -= pos;
387+
}
388+
389+
return false;
390+
}
391+
342392
} // namespace hku

hikyuu_cpp/hikyuu/utilities/arithmetic.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,9 @@ std::string HKU_UTILS_API utf8_to_upper(const std::string &s) noexcept;
227227
/** UTF-8字符串大小写折叠比较 */
228228
bool HKU_UTILS_API utf8_fold_equal(const std::string &s1, const std::string &s2) noexcept;
229229

230+
/** UTF-8字符串包含子字符串 */
231+
bool HKU_UTILS_API utf8_contains(const std::string &s, const std::string &sub) noexcept;
232+
230233
/** 删除字符串两端空格 */
231234
inline void trim(std::string &s) {
232235
if (s.empty()) {

xmake.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ add_requires("nng", {system = false, configs = {NNG_ENABLE_TLS = has_config("htt
194194
add_requires("nlohmann_json", {system = false})
195195
add_requires("eigen", {system = false})
196196
add_requires("xxhash", {system = false})
197-
add_requires("utf8proc 2.11.0", {system = false})
197+
add_requires("utf8proc", {system = false})
198198

199199
if is_plat("windows", "linux", "cross") then
200200
add_requires("mimalloc", {system = false, configs ={shared = true}})

0 commit comments

Comments
 (0)