raw_formatter.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #pragma once
  2. #include "guid.h"
  3. #include <algorithm>
  4. #include <array>
  5. #include <util/generic/strbuf.h>
  6. namespace NYT {
  7. ////////////////////////////////////////////////////////////////////////////////
  8. //! A dead-simple string formatter.
  9. /*!
  10. * This formatter is intended to be as simple as possible and async signal safe.
  11. * This is the reason we do not use printf(): it does not meet signal-safety
  12. * requirements.
  13. */
  14. class TBaseFormatter
  15. {
  16. public:
  17. TBaseFormatter(char* buffer, int length)
  18. : Begin_(buffer)
  19. , Cursor_(buffer)
  20. , End_(buffer + length)
  21. { }
  22. //! Returns an underlying cursor.
  23. char* GetCursor()
  24. {
  25. return Cursor_;
  26. }
  27. //! Returns an pointer to the underlying buffer.
  28. const char* GetData() const
  29. {
  30. return Begin_;
  31. }
  32. //! Returns the number of bytes written in the buffer.
  33. int GetBytesWritten() const
  34. {
  35. return Cursor_ - Begin_;
  36. }
  37. //! Returns the number of bytes available in the buffer.
  38. int GetBytesRemaining() const
  39. {
  40. return End_ - Cursor_;
  41. }
  42. //! Advances the internal cursor #count symbols forward (assuming the data is already present).
  43. void Advance(int count)
  44. {
  45. Cursor_ += count;
  46. if (Cursor_ > End_) {
  47. Cursor_ = End_;
  48. }
  49. }
  50. //! Drops trailing #count symbols (assuming these are present).
  51. void Revert(int count)
  52. {
  53. Cursor_ -= count;
  54. }
  55. //! Appends the string and updates the internal cursor.
  56. void AppendString(const char* string)
  57. {
  58. while (*string != '\0' && Cursor_ < End_) {
  59. *Cursor_++ = *string++;
  60. }
  61. }
  62. //! Appends the string and updates the internal cursor.
  63. void AppendString(TStringBuf string)
  64. {
  65. size_t position = 0;
  66. while (position < string.length() && Cursor_ < End_) {
  67. *Cursor_++ = string[position++];
  68. }
  69. }
  70. //! Appends a single character given number of times and updates the internal cursor.
  71. void AppendChar(char ch, int count = 1)
  72. {
  73. while (Cursor_ < End_ && count > 0) {
  74. *Cursor_++ = ch;
  75. count--;
  76. }
  77. }
  78. //! Formats |number| in base |radix| and updates the internal cursor.
  79. void AppendNumber(uintptr_t number, int radix = 10, int width = 0, char ch = ' ')
  80. {
  81. int digits = 0;
  82. width = std::min(width, GetBytesRemaining());
  83. if (radix == 16) {
  84. // Optimize output of hex numbers.
  85. uintptr_t reverse = 0;
  86. int length = 0;
  87. do {
  88. reverse <<= 4;
  89. reverse |= number & 0xf;
  90. number >>= 4;
  91. ++length;
  92. } while (number > 0);
  93. for (int index = 0; index < length && Cursor_ + digits < End_; ++index) {
  94. unsigned int modulus = reverse & 0xf;
  95. Cursor_[digits] = (modulus < 10 ? '0' + modulus : 'a' + modulus - 10);
  96. ++digits;
  97. reverse >>= 4;
  98. }
  99. } else {
  100. while (Cursor_ + digits < End_) {
  101. const int modulus = number % radix;
  102. number /= radix;
  103. Cursor_[digits] = (modulus < 10 ? '0' + modulus : 'a' + modulus - 10);
  104. ++digits;
  105. if (number == 0) {
  106. break;
  107. }
  108. }
  109. // Reverse the bytes written.
  110. std::reverse(Cursor_, Cursor_ + digits);
  111. }
  112. if (digits < width) {
  113. auto delta = width - digits;
  114. std::copy(Cursor_, Cursor_ + digits, Cursor_ + delta);
  115. std::fill(Cursor_, Cursor_ + delta, ch);
  116. Cursor_ += width;
  117. } else {
  118. Cursor_ += digits;
  119. }
  120. }
  121. //! Formats |guid| and updates the internal cursor.
  122. void AppendGuid(TGuid guid)
  123. {
  124. if (Y_LIKELY(End_ - Cursor_ >= MaxGuidStringSize)) {
  125. // Fast path.
  126. Cursor_ = WriteGuidToBuffer(Cursor_, guid);
  127. } else {
  128. // Slow path.
  129. std::array<char, MaxGuidStringSize> buffer;
  130. auto* end = WriteGuidToBuffer(buffer.data(), guid);
  131. AppendString(TStringBuf(buffer.data(), end));
  132. }
  133. }
  134. //! Resets the underlying cursor.
  135. void Reset()
  136. {
  137. Cursor_ = Begin_;
  138. }
  139. TStringBuf GetBuffer() const
  140. {
  141. return {Begin_, Cursor_};
  142. }
  143. private:
  144. char* const Begin_;
  145. char* Cursor_;
  146. char* const End_;
  147. };
  148. template <size_t N>
  149. class TRawFormatter
  150. : public TBaseFormatter
  151. {
  152. public:
  153. TRawFormatter()
  154. : TBaseFormatter(Buffer_, N)
  155. { }
  156. TRawFormatter(char* buffer, int length)
  157. : TBaseFormatter(buffer, length)
  158. { }
  159. private:
  160. char Buffer_[N];
  161. };
  162. ////////////////////////////////////////////////////////////////////////////////
  163. } // namespace NYT