raw_formatter.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 and updates the internal cursor.
  71. void AppendChar(char ch)
  72. {
  73. if (Cursor_ < End_) {
  74. *Cursor_++ = ch;
  75. }
  76. }
  77. //! Formats |number| in base |radix| and updates the internal cursor.
  78. void AppendNumber(uintptr_t number, int radix = 10, int width = 0, char ch = ' ')
  79. {
  80. int digits = 0;
  81. if (radix == 16) {
  82. // Optimize output of hex numbers.
  83. uintptr_t reverse = 0;
  84. int length = 0;
  85. do {
  86. reverse <<= 4;
  87. reverse |= number & 0xf;
  88. number >>= 4;
  89. ++length;
  90. } while (number > 0);
  91. for (int index = 0; index < length && Cursor_ + digits < End_; ++index) {
  92. unsigned int modulus = reverse & 0xf;
  93. Cursor_[digits] = (modulus < 10 ? '0' + modulus : 'a' + modulus - 10);
  94. ++digits;
  95. reverse >>= 4;
  96. }
  97. } else {
  98. while (Cursor_ + digits < End_) {
  99. const int modulus = number % radix;
  100. number /= radix;
  101. Cursor_[digits] = (modulus < 10 ? '0' + modulus : 'a' + modulus - 10);
  102. ++digits;
  103. if (number == 0) {
  104. break;
  105. }
  106. }
  107. // Reverse the bytes written.
  108. std::reverse(Cursor_, Cursor_ + digits);
  109. }
  110. if (digits < width) {
  111. auto delta = width - digits;
  112. std::copy(Cursor_, Cursor_ + digits, Cursor_ + delta);
  113. std::fill(Cursor_, Cursor_ + delta, ch);
  114. Cursor_ += width;
  115. } else {
  116. Cursor_ += digits;
  117. }
  118. }
  119. //! Formats |number| as hexadecimal number and updates the internal cursor.
  120. //! Padding will be added in front if needed.
  121. void AppendNumberAsHexWithPadding(uintptr_t number, int width)
  122. {
  123. char* begin = Cursor_;
  124. AppendString("0x");
  125. AppendNumber(number, 16);
  126. // Move to right and add padding in front if needed.
  127. if (Cursor_ < begin + width) {
  128. auto delta = begin + width - Cursor_;
  129. std::copy(begin, Cursor_, begin + delta);
  130. std::fill(begin, begin + delta, ' ');
  131. Cursor_ = begin + width;
  132. }
  133. }
  134. //! Formats |guid| and updates the internal cursor.
  135. void AppendGuid(TGuid guid)
  136. {
  137. if (Y_LIKELY(End_ - Cursor_ >= MaxGuidStringSize)) {
  138. // Fast path.
  139. Cursor_ = WriteGuidToBuffer(Cursor_, guid);
  140. } else {
  141. // Slow path.
  142. std::array<char, MaxGuidStringSize> buffer;
  143. auto* end = WriteGuidToBuffer(buffer.data(), guid);
  144. AppendString(TStringBuf(buffer.data(), end));
  145. }
  146. }
  147. //! Resets the underlying cursor.
  148. void Reset()
  149. {
  150. Cursor_ = Begin_;
  151. }
  152. TStringBuf GetBuffer() const
  153. {
  154. return {Begin_, Cursor_};
  155. }
  156. private:
  157. char* const Begin_;
  158. char* Cursor_;
  159. char* const End_;
  160. };
  161. template <size_t N>
  162. class TRawFormatter
  163. : public TBaseFormatter
  164. {
  165. public:
  166. TRawFormatter()
  167. : TBaseFormatter(Buffer_, N)
  168. { }
  169. TRawFormatter(char* buffer, int length)
  170. : TBaseFormatter(buffer, length)
  171. { }
  172. private:
  173. char Buffer_[N];
  174. };
  175. ////////////////////////////////////////////////////////////////////////////////
  176. } // namespace NYT