DebugWriter.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #include "ParserPch.h"
  6. #if ENABLE_REGEX_CONFIG_OPTIONS
  7. namespace UnifiedRegex
  8. {
  9. const char16* const DebugWriter::hex = _u("0123456789abcdef");
  10. DebugWriter::DebugWriter() : indent(0), nlPending(false)
  11. {
  12. }
  13. #define PRINT_DEBUG_WRITER() \
  14. va_list argptr; \
  15. va_start(argptr, form); \
  16. int len = _vsnwprintf_s(buf, bufLen, _TRUNCATE, form, argptr); \
  17. if (len < 0 || len >= bufLen - 1) \
  18. Output::Print(_u("<not enough buffer space to format>")); \
  19. else \
  20. { \
  21. if (len > 0) \
  22. CheckForNewline(); \
  23. Output::Print(_u("%s"), buf); \
  24. } \
  25. va_end(argptr)
  26. void __cdecl DebugWriter::Print(const Char *form, ...)
  27. {
  28. PRINT_DEBUG_WRITER();
  29. }
  30. void __cdecl DebugWriter::PrintEOL(const Char *form, ...)
  31. {
  32. PRINT_DEBUG_WRITER();
  33. EOL();
  34. }
  35. void DebugWriter::PrintEscapedString(const Char* str, CharCount len)
  36. {
  37. Assert(str != 0);
  38. CheckForNewline();
  39. const Char* pl = str + len;
  40. for (const Char* p = str; p < pl; p++)
  41. {
  42. if (*p == '"')
  43. Output::Print(_u("\\\""));
  44. else
  45. PrintEscapedChar(*p);
  46. }
  47. }
  48. void DebugWriter::PrintQuotedString(const Char* str, CharCount len)
  49. {
  50. CheckForNewline();
  51. if (str == 0)
  52. Output::Print(_u("null"));
  53. else
  54. {
  55. Output::Print(_u("\""));
  56. PrintEscapedString(str, len);
  57. Output::Print(_u("\""));
  58. }
  59. }
  60. void DebugWriter::PrintEscapedChar(const Char c)
  61. {
  62. CheckForNewline();
  63. if (c > 0xff)
  64. Output::Print(_u("\\u%lc%lc%lc%lc"), hex[c >> 12], hex[(c >> 8) & 0xf], hex[(c >> 4) & 0xf], hex[c & 0xf]);
  65. else if (c == '-')
  66. Output::Print(_u("\\x2d"));
  67. else if (c < ' ' || c > '~')
  68. Output::Print(_u("\\x%lc%lc"), hex[c >> 4], hex[c & 0xf]);
  69. else
  70. Output::Print(_u("%lc"), c);
  71. }
  72. void DebugWriter::PrintQuotedChar(const Char c)
  73. {
  74. CheckForNewline();
  75. Output::Print(_u("'"));
  76. if (c == '\'')
  77. Output::Print(_u("\\'"));
  78. else
  79. PrintEscapedChar(c);
  80. Output::Print(_u("'"));
  81. }
  82. void DebugWriter::EOL()
  83. {
  84. CheckForNewline();
  85. nlPending = true;
  86. }
  87. void DebugWriter::Indent()
  88. {
  89. indent++;
  90. }
  91. void DebugWriter::Unindent()
  92. {
  93. indent--;
  94. }
  95. void DebugWriter::Flush()
  96. {
  97. Output::Print(_u("\n"));
  98. Output::Flush();
  99. nlPending = false;
  100. }
  101. void DebugWriter::BeginLine()
  102. {
  103. Output::Print(_u("\n%*s"), indent * 4, _u(""));
  104. }
  105. }
  106. #endif