yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
812e47898
master
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 14virtual 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 17virtual bool isEscapingNeeded (const UnownedStringSlice & slice )= 0 ; 18/// True if we need to unescape 19virtual 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 24virtual SlangResult appendEscaped (const UnownedStringSlice & slice ,StringBuilder & out )= 0 ; 25/// Given a slice append it unescaped 26/// Does not consume surrounding quotes 27virtual 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. 33virtual SlangResult lexQuoted (const char * cursor ,const char ** outCursor )= 0 ; 34 35SLANG_FORCE_INLINE char getQuoteChar ()const {return m_quoteChar ; } 36 37StringEscapeHandler (char quoteChar ) 38 :m_quoteChar (quoteChar ) 39 { 40 } 41 42protected : 43const 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{ 53typedef StringEscapeHandler Handler ; 54 55enum class Style 56 { 57Cpp ,///< Cpp style quoting and escape handling 58Space ,///< Applies quotes if there are spaces. Does not escape. 59JSON ,///< Json encoding 60Slang ,///< Slang style string encoding (For now same as Cpp but that may change in the 61///< future) 62CountOf , 63 }; 64 65/// Given a style returns a handler 66static Handler * getHandler (Style style ); 67 68/// Get without quotes. Will assert if not correctly quoted 69static UnownedStringSlice unquote (char quoteChar ,const UnownedStringSlice & slice ); 70static UnownedStringSlice unquote (Handler * handler ,const UnownedStringSlice & slice ) 71 { 72return unquote (handler -> getQuoteChar (),slice ); 73 } 74 75/// True is slice is quoted 76static bool isQuoted (char quoteChar ,UnownedStringSlice & slice ); 77static bool isQuoted (Handler * handler ,UnownedStringSlice & slice ) 78 { 79return 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. 83static UnownedStringSlice maybeUnquoteCommandLineArg (UnownedStringSlice slice ); 84 85/// If quoting is needed appends to out quoted 86static SlangResult appendMaybeQuoted ( 87Handler * handler , 88const UnownedStringSlice & slice , 89StringBuilder & out ); 90 91/// If the slice appears to be quoted for the style, unquote it, else just append to out 92static SlangResult appendMaybeUnquoted ( 93Handler * handler , 94const UnownedStringSlice & slice , 95StringBuilder & out ); 96 97/// Appends to out slice without quotes 98static SlangResult appendUnquoted ( 99Handler * handler , 100const UnownedStringSlice & slice , 101StringBuilder & out ); 102 103/// Append with quotes (even if not needed) 104static SlangResult appendQuoted ( 105Handler * handler , 106const UnownedStringSlice & slice , 107StringBuilder & 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 112static SlangResult isUnescapeShellLikeNeeded (Handler * handler ,const UnownedStringSlice & slice ); 113 114/// Shells can have multiple quoted sections. This function makes a string with out quoting 115static SlangResult unescapeShellLike ( 116Handler * handler , 117const UnownedStringSlice & slice , 118StringBuilder & out ); 119 120static String escapeString (UnownedStringSlice input ,Style style = Style ::Slang ); 121static String unescapeString (UnownedStringSlice input ,Style style = Style ::Slang ); 122}; 123 124 125}// namespace Slang 126 127#endif // SLANG_CORE_STRING_ESCAPE_UTIL_H