MtFmt 1.0.0
MtFmt is a format library on embed. system and wrote by pure C.
载入中...
搜索中...
未找到
mm_string.hpp
浏览该文件的文档.
1// SPDX-License-Identifier: LGPL-3.0
12#if !defined(_INCLUDE_MM_STRING_HPP_)
13#define _INCLUDE_MM_STRING_HPP_ 1
14#include "mm_cfg.h"
15#include "mm_fmt.h"
16#include "mm_result.hpp"
17#include "mm_string.h"
18#include <array>
19#include <cstddef>
20#include <functional>
21#include <iterator>
22#include <limits>
23#include <string.h>
24#include <string>
25namespace mtfmt
26{
31class string;
32
38
39namespace details
40{
41#if _MSTR_USE_UTF_8
42template <std::size_t N>
43constexpr uint32_t utf8_meta(
44 const mstr_char_t (&u8char)[N], std::size_t idx, uint32_t mask
45)
46{
47 // 取得utf-8编码有效的部份: u8char[idx] & mask
48 return static_cast<uint32_t>(
49 static_cast<uint8_t>(u8char[idx]) & mask
50 );
51}
52
53template <std::size_t N>
54constexpr typename std::enable_if<N == 3, unicode_t>::type unicode_char(
55 const mstr_char_t (&u8char)[N]
56)
57{
58 return (utf8_meta(u8char, 1, 0x3f)) |
59 (utf8_meta(u8char, 0, 0x1f) << 6);
60}
61
62template <std::size_t N>
63constexpr typename std::enable_if<N == 4, unicode_t>::type unicode_char(
64 const mstr_char_t (&u8char)[N]
65)
66{
67 return (utf8_meta(u8char, 2, 0x3f)) |
68 (utf8_meta(u8char, 1, 0x3f) << 6) |
69 (utf8_meta(u8char, 0, 0x0f) << 12);
70}
71
72template <std::size_t N>
73constexpr typename std::enable_if<N == 5, unicode_t>::type unicode_char(
74 const mstr_char_t (&u8char)[N]
75)
76{
77 return (utf8_meta(u8char, 3, 0x3f)) |
78 (utf8_meta(u8char, 2, 0x3f) << 6) |
79 (utf8_meta(u8char, 1, 0x3f) << 12) |
80 (utf8_meta(u8char, 0, 0x07) << 18);
81}
82#endif // _MSTR_USE_UTF_8
83
88template <std::size_t N>
89constexpr typename std::enable_if<N <= 2, unicode_t>::type unicode_char(
90 const mstr_char_t (&u8char)[N]
91)
92{
93 return u8char[0];
94}
95
100class string_iterator
101{
106 const char* beg;
107
112 const char* it;
113
118 usize_t rem_length;
119
120public:
121 using value_type = unicode_t;
122 using pointer = const value_type*;
123 using reference = const value_type&;
124 using difference_type = iptr_t;
125 using iterator_category = std::bidirectional_iterator_tag;
126
127 string_iterator(
128 const mstr_char_t* _beg, const mstr_char_t* _buff, usize_t _leng
129 )
130 : beg(_beg), it(_buff), rem_length(_leng)
131 {
132 }
133
134 string_iterator(string_iterator&&) = default;
135 string_iterator(const string_iterator&) = default;
136 string_iterator& operator=(string_iterator&&) = default;
137 string_iterator& operator=(const string_iterator&) = default;
138
139 reference operator*() const
140 {
141 static mstr_codepoint_t code;
142#if _MSTR_USE_UTF_8
143 usize_t len = mstr_char_length(*it);
144 mstr_result_t res = mstr_codepoint_of(&code, it, len);
145 if (MSTR_FAILED(res)) {
146#if _MSTR_USE_CPP_EXCEPTION
147 throw mtfmt_error(MStr_Err_UnicodeEncodingError);
148#else
150#endif // _MSTR_USE_CPP_EXCEPTION
151 }
152#else
153 code = *it;
154#endif
155 // 因为code是计算出来的
156 // 因此用static吧...c++11似乎无法延续const reference&的lifetime
157 return code;
158 }
159
160 string_iterator& operator++()
161 {
162 if (rem_length > 0) {
163 usize_t len = mstr_char_length(*it);
164 it += len;
165 rem_length -= 1;
166 }
167 else {
168#if _MSTR_USE_CPP_EXCEPTION
169 throw mtfmt_error(MStr_Err_IteratorOutOfBound);
170#else
172#endif // _MSTR_USE_CPP_EXCEPTION
173 }
174 return *this;
175 }
176
177 string_iterator operator++(int)
178 {
179 string_iterator tmp = *this;
180 ++*this;
181 return tmp;
182 }
183
184 string_iterator& operator--()
185 {
186 if (it > beg) {
187 usize_t run = static_cast<usize_t>(it - beg);
188 // 因为迭代器是[begin, end)
189 // 因此先减掉1
190 usize_t len = mstr_lead_char_offset(it - 1, run);
191 it -= len;
192 rem_length += 1;
193 }
194 else {
195#if _MSTR_USE_CPP_EXCEPTION
196 throw mtfmt_error(MStr_Err_IteratorOutOfBound);
197#else
199#endif // _MSTR_USE_CPP_EXCEPTION
200 }
201 return *this;
202 }
203
204 string_iterator operator--(int)
205 {
206 string_iterator tmp = *this;
207 --*this;
208 return tmp;
209 }
210
211 bool operator==(const string_iterator& rhs) const noexcept
212 {
213 return it == rhs.it;
214 }
215
216 bool operator!=(const string_iterator& rhs) const noexcept
217 {
218 return it != rhs.it;
219 }
220};
221
226template <typename T> struct fixed_wrapper
227{
228 using value_t = T;
229
230 T value;
231};
232
237template <typename T> using wrapper_t = typename T::value_t;
238
239} // namespace details
240
245class string final
246{
252
253public:
255 using pointer = value_t*;
256 using const_pointer = const value_t*;
258 using const_reference = const value_t&;
259 using size_type = size_t;
260 using difference_type = ptrdiff_t;
261 using const_iterator = details::string_iterator;
263 std::reverse_iterator<const_iterator>;
264
265 using repeat_char_t = std::tuple<unicode_t, std::size_t>;
266
271 string() noexcept
272 {
274 }
275
282 {
284 }
285
291 string(const std::string& str)
292 {
293 mstr_create(&this_obj, str.c_str());
294 }
295
301 string(const string& str) noexcept
302 {
304 mstr_copy_from(&this_obj, &str.this_obj);
305 }
306
308 {
309 mstr_free(&this_obj);
310 }
311
315 string& operator=(const string& str) noexcept
316 {
317 mstr_clear(&this_obj);
318 mstr_concat(&this_obj, &str.this_obj);
319 return *this;
320 }
321
326 usize_t length() const noexcept
327 {
328 return this_obj.length;
329 }
330
335 usize_t byte_count() const noexcept
336 {
337 return this_obj.count;
338 }
339
344 bool operator==(const string& str) const noexcept
345 {
346 return !!mstr_equal(&this_obj, &str.this_obj);
347 }
348
353 template <std::size_t N>
354 bool operator==(const value_t (&str)[N]) const noexcept
355 {
356 return !!mstr_equal_cstr(&this_obj, str, N - 1);
357 }
358
363 template <std::size_t N>
364 friend bool operator==(
365 const value_t (&str)[N], const string& pthis
366 ) noexcept
367 {
368 return pthis == str;
369 }
370
375 bool operator!=(const string& str) const noexcept
376 {
377 return !(*this == str);
378 }
379
384 template <std::size_t N>
385 bool operator!=(const value_t (&str)[N]) const noexcept
386 {
387 return !(*this == str);
388 }
389
394 template <std::size_t N>
395 friend bool operator!=(
396 const value_t (&str)[N], const string& pthis
397 ) noexcept
398 {
399 return !(pthis == str);
400 }
401
408 {
409 mstr_result_t code = mstr_reserve(&this_obj, new_size);
410 if (MSTR_SUCC(code)) {
411 return unit_t();
412 }
413 else {
414 return code;
415 }
416 }
417
422 template <std::size_t N>
423 bool start_with(const value_t (&prefix)[N]) const noexcept
424 {
425 return !!mstr_start_with(&this_obj, prefix, N);
426 }
427
432 bool start_with(const value_t* prefix) const noexcept
433 {
434 return !!mstr_start_with(&this_obj, prefix, strlen(prefix));
435 }
436
441 bool start_with(const string* prefix) const noexcept
442 {
443 const char* buff = prefix->this_obj.buff;
444 usize_t buff_len = prefix->this_obj.count;
445 return !!mstr_start_with(&this_obj, buff, buff_len);
446 }
447
452 template <std::size_t N>
453 bool end_with(const value_t (&suffix)[N]) const noexcept
454 {
455 return !!mstr_end_with(&this_obj, suffix, N);
456 }
457
462 bool end_with(const value_t* suffix) const noexcept
463 {
464 return !!mstr_end_with(&this_obj, suffix, strlen(suffix));
465 }
466
471 bool end_with(const string* suffix) const noexcept
472 {
473 const char* buff = suffix->this_obj.buff;
474 usize_t buff_len = suffix->this_obj.count;
475 return !!mstr_end_with(&this_obj, buff, buff_len);
476 }
477
483 {
484 mstr_result_t code = mstr_append(&this_obj, uni_char);
485 if (MSTR_SUCC(code)) {
486 return unit_t();
487 }
488 else {
489 return code;
490 }
491 }
492
498 unicode_t ch, std::size_t repeat
499 ) noexcept
500 {
501 mstr_result_t code = mstr_repeat_append(&this_obj, ch, repeat);
502 if (MSTR_SUCC(code)) {
503 return unit_t();
504 }
505 else {
506 return code;
507 }
508 }
509
514 result<unit_t, mstr_result_t> concat(const string& rhs) noexcept
515 {
516 mstr_result_t code = mstr_concat(&this_obj, &rhs.this_obj);
517 if (MSTR_SUCC(code)) {
518 return unit_t();
519 }
520 else {
521 return code;
522 }
523 }
524
529 template <std::size_t N>
531 ) noexcept
532 {
534 if (MSTR_SUCC(code)) {
535 return unit_t();
536 }
537 else {
538 return code;
539 }
540 }
541
548 string& operator+=(const string& rhs)
549 {
550 concat(rhs).or_exception([](error_code_t e) {
551 return mtfmt_error(e);
552 });
553 return *this;
554 }
555
562 template <std::size_t N> string& operator+=(const value_t (&rhs)[N])
563 {
564 concat(rhs).or_exception([](error_code_t e) {
565 return mtfmt_error(e);
566 });
567 return *this;
568 }
569
577 {
578 push(rhs).or_exception([](error_code_t e) {
579 return mtfmt_error(e);
580 });
581 return *this;
582 }
583
590 string& operator+=(const repeat_char_t& rhs)
591 {
592 auto ch = std::get<0>(rhs);
593 auto cnt = std::get<1>(rhs);
594 push(ch, cnt).or_exception([](error_code_t e) {
595 return mtfmt_error(e);
596 });
597 return *this;
598 }
599
603 void clear() noexcept
604 {
605 mstr_clear(&this_obj);
606 }
607
612 void reverse() noexcept
613 {
614 mstr_reverse_self(&this_obj);
615 }
616
622 usize_t idx, unicode_t ch
623 ) noexcept
624 {
625 error_code_t res = mstr_insert(&this_obj, idx, ch);
626 if (MSTR_SUCC(res)) {
627 return unit_t{};
628 }
629 else {
630 return res;
631 }
632 }
633
639 {
640 unicode_t value = 0;
641 error_code_t res = mstr_remove(&this_obj, &value, idx);
642 if (MSTR_SUCC(res)) {
643 return value;
644 }
645 else {
646 return res;
647 }
648 }
649
657 bool contains(const value_t* patt) const noexcept
658 {
659 return mstr_contains(&this_obj, patt, strlen(patt));
660 }
661
669 template <std::size_t N>
670 bool contains(const value_t (&patt)[N]) const noexcept
671 {
672 return mstr_contains(&this_obj, patt, N);
673 }
674
682 bool contains(const string& patt) const noexcept
683 {
684 return mstr_contains(
685 &this_obj, patt.this_obj.buff, patt.this_obj.count
686 );
687 }
688
696 const value_t* patt, usize_t begin_pos = 0, usize_t patt_len = 0
697 ) const noexcept
698 {
699 mstr_result_t res;
700 MStringMatchResult find_result;
701 res = mstr_find(
702 &this_obj,
703 &find_result,
704 begin_pos,
705 patt,
706 patt_len == 0 ? strlen(patt) : patt_len
707 );
708 if (MSTR_FAILED(res)) {
709 return res;
710 }
711 else if (find_result.is_matched) {
712 return static_cast<isize_t>(find_result.begin_pos);
713 }
714 else {
715 return static_cast<isize_t>(-1);
716 }
717 }
718
725 template <std::size_t N>
727 const value_t (&patt)[N], usize_t begin_pos = 0
728 ) const noexcept
729 {
730 return find(patt, begin_pos, N);
731 }
732
740 const mtfmt::string& patt, usize_t begin_pos = 0
741 ) const noexcept
742 {
743 return find(patt.this_obj.buff, begin_pos, patt.this_obj.count);
744 }
745
752 const value_t* patt, usize_t begin_pos = 0, usize_t patt_cnt = 0
753 ) const noexcept
754 {
755 mstr_result_t res;
756 MStringMatchResult find_result;
757 res = mstr_find(
758 &this_obj,
759 &find_result,
760 begin_pos,
761 patt,
762 patt_cnt == 0 ? strlen(patt) : patt_cnt
763 );
764 if (MSTR_FAILED(res)) {
765 return res;
766 }
767 else if (find_result.is_matched) {
768 return static_cast<isize_t>(find_result.begin_pos);
769 }
770 else {
772 }
773 }
774
780 template <std::size_t N>
782 const value_t (&patt)[N], usize_t begin_pos = 0
783 ) const noexcept
784 {
785 return find_or_err(patt, begin_pos, N);
786 }
787
794 const mtfmt::string& patt, usize_t begin_pos = 0
795 ) const noexcept
796 {
797 return find_or_err(
798 patt.this_obj.buff, begin_pos, patt.this_obj.count
799 );
800 }
801
811 const value_t* patt,
813 usize_t patt_cnt = 0
814 ) noexcept
815 {
816 mstr_result_t res;
817 res = mstr_retain(
818 &this_obj,
819 mode,
820 patt,
821 patt_cnt == 0 ? strlen(patt) : patt_cnt
822 );
823 if (MSTR_SUCC(res)) {
824 return unit_t{};
825 }
826 else {
827 return res;
828 }
829 }
830
838 template <std::size_t N>
840 const value_t (&patt)[N],
842 ) noexcept
843 {
844 return retain(patt, mode, N);
845 }
846
855 const mtfmt::string& patt,
857 ) noexcept
858 {
859 return retain(patt.this_obj.buff, mode, patt.this_obj.count);
860 }
861
867 const value_t* c_str() noexcept
868 {
869 return mstr_c_str(&this_obj);
870 }
871
876 std::string as_std_string()
877 {
878 return std::string(this_obj.buff, this_obj.count);
879 }
880
885 const_iterator begin() const noexcept
886 {
887 return const_iterator(
889 );
890 }
891
896 const_iterator end() const noexcept
897 {
898 return const_iterator(
900 );
901 }
902
908 {
909 return const_reverse_iterator(end());
910 }
911
917 {
919 }
920
927 unicode_t operator[](std::size_t i) const
928 {
929 unicode_t code = mstr_char_at(&this_obj, i);
930 if (code == 0) {
931#if _MSTR_USE_CPP_EXCEPTION
933#else
935#endif // _MSTR_USE_CPP_EXCEPTION
936 }
937 return code;
938 }
939
947 template <typename T>
949 std::is_signed<T>::value &&
950 std::numeric_limits<T>::is_integer &&
951 sizeof(T) <= sizeof(int32_t),
954 const T& value,
957 ) noexcept
958 {
960 &this_obj, static_cast<int32_t>(value), index, sign
961 );
962 if (MSTR_SUCC(res)) {
963 return unit_t{};
964 }
965 else {
966 return res;
967 }
968 }
969
976 template <typename T>
978 std::is_unsigned<T>::value &&
979 std::numeric_limits<T>::is_integer &&
980 sizeof(T) <= sizeof(uint32_t),
981 result<unit_t, error_code_t>>
983 const T& value, MStrFmtIntIndex index = MStrFmtIntIndex_Dec
984 ) noexcept
985 {
987 &this_obj, static_cast<uint32_t>(value), index
988 );
989 if (MSTR_SUCC(res)) {
990 return unit_t{};
991 }
992 else {
993 return res;
994 }
995 }
996
1007 template <typename T>
1009 std::is_signed<details::wrapper_t<T>>::value &&
1011 result<unit_t, error_code_t>>
1013 const T& value,
1014 int32_t p,
1016 ) noexcept
1017 {
1019 &this_obj, static_cast<int32_t>(value.value), p, sign
1020 );
1021 if (MSTR_SUCC(res)) {
1022 return unit_t{};
1023 }
1024 else {
1025 return res;
1026 }
1027 }
1028
1038 template <typename T>
1040 std::is_unsigned<details::wrapper_t<T>>::value &&
1043 append_from(const T& value, uint32_t p) noexcept
1044 {
1046 &this_obj, static_cast<uint32_t>(value.value), p
1047 );
1048 if (MSTR_SUCC(res)) {
1049 return unit_t{};
1050 }
1051 else {
1052 return res;
1053 }
1054 }
1055
1063 template <typename T>
1064 static details::enable_if_t<
1065 std::is_signed<T>::value &&
1066 std::numeric_limits<T>::is_integer &&
1067 sizeof(T) <= sizeof(int32_t),
1069 from(
1070 const T& value,
1073 ) noexcept
1074 {
1075 string ret_str;
1077 &ret_str.this_obj, static_cast<int32_t>(value), index, sign
1078 );
1079 if (MSTR_SUCC(res)) {
1080 return ret_str;
1081 }
1082 else {
1083 return res;
1084 }
1085 }
1086
1093 template <typename T>
1094 static details::enable_if_t<
1095 std::is_unsigned<T>::value &&
1096 std::numeric_limits<T>::is_integer &&
1097 sizeof(T) <= sizeof(uint32_t),
1098 result<string, error_code_t>>
1099 from(
1100 const T& value, MStrFmtIntIndex index = MStrFmtIntIndex_Dec
1101 ) noexcept
1102 {
1103 string ret_str;
1105 &ret_str.this_obj, static_cast<uint32_t>(value), index
1106 );
1107 if (MSTR_SUCC(res)) {
1108 return ret_str;
1109 }
1110 else {
1111 return res;
1112 }
1113 }
1114
1125 template <typename T>
1126 static details::enable_if_t<
1127 std::is_signed<details::wrapper_t<T>>::value &&
1129 result<string, error_code_t>>
1131 const T& value,
1132 int32_t p,
1134 ) noexcept
1135 {
1136 string ret_str;
1138 &ret_str.this_obj,
1139 static_cast<int32_t>(value.value),
1140 p,
1141 sign
1142 );
1143 if (MSTR_SUCC(res)) {
1144 return ret_str;
1145 }
1146 else {
1147 return res;
1148 }
1149 }
1150
1160 template <typename T>
1161 static details::enable_if_t<
1162 std::is_unsigned<details::wrapper_t<T>>::value &&
1165 from(const T& value, uint32_t p) noexcept
1166 {
1167 string ret_str;
1169 &ret_str.this_obj, static_cast<uint32_t>(value.value), p
1170 );
1171 if (MSTR_SUCC(res)) {
1172 return ret_str;
1173 }
1174 else {
1175 return res;
1176 }
1177 }
1178
1185 template <std::size_t N>
1186 constexpr static unicode_t unicode_char(const value_t (&u8char)[N])
1187 {
1188#if _MSTR_USE_UTF_8
1190#else
1191 return static_cast<unicode_t>(u8char[0]);
1192#endif // _MSTR_USE_UTF_8
1193 }
1194
1205 template <std::size_t N, typename... Args>
1207 const value_t (&fmt_str)[N], Args&&... args
1208 )
1209 {
1210 return format_variable(fmt_str, std::forward<Args&&>(args)...);
1211 }
1212
1223 template <typename... Args>
1225 const string::value_t* fmt_str, Args&&... args
1226 )
1227 {
1228 string str;
1230 &str.raw_object_mut(),
1231 fmt_str,
1232 sizeof...(args),
1233 std::forward<Args&&>(args)...
1234 );
1235 if (MSTR_SUCC(code)) {
1236 return str;
1237 }
1238 else {
1239 return code;
1240 }
1241 }
1242
1243protected:
1249 {
1250 return this_obj;
1251 }
1252
1258 {
1259 return this_obj;
1260 }
1261};
1262
1270template <typename T>
1271details::fixed_wrapper<T> fixed_value(const T& value)
1272{
1273 return details::fixed_wrapper<T>{value};
1274}
1275
1276namespace literals
1277{
1282inline string operator""_ms(const char* str)
1283{
1284 return string(str);
1285}
1286} // namespace literals
1287
1288} // namespace mtfmt
1289#endif // _INCLUDE_MM_STRING_HPP_
表示存在错误
Definition mm_result.hpp:400
结果类
Definition mm_result.hpp:415
字符串类
Definition mm_string.hpp:246
bool contains(const string &patt) const noexcept
判断字符串是否包括字符串B (string对象)
Definition mm_string.hpp:682
const value_t * const_pointer
Definition mm_string.hpp:256
bool start_with(const value_t *prefix) const noexcept
判断字符串是否以另一个字串开始(c_str)
Definition mm_string.hpp:432
static result< string, error_code_t > format(const value_t(&fmt_str)[N], Args &&... args)
进行格式化(字符串数组)
Definition mm_string.hpp:1206
MString & raw_object_mut() noexcept
返回raw object(可变的)
Definition mm_string.hpp:1257
static constexpr unicode_t unicode_char(const value_t(&u8char)[N])
取得Unicode代码点的字符
Definition mm_string.hpp:1186
bool start_with(const string *prefix) const noexcept
判断字符串是否以另一个字串开始(MString)
Definition mm_string.hpp:441
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition mm_string.hpp:263
result< usize_t, error_code_t > find_or_err(const mtfmt::string &patt, usize_t begin_pos=0) const noexcept
查找字符串 (mtfmt::string)
Definition mm_string.hpp:793
string & operator+=(unicode_t rhs)
字符串拼接 (字符)
Definition mm_string.hpp:576
friend bool operator==(const value_t(&str)[N], const string &pthis) noexcept
相等(cstr left)
Definition mm_string.hpp:364
bool end_with(const value_t *suffix) const noexcept
判断字符串是否以另一个字串结束(c_str)
Definition mm_string.hpp:462
const value_t * c_str() noexcept
取得C风格字符串
Definition mm_string.hpp:867
result< unit_t, mstr_result_t > concat(const string &rhs) noexcept
放入一个字符串
Definition mm_string.hpp:514
const MString & raw_object() const noexcept
返回raw object
Definition mm_string.hpp:1248
string & operator+=(const value_t(&rhs)[N])
字符串拼接 (self, c_str)
Definition mm_string.hpp:562
value_t & reference
Definition mm_string.hpp:257
const_iterator begin() const noexcept
取得迭代器起始 (const)
Definition mm_string.hpp:885
result< unit_t, mstr_result_t > push(unicode_t uni_char) noexcept
放入一个字符
Definition mm_string.hpp:482
friend bool operator!=(const value_t(&str)[N], const string &pthis) noexcept
不等(cstr left)
Definition mm_string.hpp:395
result< unit_t, mstr_result_t > reserve(usize_t new_size) noexcept
保留足够的内存
Definition mm_string.hpp:407
bool end_with(const value_t(&suffix)[N]) const noexcept
判断字符串是否以另一个字串结束(c_str buffer)
Definition mm_string.hpp:453
string & operator=(const string &str) noexcept
copy
Definition mm_string.hpp:315
unicode_t operator[](std::size_t i) const
取得第n个unicode字符
Definition mm_string.hpp:927
details::string_iterator const_iterator
Definition mm_string.hpp:261
std::string as_std_string()
取得std::string
Definition mm_string.hpp:876
ptrdiff_t difference_type
Definition mm_string.hpp:260
void clear() noexcept
取得C风格字符串
Definition mm_string.hpp:603
string & operator+=(const string &rhs)
字符串拼接 (self)
Definition mm_string.hpp:548
string(const value_t *c_str)
从C字符串创建
Definition mm_string.hpp:281
string(const string &str) noexcept
从self创建
Definition mm_string.hpp:301
details::enable_if_t< std::is_unsigned< details::wrapper_t< T > >::value &&details::is_instance_of< details::fixed_wrapper, T >::value, result< unit_t, error_code_t > > append_from(const T &value, uint32_t p) noexcept
对无符号量化值进行格式化, 并填充到this中
Definition mm_string.hpp:1043
result< unit_t, mstr_result_t > concat(const value_t(&rhs)[N]) noexcept
放入一个字符串 (c_str)
Definition mm_string.hpp:530
result< unit_t, error_code_t > insert(usize_t idx, unicode_t ch) noexcept
在idx位置插入字符
Definition mm_string.hpp:621
bool operator==(const value_t(&str)[N]) const noexcept
相等(cstr)
Definition mm_string.hpp:354
result< usize_t, error_code_t > find_or_err(const value_t(&patt)[N], usize_t begin_pos=0) const noexcept
查找字符串 (c_str array)
Definition mm_string.hpp:781
result< unit_t, error_code_t > retain(const mtfmt::string &patt, MStringReplaceOption mode=MStringReplaceOption_All) noexcept
剔除掉patt字符(string object)
Definition mm_string.hpp:854
result< unicode_t, error_code_t > remove(usize_t idx) noexcept
移除idx位置的字符, 并返回被移除的字符
Definition mm_string.hpp:638
result< unit_t, error_code_t > retain(const value_t(&patt)[N], MStringReplaceOption mode=MStringReplaceOption_All) noexcept
剔除掉patt字符(c str array)
Definition mm_string.hpp:839
value_t * pointer
Definition mm_string.hpp:255
details::enable_if_t< std::is_signed< T >::value &&std::numeric_limits< T >::is_integer &&sizeof(T)<=sizeof(int32_t), result< unit_t, error_code_t > > append_from(const T &value, MStrFmtIntIndex index=MStrFmtIntIndex_Dec, MStrFmtSignDisplay sign=MStrFmtSignDisplay_NegOnly) noexcept { error_code_t res=mstr_fmt_itoa(&this_obj, static_cast< int32_t >(value), index, sign);if(MSTR_SUCC(res)) { return unit_t{};} else { return res;} } template< typename T > details::enable_if_t< std::is_unsigned< T >::value &&std::numeric_limits< T >::is_integer &&sizeof(T)<=sizeof(uint32_t), result< unit_t, error_code_t > > append_from(const T &value, MStrFmtIntIndex index=MStrFmtIntIndex_Dec) noexcept { error_code_t res=mstr_fmt_utoa(&this_obj, static_cast< uint32_t >(value), index);if(MSTR_SUCC(res)) { return unit_t{};} else { return res;} } template< typename T > details::enable_if_t< std::is_signed< details::wrapper_t< T > >::value &&details::is_instance_of< details::fixed_wrapper, T >::value, result< unit_t, error_code_t > > append_from(const T &value, int32_t p, MStrFmtSignDisplay sign=MStrFmtSignDisplay_NegOnly) noexcept
对有符号整数值进行格式化, 并填充到this中
Definition mm_string.hpp:1012
void reverse() noexcept
翻转字符串
Definition mm_string.hpp:612
string() noexcept
创建空的字符串
Definition mm_string.hpp:271
~string()
Definition mm_string.hpp:307
const_reverse_iterator rend() const noexcept
取得反向迭代器结束 (const)
Definition mm_string.hpp:916
result< unit_t, error_code_t > retain(const value_t *patt, MStringReplaceOption mode=MStringReplaceOption_All, usize_t patt_cnt=0) noexcept
剔除掉patt字符
Definition mm_string.hpp:810
size_t size_type
Definition mm_string.hpp:259
usize_t length() const noexcept
取得字符串长度
Definition mm_string.hpp:326
bool start_with(const value_t(&prefix)[N]) const noexcept
判断字符串是否以另一个字串开始(c_str buffer)
Definition mm_string.hpp:423
mstr_char_t value_t
Definition mm_string.hpp:254
static result< string, error_code_t > format_variable(const string::value_t *fmt_str, Args &&... args)
进行格式化(动态的格式化串)
Definition mm_string.hpp:1224
static details::enable_if_t< std::is_unsigned< details::wrapper_t< T > >::value &&details::is_instance_of< details::fixed_wrapper, T >::value, result< string, error_code_t > > from(const T &value, uint32_t p) noexcept
对无符号量化值进行格式化并返回
Definition mm_string.hpp:1165
bool end_with(const string *suffix) const noexcept
判断字符串是否以另一个字串结束(MString)
Definition mm_string.hpp:471
bool operator!=(const value_t(&str)[N]) const noexcept
不等(cstr)
Definition mm_string.hpp:385
result< isize_t, error_code_t > find(const value_t *patt, usize_t begin_pos=0, usize_t patt_len=0) const noexcept
查找字符串 (c_str)
Definition mm_string.hpp:695
string & operator+=(const repeat_char_t &rhs)
字符串拼接 (重复n个)
Definition mm_string.hpp:590
bool operator!=(const string &str) const noexcept
不等
Definition mm_string.hpp:375
const_reverse_iterator rbegin() const noexcept
取得反向迭代器起始 (const)
Definition mm_string.hpp:907
result< usize_t, error_code_t > find_or_err(const value_t *patt, usize_t begin_pos=0, usize_t patt_cnt=0) const noexcept
查找字符串 (c_str)
Definition mm_string.hpp:751
bool operator==(const string &str) const noexcept
相等
Definition mm_string.hpp:344
const value_t & const_reference
Definition mm_string.hpp:258
std::tuple< unicode_t, std::size_t > repeat_char_t
Definition mm_string.hpp:265
usize_t byte_count() const noexcept
取得字符串占用的字节数
Definition mm_string.hpp:335
MString this_obj
c对象
Definition mm_string.hpp:251
result< unit_t, mstr_result_t > push(unicode_t ch, std::size_t repeat) noexcept
重复放入一个字符
Definition mm_string.hpp:497
result< isize_t, error_code_t > find(const mtfmt::string &patt, usize_t begin_pos=0) const noexcept
查找字符串 (mtfmt::string)
Definition mm_string.hpp:739
const_iterator end() const noexcept
取得迭代器结束 (const)
Definition mm_string.hpp:896
string(const std::string &str)
从std::string创建
Definition mm_string.hpp:291
bool contains(const value_t(&patt)[N]) const noexcept
判断字符串是否包括字符串B (cstr数组)
Definition mm_string.hpp:670
bool contains(const value_t *patt) const noexcept
判断字符串是否包括字符串B
Definition mm_string.hpp:657
static static details::enable_if_t< std::is_signed< T >::value &&std::numeric_limits< T >::is_integer &&sizeof(T)<=sizeof(int32_t), result< string, error_code_t > > from(const T &value, MStrFmtIntIndex index=MStrFmtIntIndex_Dec, MStrFmtSignDisplay sign=MStrFmtSignDisplay_NegOnly) noexcept { string ret_str;error_code_t res=mstr_fmt_itoa(&ret_str.this_obj, static_cast< int32_t >(value), index, sign);if(MSTR_SUCC(res)) { return ret_str;} else { return res;} } template< typename T > static details::enable_if_t< std::is_unsigned< T >::value &&std::numeric_limits< T >::is_integer &&sizeof(T)<=sizeof(uint32_t), result< string, error_code_t > > from(const T &value, MStrFmtIntIndex index=MStrFmtIntIndex_Dec) noexcept { string ret_str;error_code_t res=mstr_fmt_utoa(&ret_str.this_obj, static_cast< uint32_t >(value), index);if(MSTR_SUCC(res)) { return ret_str;} else { return res;} } template< typename T > details::enable_if_t< std::is_signed< details::wrapper_t< T > >::value &&details::is_instance_of< details::fixed_wrapper, T >::value, result< string, error_code_t > > from(const T &value, int32_t p, MStrFmtSignDisplay sign=MStrFmtSignDisplay_NegOnly) noexcept
对有符号整数值进行格式化并返回
Definition mm_string.hpp:1130
result< isize_t, error_code_t > find(const value_t(&patt)[N], usize_t begin_pos=0) const noexcept
查找字符串 (c_str array)
Definition mm_string.hpp:726
配置选项
#define mstr_cause_exception(code)
Definition mm_cfg.h:395
mstr_format(MString *res_str, const char *fmt, usize_t fmt_place,...)
格式化字符串
Definition mm_fmt.c:66
字符串格式化
@ MStrFmtIntIndex_Dec
转换为十进制字符串
Definition mm_fmt.h:54
enum tagMStrFmtIntIndex MStrFmtIntIndex
转换整数时采用的进制
mstr_fmt_itoa(MString *str, int32_t value, MStrFmtIntIndex index, MStrFmtSignDisplay sign)
将有符号整数转换为字符串
Definition mm_into.c:34
mstr_fmt_uqtoa(MString *res_str, uint32_t value, uint32_t quat)
将无符号量化值转换为字符串
Definition mm_into.c:115
mstr_fmt_iqtoa(MString *res_str, int32_t value, uint32_t quat, MStrFmtSignDisplay sign)
将有符号量化值转换为字符串
Definition mm_into.c:96
mstr_fmt_utoa(MString *res_str, uint32_t value, MStrFmtIntIndex index)
将无符号整数转换为字符串
Definition mm_into.c:51
@ MStrFmtSignDisplay_NegOnly
仅在值小于0时显示符号
Definition mm_parser.h:157
enum tagMStrFmtSignDisplay MStrFmtSignDisplay
格式化值时候的符号显示方式
mstr_find(const MString *str, MStringMatchResult *f_res, usize_t begin_pos, const char *pattern, usize_t pattern_cnt)
查找子串第一次出现的位置
Definition mm_pattern.c:60
mstr_retain(MString *str, MStringReplaceOption opt, const char *patt, usize_t patt_cnt)
从字符串中移除所有匹配substr的字符
Definition mm_pattern.c:114
enum tagMStrResult mstr_result_t
结果类型
#define MSTR_SUCC(s)
Definition mm_result.h:83
@ MStr_Err_UnicodeEncodingError
Definition mm_result.h:27
@ MStr_Err_IteratorOutOfBound
Definition mm_result.h:35
@ MStr_Err_IndexOutOfBound
Definition mm_result.h:31
@ MStr_Err_NoSubstrFound
Definition mm_result.h:33
#define MSTR_FAILED(s)
Definition mm_result.h:86
monadic result
mstr_codepoint_of(mstr_codepoint_t *code, const mstr_char_t *ch, usize_t byte_count)
取得lead字符ch[0]所跟着的内容的unicode代码点值 如果未启用UTF-8, 该函数返回未实现错误
Definition mm_string.c:644
mstr_start_with(const MString *str, const char *prefix, usize_t prefix_cnt)
判断字符串是否以某个字串开始
Definition mm_string.c:313
mstr_contains(const MString *str, const char *pattern, usize_t pattern_cnt)
判断字符串是否含有pattern
Definition mm_string.c:342
mstr_append(MString *str, mstr_codepoint_t ch)
向字符串尾部插入一个字符
Definition mm_string.c:144
mstr_char_at(const MString *str, usize_t idx)
取得第idx位置的字符ch
Definition mm_string.c:358
mstr_end_with(const MString *str, const char *suffix, usize_t suffix_cnt)
判断字符串是否以某个字串结束
Definition mm_string.c:327
mstr_equal(const MString *a, const MString *b)
判断两个字符串是否相等
Definition mm_string.c:296
mstr_insert(MString *str, usize_t idx, mstr_codepoint_t ch)
从字符串中idx位置插入一个字符
Definition mm_string.c:418
mstr_concat(MString *str, const MString *other)
拼接字符串
Definition mm_string.c:195
mstr_repeat_append(MString *str, mstr_codepoint_t ch, usize_t cnt)
向字符串尾部重复插入一个字符
Definition mm_string.c:150
mstr_lead_char_offset(const mstr_char_t *buff, usize_t hist_len)
判断buff的lead字符偏移量(相反数, 比如偏移量是-2但是会返回2)
Definition mm_string.c:493
mstr_reserve(MString *str, usize_t new_size)
保留 sz 个char数的内存区
Definition mm_string.c:121
mstr_copy_from(MString *str, const MString *other)
从other复制字符串到str
Definition mm_string.c:110
mstr_equal_cstr(const MString *a, const mstr_char_t *b, usize_t b_cnt)
判断两个字符串是否相等(cstr)
Definition mm_string.c:302
mstr_create(MString *str, const char *content)
创建字符串
Definition mm_string.c:50
mstr_remove(MString *str, mstr_codepoint_t *removed_ch, usize_t idx)
从字符串中移除idx位置的字符
Definition mm_string.c:382
mstr_concat_cstr(MString *str, const char *other)
拼接字符串(cstr)
Definition mm_string.c:220
字符串
@ MStringReplaceOption_All
全部替换
Definition mm_string.h:46
const MString * str
Definition mm_string.h:440
#define mstr_init(pstr)
初始化一个空的字符串
Definition mm_string.h:137
enum tagMStringReplaceOption MStringReplaceOption
字符串替换选项
intptr_t isize_t
尺寸(有符号, sizeof(isize_t) == sizeof(usize_t) == sizeof(iptr_t))
Definition mm_type.h:39
uint32_t mstr_codepoint_t
unicode代码点
Definition mm_type.h:74
char mstr_char_t
字符
Definition mm_type.h:68
intptr_t iptr_t
Definition mm_type.h:39
size_t usize_t
尺寸(无符号)
Definition mm_type.h:32
constexpr uint32_t utf8_meta(const mstr_char_t(&u8char)[N], std::size_t idx, uint32_t mask)
Definition mm_string.hpp:43
constexpr std::enable_if< N==3, unicode_t >::type unicode_char(const mstr_char_t(&u8char)[N])
Definition mm_string.hpp:54
typename std::enable_if< cond, T >::type enable_if_t
enable_if_t, 和cpp14一样(但是这里是cpp11呜呜呜)
Definition mm_type.hpp:205
Definition mm_parser.hpp:17
details::fixed_wrapper< T > fixed_value(const T &value)
标记该值是一个定点数
Definition mm_string.hpp:1271
mstr_codepoint_t unicode_t
unicode字符
Definition mm_string.hpp:37
details::unit_t unit_t
表示单位类型
Definition mm_result.hpp:388
mstr_result_t error_code_t
返回值的错误结果
Definition mm_result.hpp:394
判断Ti是否为模板Tt的实例化
Definition mm_type.hpp:150
static constexpr bool value
Definition mm_type.hpp:151
单位类型
Definition mm_type.hpp:29
字符串匹配信息
Definition mm_string.h:54
usize_t begin_pos
目标位置的字符索引
Definition mm_string.h:65
usize_t is_matched
是否找到了目标
Definition mm_string.h:59
字符串
Definition mm_string.h:79
usize_t length
字符串长度
Definition mm_string.h:98
usize_t count
字符串的字节长度
Definition mm_string.h:92
char * buff
Definition mm_string.h:80