//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
#include "ParserPch.h"

#if ENABLE_REGEX_CONFIG_OPTIONS

namespace UnifiedRegex
{
    const wchar16* const DebugWriter::hex = CH_WSTR("0123456789abcdef");

    DebugWriter::DebugWriter() : indent(0), nlPending(false)
    {
    }

    void __cdecl DebugWriter::Print(const Char *form, ...)
    {
        va_list argptr;
        va_start(argptr, form);
        int len = _vsnwprintf_s(buf, bufLen, _TRUNCATE, form, argptr);
        if (len < 0)
            Output::Print(CH_WSTR("<not enough buffer space to format>"));
        else
        {
            if (len > 0)
                CheckForNewline();
            Output::Print(CH_WSTR("%s"), buf);
        }
    }

    void __cdecl DebugWriter::PrintEOL(const Char *form, ...)
    {
        va_list argptr;
        va_start(argptr, form);
        int len = _vsnwprintf_s(buf, bufLen, _TRUNCATE, form, argptr);
        Assert(len >= 0 && len < bufLen - 1);
        if (len > 0)
            CheckForNewline();
        Output::Print(CH_WSTR("%s"), buf);
        EOL();
    }

    void DebugWriter::PrintEscapedString(const Char* str, CharCount len)
    {
        Assert(str != 0);
        CheckForNewline();

        const Char* pl = str + len;
        for (const Char* p = str; p < pl; p++)
        {
            if (*p == '"')
                Output::Print(CH_WSTR("\\\""));
            else
                PrintEscapedChar(*p);
        }
    }

    void DebugWriter::PrintQuotedString(const Char* str, CharCount len)
    {
        CheckForNewline();
        if (str == 0)
            Output::Print(CH_WSTR("null"));
        else
        {
            Output::Print(CH_WSTR("\""));
            PrintEscapedString(str, len);
            Output::Print(CH_WSTR("\""));
        }
    }

    void DebugWriter::PrintEscapedChar(const Char c)
    {
        CheckForNewline();
        if (c > 0xff)
            Output::Print(CH_WSTR("\\u%lc%lc%lc%lc"), hex[c >> 12], hex[(c >> 8) & 0xf], hex[(c >> 4) & 0xf], hex[c & 0xf]);
        else if (c < ' ' || c > '~')
            Output::Print(CH_WSTR("\\x%lc%lc"), hex[c >> 4], hex[c & 0xf]);
        else
            Output::Print(CH_WSTR("%lc"), c);
    }

    void DebugWriter::PrintQuotedChar(const Char c)
    {
        CheckForNewline();
        Output::Print(CH_WSTR("'"));
        if (c == '\'')
            Output::Print(CH_WSTR("\\'"));
        else
            PrintEscapedChar(c);
        Output::Print(CH_WSTR("'"));
    }

    void DebugWriter::EOL()
    {
        CheckForNewline();
        nlPending = true;
    }

    void DebugWriter::Indent()
    {
        indent++;
    }

    void DebugWriter::Unindent()
    {
        indent--;
    }

    void DebugWriter::Flush()
    {
        Output::Print(CH_WSTR("\n"));
        Output::Flush();
        nlPending = false;
    }

    void DebugWriter::BeginLine()
    {
        Output::Print(CH_WSTR("\n%*s"), indent * 4, CH_WSTR(""));
    }
}

#endif
