diff options
| author | jsmall-nvidia <jsmall@nvidia.com> | 2023-04-29 09:24:26 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-04-29 09:24:26 -0400 |
| commit | 19c0866b050a022406867aa650302f4efbf8e010 (patch) | |
| tree | f5ed4e1f5d27865518daf81c7e861b4908186b23 /source/core/slang-name-value.cpp | |
| parent | c571bcb025009f9c662e8d631fa49dbfed560287 (diff) | |
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.
Diffstat (limited to 'source/core/slang-name-value.cpp')
| -rw-r--r-- | source/core/slang-name-value.cpp | 163 |
1 files changed, 163 insertions, 0 deletions
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<NameValue>& 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<NamesValue>& 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<NamesDescriptionValue>& 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<NameValue>& 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<NamesValue>& 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<NamesDescriptionValue>& 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<NamesDescriptionValue>& 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<NameValue>& opts, List<UnownedStringSlice>& out) +{ + SLANG_UNUSED(kind); + for (auto& opt : opts) + { + out.add(UnownedStringSlice(opt.name)); + } +} + +static void _appendNames(NameValueUtil::NameKind kind, const char* names, List<UnownedStringSlice>& 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<NamesValue>& opts, List<UnownedStringSlice>& out) +{ + for (auto& opt : opts) + { + _appendNames(kind, opt.names, out); + } +} + +/* static */ void NameValueUtil::appendNames(NameKind kind, const ConstArrayView<NamesDescriptionValue>& opts, List<UnownedStringSlice>& out) +{ + for (auto& opt : opts) + { + _appendNames(kind, opt.names, out); + } +} + +/* static */ List<UnownedStringSlice> NameValueUtil::getNames(NameKind kind, const ConstArrayView<NameValue>& opts) +{ + List<UnownedStringSlice> names; + appendNames(kind, opts, names); + return names; +} + +/* static */ List<UnownedStringSlice> NameValueUtil::getNames(NameKind kind, const ConstArrayView<NamesValue>& opts) +{ + List<UnownedStringSlice> names; + appendNames(kind, opts, names); + return names; +} + +/* static */ List<UnownedStringSlice> NameValueUtil::getNames(NameKind kind, const ConstArrayView<NamesDescriptionValue>& opts) +{ + List<UnownedStringSlice> names; + appendNames(kind, opts, names); + return names; +} + +} // namespace Slang + + |
