From 19c0866b050a022406867aa650302f4efbf8e010 Mon Sep 17 00:00:00 2001 From: jsmall-nvidia Date: Sat, 29 Apr 2023 09:24:26 -0400 Subject: CommandOptions (#2856) * WIP CommandOptions * Fix some output issues. * Simplify word wrapping. * Add file extensions. * Change how lookup takes place. Add appendSplit functions to StringUtil. Make Categories hold the index range of their options. * Small improvement. * Lookup with partial option names. * Associate user values. * Encoding flags in the name. * Refactor setting up of command options. * Use CommandOptions in slang-options. * Remove old help text. * Cache the CommandOptions on the Session. * Range checking. Fix bug in the Options handling. * Extra checks for validity. * Get categories directly. * Slight improvements over output. * Added NameValue types. * Fix typo. Remove some now unused diagnostics. Fix diagnostic in testing, as output has changed. * Add minimal usage message. * Remove platform executable extension from diagnostics output. * Some improvements around getting names from NameValue types. * Improve some option descriptions. * Small fixes. --- source/core/slang-name-value.cpp | 163 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 163 insertions(+) create mode 100644 source/core/slang-name-value.cpp (limited to 'source/core/slang-name-value.cpp') diff --git a/source/core/slang-name-value.cpp b/source/core/slang-name-value.cpp new file mode 100644 index 000000000..5418043f5 --- /dev/null +++ b/source/core/slang-name-value.cpp @@ -0,0 +1,163 @@ +// slang-name-value.cpp + +#include "slang-name-value.h" + +#include "slang-string-util.h" +#include "slang-char-util.h" + +namespace Slang { + +/* static */ValueInt NameValueUtil::findValue(const ConstArrayView& opts, const UnownedStringSlice& slice, ValueInt defaultValue) +{ + for (const auto& opt : opts) + { + if (UnownedStringSlice(opt.name) == slice) + { + return opt.value; + } + } + return defaultValue; +} + +/* static */ValueInt NameValueUtil::findValue(const ConstArrayView& opts, const UnownedStringSlice& slice, ValueInt defaultValue) +{ + for (const auto& opt : opts) + { + UnownedStringSlice names(opt.names); + + if (StringUtil::indexOfInSplit(names, ',', slice) >= 0) + { + return opt.value; + } + } + return defaultValue; +} + +/* static */ValueInt NameValueUtil::findValue(const ConstArrayView& opts, const UnownedStringSlice& slice, ValueInt defaultValue) +{ + for (const auto& opt : opts) + { + UnownedStringSlice names(opt.names); + if (StringUtil::indexOfInSplit(names, ',', slice) >= 0) + { + return opt.value; + } + } + return defaultValue; +} + +/* static */ UnownedStringSlice NameValueUtil::findName(const ConstArrayView& opts, ValueInt value, const UnownedStringSlice& defaultName) +{ + for (const auto& opt : opts) + { + if (opt.value == value) + { + return UnownedStringSlice(opt.name); + } + } + return defaultName; +} + +/* static */ UnownedStringSlice NameValueUtil::findName(const ConstArrayView& opts, ValueInt value, const UnownedStringSlice& defaultName) +{ + for (const auto& opt : opts) + { + if (opt.value == value) + { + // Get the first name + return StringUtil::getAtInSplit(UnownedStringSlice(opt.names), ',', 0); + } + } + + return defaultName; +} + +/* static */ UnownedStringSlice NameValueUtil::findName(const ConstArrayView& opts, ValueInt value, const UnownedStringSlice& defaultName) +{ + for (const auto& opt : opts) + { + if (opt.value == value) + { + // Get the first name + return StringUtil::getAtInSplit(UnownedStringSlice(opt.names), ',', 0); + } + } + + return defaultName; +} + + +/* static */UnownedStringSlice NameValueUtil::findDescription(const ConstArrayView& opts, ValueInt value, const UnownedStringSlice& defaultDescription) +{ + for (const auto& opt : opts) + { + if (opt.value == value && opt.description) + { + return UnownedStringSlice(opt.description); + } + } + + return defaultDescription; +} + +/* static */ void NameValueUtil::appendNames(NameKind kind, const ConstArrayView& opts, List& out) +{ + SLANG_UNUSED(kind); + for (auto& opt : opts) + { + out.add(UnownedStringSlice(opt.name)); + } +} + +static void _appendNames(NameValueUtil::NameKind kind, const char* names, List& out) +{ + if (kind == NameValueUtil::NameKind::All) + { + StringUtil::appendSplit(UnownedStringSlice(names), ',', out); + } + else + { + out.add(StringUtil::getAtInSplit(UnownedStringSlice(names), ',', 0)); + } +} + +/* static */ void NameValueUtil::appendNames(NameKind kind, const ConstArrayView& opts, List& out) +{ + for (auto& opt : opts) + { + _appendNames(kind, opt.names, out); + } +} + +/* static */ void NameValueUtil::appendNames(NameKind kind, const ConstArrayView& opts, List& out) +{ + for (auto& opt : opts) + { + _appendNames(kind, opt.names, out); + } +} + +/* static */ List NameValueUtil::getNames(NameKind kind, const ConstArrayView& opts) +{ + List names; + appendNames(kind, opts, names); + return names; +} + +/* static */ List NameValueUtil::getNames(NameKind kind, const ConstArrayView& opts) +{ + List names; + appendNames(kind, opts, names); + return names; +} + +/* static */ List NameValueUtil::getNames(NameKind kind, const ConstArrayView& opts) +{ + List names; + appendNames(kind, opts, names); + return names; +} + +} // namespace Slang + + -- cgit v1.2.3