|
40 | 40 | %% Macros |
41 | 41 | %%==================================================================== |
42 | 42 |
|
43 | | --define(ESCAPE_LVALUE(Value), |
44 | | - sub(sub(sub(Value, "\\", "\\\\\\\\"), "\n", "\\\\n"), "\"", "\\\\\"")). |
45 | | - |
46 | 43 | %%==================================================================== |
47 | 44 | %% Format API |
48 | 45 | %%==================================================================== |
@@ -156,33 +153,54 @@ labels_string(Labels) -> |
156 | 153 | emit_series(Fd, Name, Labels, undefined) -> |
157 | 154 | LString = labels_string(Labels), |
158 | 155 | file:write(Fd, [Name, LString, " NaN\n"]); |
| 156 | +emit_series(Fd, Name, Labels, Value) when is_integer(Value) -> |
| 157 | + LString = labels_string(Labels), |
| 158 | + file:write(Fd, [Name, LString, " ", integer_to_list(Value) , "\n"]); |
159 | 159 | emit_series(Fd, Name, Labels, Value) -> |
160 | 160 | LString = labels_string(Labels), |
161 | 161 | file:write(Fd, [Name, LString, " ", io_lib:format("~p", [Value]) , "\n"]). |
162 | 162 |
|
163 | 163 | %% @private |
164 | 164 | escape_metric_help(Help) -> |
165 | | - sub(sub(Help, "\\", "\\\\\\\\"), "\n", "\\\\n"). |
| 165 | + escape_string(fun escape_help_char/1, Help). |
166 | 166 |
|
167 | | -bound_to_label_value(Bound) when is_number(Bound) -> |
168 | | - io_lib:format("~p", [Bound]); |
| 167 | +%% @private |
| 168 | +escape_help_char($\\ = X) -> |
| 169 | + <<X, X>>; |
| 170 | +escape_help_char($\n) -> |
| 171 | + <<$\\, $n>>; |
| 172 | +escape_help_char(X) -> |
| 173 | + <<X>>. |
| 174 | + |
| 175 | +bound_to_label_value(Bound) when is_integer(Bound) -> |
| 176 | + integer_to_list(Bound); |
| 177 | +bound_to_label_value(Bound) when is_float(Bound) -> |
| 178 | + float_to_list(Bound); |
169 | 179 | bound_to_label_value(infinity) -> |
170 | 180 | "+Inf". |
171 | 181 |
|
172 | | --spec escape_label_value(binary() | iolist() | undefined) -> string(). |
| 182 | +-spec escape_label_value(binary() | iolist() | undefined) -> binary(). |
173 | 183 | %% @private |
174 | | -escape_label_value(LValue) when is_list(LValue)-> |
175 | | - ?ESCAPE_LVALUE(LValue); |
176 | | -escape_label_value(LValue) when is_binary(LValue) -> |
177 | | - ?ESCAPE_LVALUE(LValue); |
| 184 | +escape_label_value(LValue) when is_list(LValue); is_binary(LValue) -> |
| 185 | + escape_string(fun escape_label_char/1, LValue); |
178 | 186 | escape_label_value(Value) -> |
179 | 187 | erlang:error({wtf, Value}). |
180 | 188 |
|
| 189 | +%% @private |
| 190 | +escape_label_char($\\ = X) -> |
| 191 | + <<X, X>>; |
| 192 | +escape_label_char($\n) -> |
| 193 | + <<$\\, $n>>; |
| 194 | +escape_label_char($" = X) -> |
| 195 | + <<$\\, X>>; |
| 196 | +escape_label_char(X) -> |
| 197 | + <<X>>. |
181 | 198 |
|
182 | | --spec sub(iodata(), string(), string()) -> string(). |
183 | | -sub(Str, Old, New) -> |
184 | | - RegExp = "\\Q" ++ Old ++ "\\E", |
185 | | - re:replace(Str, RegExp, New, [global, {return, list}]). |
| 199 | +%% @private |
| 200 | +escape_string(Fun, Str) when is_binary(Str) -> |
| 201 | + << <<(Fun(X))/binary>> || <<X:8>> <= Str >>; |
| 202 | +escape_string(Fun, Str) -> |
| 203 | + escape_string(Fun, iolist_to_binary(Str)). |
186 | 204 |
|
187 | 205 | %% |
188 | 206 | %% %CopyrightBegin% |
|
0 commit comments