-
Notifications
You must be signed in to change notification settings - Fork 16
Add WGPUStringView string utilities (wgpu_string.h) #248
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| /***************************************************************************** | ||
| * weBIGeo | ||
| * Copyright (C) 2026 Matthias Huerbe | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| *****************************************************************************/ | ||
|
|
||
| #include <catch2/catch_test_macros.hpp> | ||
| #include <cstring> | ||
| #include <string> | ||
| #include <string_view> | ||
| #include <webgpu/base/wgpu_string.h> | ||
|
|
||
| using namespace webgpu; | ||
|
|
||
| static_assert(""_wsv.length == 0); | ||
| static_assert("hello"_wsv.length == 5); | ||
| static_assert("a\0b"_wsv.length == 3); | ||
|
|
||
| TEST_CASE("wsv literal - construct", "[wgpu_string]") | ||
| { | ||
| auto str = "hello"_wsv; | ||
| CHECK(str.length == 5); | ||
| CHECK(std::memcmp(str.data, "hello", 5) == 0); | ||
| } | ||
|
|
||
| TEST_CASE("wsv literal - empty", "[wgpu_string]") | ||
| { | ||
| CHECK(""_wsv.length == 0); | ||
| } | ||
|
|
||
| TEST_CASE("wsv literal - keeps embedded NUL", "[wgpu_string]") | ||
| { | ||
| auto str = "a\0b"_wsv; | ||
| CHECK(str.length == 3); | ||
| CHECK(std::memcmp(str.data, "a\0b", 3) == 0); | ||
| } | ||
|
|
||
| TEST_CASE("wsv() - from string_view / string / const char*", "[wgpu_string]") | ||
| { | ||
| std::string_view sv = "hello"; | ||
| CHECK(wsv(sv) == "hello"_wsv); | ||
|
|
||
| std::string str = "world"; | ||
| CHECK(wsv(str) == "world"_wsv); | ||
|
|
||
| CHECK(wsv("literal") == "literal"_wsv); | ||
|
|
||
| CHECK(wsv(std::string_view("hello", 3)).length == 3); | ||
| CHECK(wsv(std::string_view("hello", 3)) == "hel"_wsv); | ||
| } | ||
|
|
||
| TEST_CASE("wsv() - empty", "[wgpu_string]") | ||
| { | ||
| CHECK(wsv(std::string_view {}).length == 0); | ||
| CHECK(wsv("") == ""_wsv); | ||
| } | ||
|
|
||
| TEST_CASE("wsv_length - explicit length returned as-is", "[wgpu_string]") | ||
| { | ||
| CHECK(wsv_length(WGPUStringView { "hello", 5 }) == 5); | ||
| CHECK(wsv_length(WGPUStringView { "hello", 2 }) == 2); | ||
| CHECK(wsv_length(WGPUStringView { "abc", 0 }) == 0); | ||
| } | ||
|
|
||
| TEST_CASE("wsv_length - WGPU_STRLEN sentinel uses strlen", "[wgpu_string]") | ||
| { | ||
| CHECK(wsv_length(WGPUStringView { "hello", WGPU_STRLEN }) == 5); | ||
| CHECK(wsv_length(WGPUStringView { "", WGPU_STRLEN }) == 0); | ||
| } | ||
|
|
||
| TEST_CASE("wsv_length - sentinel with null data is 0", "[wgpu_string]") | ||
| { | ||
| CHECK(wsv_length(WGPUStringView { nullptr, WGPU_STRLEN }) == 0); | ||
| } | ||
|
|
||
| TEST_CASE("operator== - equal content", "[wgpu_string]") | ||
| { | ||
| CHECK("hello"_wsv == WGPUStringView { "hello", 5 }); | ||
| CHECK("hello"_wsv == WGPUStringView { "hello", WGPU_STRLEN }); | ||
| } | ||
|
|
||
| TEST_CASE("operator== - different content, same length", "[wgpu_string]") | ||
| { | ||
| CHECK_FALSE("hello"_wsv == "world"_wsv); | ||
| CHECK("hello"_wsv != "world"_wsv); | ||
| } | ||
|
|
||
| TEST_CASE("operator== - different length", "[wgpu_string]") | ||
| { | ||
| CHECK_FALSE("hello"_wsv == "hell"_wsv); | ||
| CHECK_FALSE("hello"_wsv == "hello!"_wsv); | ||
| } | ||
|
|
||
| TEST_CASE("operator== - empty views", "[wgpu_string]") | ||
| { | ||
| CHECK(""_wsv == ""_wsv); | ||
| CHECK(WGPUStringView { nullptr, WGPU_STRLEN } == ""_wsv); | ||
| CHECK_FALSE(""_wsv == "x"_wsv); | ||
| } | ||
|
|
||
| TEST_CASE("operator== - NUL-safe, not strlen-based", "[wgpu_string]") | ||
| { | ||
| CHECK("a\0b"_wsv == WGPUStringView { "a\0b", 3 }); | ||
| CHECK_FALSE("a\0b"_wsv == WGPUStringView { "a\0c", 3 }); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| /***************************************************************************** | ||
| * weBIGeo | ||
| * Copyright (C) 2026 Matthias Huerbe | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| *****************************************************************************/ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <webgpu/webgpu.h> | ||
| #include <cstring> // std::strlen & std::memcmp | ||
| #include <string_view> | ||
|
|
||
| namespace webgpu | ||
| { | ||
|
|
||
| // utility to create a webgpu string view from the string literal | ||
| inline constexpr WGPUStringView operator""_wsv(const char* s, size_t l) noexcept | ||
| { | ||
| return { s, l }; | ||
| } | ||
|
|
||
| // utility to create a webgpu string view from std::string, std::string_view, const char* | ||
| inline constexpr WGPUStringView wsv(std::string_view s) noexcept | ||
| { | ||
| return { s.data(), s.size() }; | ||
| } | ||
|
|
||
| // length of a possibly WGPU_STRLEN-sentinel string view | ||
| inline constexpr size_t wsv_length(WGPUStringView s) noexcept | ||
| { | ||
| return (s.length == WGPU_STRLEN) ? (s.data ? std::strlen(s.data) : 0) : s.length; | ||
| } | ||
|
|
||
| // content equality of two string views | ||
| inline constexpr bool operator==(WGPUStringView a, WGPUStringView b) noexcept | ||
| { | ||
| size_t na = wsv_length(a), nb = wsv_length(b); | ||
| return na == nb && (na == 0 || std::memcmp(a.data, b.data, na) == 0); | ||
| } | ||
|
|
||
| } // namespace webgpu | ||
|
|
||
| // usable by include | ||
| using webgpu::operator""_wsv; | ||
| using webgpu::operator==; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The name of the method can be improved. If it is used in the code base, people understand it, but nobody will ever find it on its own.
to_wsv()or something