Skip to content

Commit 8f3a965

Browse files
authored
Refactor ansi_color_escape to track size instead of using a null terminator (#4511)
1 parent ae8cb1e commit 8f3a965

File tree

1 file changed

+15
-18
lines changed

1 file changed

+15
-18
lines changed

include/fmt/color.h

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -375,19 +375,17 @@ template <typename Char> struct ansi_color_escape {
375375
// 10 more.
376376
if (is_background) value += 10u;
377377

378-
size_t index = 0;
379-
buffer[index++] = static_cast<Char>('\x1b');
380-
buffer[index++] = static_cast<Char>('[');
378+
buffer[size++] = static_cast<Char>('\x1b');
379+
buffer[size++] = static_cast<Char>('[');
381380

382381
if (value >= 100u) {
383-
buffer[index++] = static_cast<Char>('1');
382+
buffer[size++] = static_cast<Char>('1');
384383
value %= 100u;
385384
}
386-
buffer[index++] = static_cast<Char>('0' + value / 10u);
387-
buffer[index++] = static_cast<Char>('0' + value % 10u);
385+
buffer[size++] = static_cast<Char>('0' + value / 10u);
386+
buffer[size++] = static_cast<Char>('0' + value % 10u);
388387

389-
buffer[index++] = static_cast<Char>('m');
390-
buffer[index++] = static_cast<Char>('\0');
388+
buffer[size++] = static_cast<Char>('m');
391389
return;
392390
}
393391

@@ -398,7 +396,7 @@ template <typename Char> struct ansi_color_escape {
398396
to_esc(color.r, buffer + 7, ';');
399397
to_esc(color.g, buffer + 11, ';');
400398
to_esc(color.b, buffer + 15, 'm');
401-
buffer[19] = static_cast<Char>(0);
399+
size = 19;
402400
}
403401
FMT_CONSTEXPR ansi_color_escape(emphasis em) noexcept {
404402
uint8_t em_codes[num_emphases] = {};
@@ -411,26 +409,25 @@ template <typename Char> struct ansi_color_escape {
411409
if (has_emphasis(em, emphasis::conceal)) em_codes[6] = 8;
412410
if (has_emphasis(em, emphasis::strikethrough)) em_codes[7] = 9;
413411

414-
size_t index = 0;
415412
for (size_t i = 0; i < num_emphases; ++i) {
416413
if (!em_codes[i]) continue;
417-
buffer[index++] = static_cast<Char>('\x1b');
418-
buffer[index++] = static_cast<Char>('[');
419-
buffer[index++] = static_cast<Char>('0' + em_codes[i]);
420-
buffer[index++] = static_cast<Char>('m');
414+
buffer[size++] = static_cast<Char>('\x1b');
415+
buffer[size++] = static_cast<Char>('[');
416+
buffer[size++] = static_cast<Char>('0' + em_codes[i]);
417+
buffer[size++] = static_cast<Char>('m');
421418
}
422-
buffer[index++] = static_cast<Char>(0);
423419
}
424420
FMT_CONSTEXPR operator const Char*() const noexcept { return buffer; }
425421

426422
FMT_CONSTEXPR auto begin() const noexcept -> const Char* { return buffer; }
427-
FMT_CONSTEXPR20 auto end() const noexcept -> const Char* {
428-
return buffer + basic_string_view<Char>(buffer).size();
423+
FMT_CONSTEXPR auto end() const noexcept -> const Char* {
424+
return buffer + size;
429425
}
430426

431427
private:
432428
static constexpr size_t num_emphases = 8;
433-
Char buffer[7u + 4u * num_emphases + 1u];
429+
Char buffer[7u + 4u * num_emphases];
430+
size_t size = 0;
434431

435432
static FMT_CONSTEXPR void to_esc(uint8_t c, Char* out,
436433
char delimiter) noexcept {

0 commit comments

Comments
 (0)