yum-mirror/slang

Making it easier to work with shaders

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

Yong HeMake interface types non c-style in Slang2026. (#7260)812e47898

master
4.6 KiB127 linesraw
1#ifndef SLANG_CORE_STRING_ESCAPE_UTIL_H
2#define SLANG_CORE_STRING_ESCAPE_UTIL_H
3
4#include "slang-list.h"
5#include "slang-string.h"
6
7namespace Slang
8{
9
10class StringEscapeHandler
11{
12public:
13    /// True if quoting is needed
14    virtual bool isQuotingNeeded(const UnownedStringSlice& slice) = 0;
15    /// True if any escaping is needed. If not slice can be used (assuming appropriate quoting) as
16    /// is
17    virtual bool isEscapingNeeded(const UnownedStringSlice& slice) = 0;
18    /// True if we need to unescape
19    virtual bool isUnescapingNeeeded(const UnownedStringSlice& slice) = 0;
20
21    /// Takes slice and adds any appropriate escaping (for example C++/C type escaping for special
22    /// characters like '\', '"' and if not ascii will write out as hex sequence) Does not append
23    /// quotes
24    virtual SlangResult appendEscaped(const UnownedStringSlice& slice, StringBuilder& out) = 0;
25    /// Given a slice append it unescaped
26    /// Does not consume surrounding quotes
27    virtual SlangResult appendUnescaped(const UnownedStringSlice& slice, StringBuilder& out) = 0;
28
29    /// Lex quoted text.
30    /// The first character of cursor should be the quoteCharacter.
31    /// cursor points to the string to be lexed - must typically be 0 terminated.
32    /// outCursor on successful lex will be at the next character after was processed.
33    virtual SlangResult lexQuoted(const char* cursor, const char** outCursor) = 0;
34
35    SLANG_FORCE_INLINE char getQuoteChar() const { return m_quoteChar; }
36
37    StringEscapeHandler(char quoteChar)
38        : m_quoteChar(quoteChar)
39    {
40    }
41
42protected:
43    const char m_quoteChar;
44};
45
46/* A set of function that can be used for escaping/unescaping quoting/unquoting strings.
47
48The distinction between 'escaping' and 'quoting' here, is just that escaping is the 'payload' of
49quotes. In *principal* the Style can determine different styles of escaping that can be used.
50*/
51struct StringEscapeUtil
52{
53    typedef StringEscapeHandler Handler;
54
55    enum class Style
56    {
57        Cpp,   ///< Cpp style quoting and escape handling
58        Space, ///< Applies quotes if there are spaces. Does not escape.
59        JSON,  ///< Json encoding
60        Slang, ///< Slang style string encoding (For now same as Cpp but that may change in the
61               ///< future)
62        CountOf,
63    };
64
65    /// Given a style returns a handler
66    static Handler* getHandler(Style style);
67
68    /// Get without quotes. Will assert if not correctly quoted
69    static UnownedStringSlice unquote(char quoteChar, const UnownedStringSlice& slice);
70    static UnownedStringSlice unquote(Handler* handler, const UnownedStringSlice& slice)
71    {
72        return unquote(handler->getQuoteChar(), slice);
73    }
74
75    /// True is slice is quoted
76    static bool isQuoted(char quoteChar, UnownedStringSlice& slice);
77    static bool isQuoted(Handler* handler, UnownedStringSlice& slice)
78    {
79        return isQuoted(handler->getQuoteChar(), slice);
80    }
81
82    /// Given a command line arg slice, if it is quoted, unquotes it, else returns the slice as is.
83    static UnownedStringSlice maybeUnquoteCommandLineArg(UnownedStringSlice slice);
84
85    /// If quoting is needed appends to out quoted
86    static SlangResult appendMaybeQuoted(
87        Handler* handler,
88        const UnownedStringSlice& slice,
89        StringBuilder& out);
90
91    /// If the slice appears to be quoted for the style, unquote it, else just append to out
92    static SlangResult appendMaybeUnquoted(
93        Handler* handler,
94        const UnownedStringSlice& slice,
95        StringBuilder& out);
96
97    /// Appends to out slice without quotes
98    static SlangResult appendUnquoted(
99        Handler* handler,
100        const UnownedStringSlice& slice,
101        StringBuilder& out);
102
103    /// Append with quotes (even if not needed)
104    static SlangResult appendQuoted(
105        Handler* handler,
106        const UnownedStringSlice& slice,
107        StringBuilder& out);
108
109
110    /// True if requires 'shell-like' unescape. With shell-like, quoting does *not* have to start at
111    /// the start of the slice. and there may be multiple quoted section
112    static SlangResult isUnescapeShellLikeNeeded(Handler* handler, const UnownedStringSlice& slice);
113
114    /// Shells can have multiple quoted sections. This function makes a string with out quoting
115    static SlangResult unescapeShellLike(
116        Handler* handler,
117        const UnownedStringSlice& slice,
118        StringBuilder& out);
119
120    static String escapeString(UnownedStringSlice input, Style style = Style::Slang);
121    static String unescapeString(UnownedStringSlice input, Style style = Style::Slang);
122};
123
124
125} // namespace Slang
126
127#endif // SLANG_CORE_STRING_ESCAPE_UTIL_H