yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

tareksanderReflection compiler option (#5507)7e51180ed

master
1.7 KiB85 linesraw
1#include "slang-pretty-writer.h"
2
3#include "../core/slang-string-escape-util.h"
4
5namespace Slang
6{
7
8void PrettyWriter::writeRaw(char const* begin, char const* end)
9{
10    SLANG_ASSERT(end >= begin);
11    writeRaw(UnownedStringSlice(begin, end));
12}
13
14void PrettyWriter::adjust()
15{
16    // Only indent if at start of a line
17    if (m_startOfLine)
18    {
19        // Output current indentation
20        m_builder.appendRepeatedChar(' ', m_indent * 4);
21        m_startOfLine = false;
22    }
23}
24
25void PrettyWriter::dedent()
26{
27    SLANG_ASSERT(m_indent > 0);
28    m_indent--;
29}
30
31void PrettyWriter::write(const UnownedStringSlice& slice)
32{
33    const auto end = slice.end();
34    auto start = slice.begin();
35
36    while (start < end)
37    {
38        const char* cur = start;
39
40        // Search for \n if there is one
41        while (cur < end && *cur != '\n')
42            cur++;
43
44        // If there were some chars, adjust and write
45        if (cur > start)
46        {
47            adjust();
48            writeRaw(UnownedStringSlice(start, cur));
49        }
50
51        if (cur < end && *cur == '\n')
52        {
53            writeRawChar('\n');
54            // Skip the CR
55            cur++;
56            // Mark we are at the start of a line
57            m_startOfLine = true;
58        }
59
60        start = cur;
61    }
62}
63
64void PrettyWriter::writeEscapedString(const UnownedStringSlice& slice)
65{
66    adjust();
67    auto handler = StringEscapeUtil::getHandler(StringEscapeUtil::Style::Cpp);
68    StringEscapeUtil::appendQuoted(handler, slice, m_builder);
69}
70
71void PrettyWriter::maybeComma()
72{
73    if (auto state = m_commaState)
74    {
75        if (!state->needComma)
76        {
77            state->needComma = true;
78            return;
79        }
80    }
81
82    write(toSlice(",\n"));
83}
84
85} // namespace Slang