DebugWriter.cpp 3.1 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. void __cdecl DebugWriter::Print(const Char *form, ...)
  14. {
  15. va_list argptr;
  16. va_start(argptr, form);
  17. int len = _vsnwprintf_s(buf, bufLen, _TRUNCATE, form, argptr);
  18. if (len < 0)
  19. Output::Print(_u("<not enough buffer space to format>"));
  20. else
  21. {
  22. if (len > 0)
  23. CheckForNewline();
  24. Output::Print(_u("%s"), buf);
  25. }
  26. }
  27. void __cdecl DebugWriter::PrintEOL(const Char *form, ...)
  28. {
  29. va_list argptr;
  30. va_start(argptr, form);
  31. int len = _vsnwprintf_s(buf, bufLen, _TRUNCATE, form, argptr);
  32. Assert(len >= 0 && len < bufLen - 1);
  33. if (len > 0)
  34. CheckForNewline();
  35. Output::Print(_u("%s"), buf);
  36. EOL();
  37. }
  38. void DebugWriter::PrintEscapedString(const Char* str, CharCount len)
  39. {
  40. Assert(str != 0);
  41. CheckForNewline();
  42. const Char* pl = str + len;
  43. for (const Char* p = str; p < pl; p++)
  44. {
  45. if (*p == '"')
  46. Output::Print(_u("\\\""));
  47. else
  48. PrintEscapedChar(*p);
  49. }
  50. }
  51. void DebugWriter::PrintQuotedString(const Char* str, CharCount len)
  52. {
  53. CheckForNewline();
  54. if (str == 0)
  55. Output::Print(_u("null"));
  56. else
  57. {
  58. Output::Print(_u("\""));
  59. PrintEscapedString(str, len);
  60. Output::Print(_u("\""));
  61. }
  62. }
  63. void DebugWriter::PrintEscapedChar(const Char c)
  64. {
  65. CheckForNewline();
  66. if (c > 0xff)
  67. Output::Print(_u("\\u%lc%lc%lc%lc"), hex[c >> 12], hex[(c >> 8) & 0xf], hex[(c >> 4) & 0xf], hex[c & 0xf]);
  68. else if (c < ' ' || c > '~')
  69. Output::Print(_u("\\x%lc%lc"), hex[c >> 4], hex[c & 0xf]);
  70. else
  71. Output::Print(_u("%lc"), c);
  72. }
  73. void DebugWriter::PrintQuotedChar(const Char c)
  74. {
  75. CheckForNewline();
  76. Output::Print(_u("'"));
  77. if (c == '\'')
  78. Output::Print(_u("\\'"));
  79. else
  80. PrintEscapedChar(c);
  81. Output::Print(_u("'"));
  82. }
  83. void DebugWriter::EOL()
  84. {
  85. CheckForNewline();
  86. nlPending = true;
  87. }
  88. void DebugWriter::Indent()
  89. {
  90. indent++;
  91. }
  92. void DebugWriter::Unindent()
  93. {
  94. indent--;
  95. }
  96. void DebugWriter::Flush()
  97. {
  98. Output::Print(_u("\n"));
  99. Output::Flush();
  100. nlPending = false;
  101. }
  102. void DebugWriter::BeginLine()
  103. {
  104. Output::Print(_u("\n%*s"), indent * 4, _u(""));
  105. }
  106. }
  107. #endif