MCPcopy Create free account
hub / github.com/CppCXY/EmmyLuaCodeStyle / format

Function format

Util/include/Util/format.h:70–175  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

68 */
69template<class... ARGS>
70std::string format(std::string_view fmt, ARGS... args) {
71 std::vector<std::string> argVec = {toFormatString<ARGS>(args)...};
72
73 enum class ParseState {
74 Normal,
75 LeftBrace,
76 RightBrace
77 } state = ParseState::Normal;
78
79 std::vector<FormatReplaceExpress> replaceExpresses;
80
81
82 std::size_t leftBraceBegin = 0;
83
84 std::size_t rightBraceBegin = 0;
85
86 // 如果在表达式中出现左大括号
87 std::size_t exprLeftCount = 0;
88
89
90 for (std::size_t index = 0; index != fmt.size(); index++) {
91 char ch = fmt[index];
92
93 switch (state) {
94 case ParseState::Normal: {
95 if (ch == '{') {
96 state = ParseState::LeftBrace;
97 leftBraceBegin = index;
98 exprLeftCount = 0;
99 } else if (ch == '}') {
100 state = ParseState::RightBrace;
101 rightBraceBegin = index;
102 }
103 break;
104 }
105 case ParseState::LeftBrace: {
106 if (ch == '{') {
107 // 认为是左双大括号转义为可见的'{'
108 if (index == leftBraceBegin + 1) {
109 replaceExpresses.emplace_back("{", leftBraceBegin, index, false);
110 state = ParseState::Normal;
111 } else {
112 exprLeftCount++;
113 }
114 } else if (ch == '}') {
115 // 认为是表达式内的大括号
116 if (exprLeftCount > 0) {
117 exprLeftCount--;
118 continue;
119 }
120
121 replaceExpresses.emplace_back(fmt.substr(leftBraceBegin + 1, index - leftBraceBegin - 1),
122 leftBraceBegin, index, true);
123
124
125 state = ParseState::Normal;
126 }
127 break;

Callers 8

ParseMethod · 0.50
PrintUsageMethod · 0.50
GetDebugViewMethod · 0.50
LocalAttributeMethod · 0.50
CheckMethod · 0.50
CheckAndNextMethod · 0.50
MakeErrorsFunction · 0.50
TESTFunction · 0.50

Calls 3

strMethod · 0.80
sizeMethod · 0.45
emptyMethod · 0.45

Tested by 2

MakeErrorsFunction · 0.40
TESTFunction · 0.40