-
-
Notifications
You must be signed in to change notification settings - Fork 542
refactor(cpn): introduce BoundedString<N> for ModelData string fields #7438
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
Open
raphaelcoeffic
wants to merge
8
commits into
main
Choose a base branch
from
companion-bounded-string
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c29f03f
feat(cpn): add BoundedString<N> typed string for data-model fields
raphaelcoeffic 5668173
refactor(cpn): migrate ModelData::filename to BoundedString<N>
raphaelcoeffic c0ba493
test(cpn): restore companion gtests harness
raphaelcoeffic dc6ac88
refactor(cpn): migrate ModelData name/labels/semver to BoundedString<N>
raphaelcoeffic fb4ca3c
test(cpn): add model YAML round-trip corpus test for BoundedString fi…
raphaelcoeffic e7a5516
ci(cpn): run companion gtests in CI
raphaelcoeffic 0cd584e
ci(cpn): fix native build path for companion gtests step
raphaelcoeffic 90f0583
fix(cpn): guard BoundedString const char* comparison against null
raphaelcoeffic 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,92 @@ | ||
| /* | ||
| * Copyright (C) EdgeTX | ||
| * | ||
| * Based on code named | ||
| * opentx - https://github.com/opentx/opentx | ||
| * th9x - http://code.google.com/p/th9x | ||
| * er9x - http://code.google.com/p/er9x | ||
| * gruvin9x - http://code.google.com/p/gruvin9x | ||
| * | ||
| * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html | ||
| * | ||
| * This program is free software; you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License version 2 as | ||
| * published by the Free Software Foundation. | ||
| * | ||
| * 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. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <string> | ||
| #include <string_view> | ||
|
|
||
| #include <QString> | ||
|
|
||
| // A self-limiting string for Companion's data-model fields. | ||
| // | ||
| // N is the maximum number of bytes the value may hold, EXCLUDING the implicit | ||
| // NUL of the legacy `char[]` fields it replaces. So a legacy `char foo[16+1]` | ||
| // (16 usable chars) maps to `BoundedString<16>`. | ||
| // | ||
| // Assignment always truncates to N bytes, mirroring the byte-level truncation | ||
| // the YAML char-array reader does today (yaml_ops.h: `str.copy(value, N-1)`). | ||
| // Truncation is byte-based on purpose: it must match the radio firmware (and the | ||
| // legacy code path) exactly, including the case where a multi-byte UTF-8 | ||
| // sequence is split at the boundary. Do not "improve" it to be codepoint-aware. | ||
| template <size_t N> | ||
| class BoundedString | ||
| { | ||
| static_assert(N > 0, "BoundedString capacity must be greater than zero"); | ||
|
|
||
| std::string s_; | ||
|
|
||
| public: | ||
| static constexpr size_t capacity() { return N; } | ||
|
|
||
| BoundedString() = default; | ||
| BoundedString(const BoundedString&) = default; | ||
| BoundedString& operator=(const BoundedString&) = default; | ||
|
|
||
| BoundedString(std::string_view v) { assign(v); } | ||
| BoundedString(const char* v) { assign(v ? std::string_view(v) : std::string_view()); } | ||
| BoundedString(const QString& v) { assign(v); } | ||
|
|
||
| BoundedString& operator=(std::string_view v) { assign(v); return *this; } | ||
| BoundedString& operator=(const char* v) { assign(v ? std::string_view(v) : std::string_view()); return *this; } | ||
| BoundedString& operator=(const QString& v) { assign(v); return *this; } | ||
|
|
||
| void assign(std::string_view v) { s_.assign(v.substr(0, N)); } | ||
| // NOTE: QString is serialised as UTF-8 here, matching the YAML layer. Some | ||
| // legacy UI call sites stored Latin-1 via QString::toLatin1(); Phase 1 must | ||
| // confirm the encoding at each migrated boundary before relying on this. | ||
| void assign(const QString& v) { assign(v.toStdString()); } | ||
|
|
||
| void clear() { s_.clear(); } | ||
| bool empty() const { return s_.empty(); } | ||
| size_t size() const { return s_.size(); } | ||
| size_t length() const { return s_.size(); } | ||
|
|
||
| const std::string& str() const { return s_; } | ||
| const char* c_str() const { return s_.c_str(); } | ||
| QString toQString() const { return QString::fromStdString(s_); } | ||
|
|
||
| // Implicit conversion so a migrated field still drops into APIs taking a | ||
| // std::string (and, via QString's std::string ctor, much of the Qt UI). | ||
| operator const std::string&() const { return s_; } | ||
|
|
||
| // Explicit comparison overloads (std::string's own operator== is a | ||
| // non-member template, so the implicit conversion above can't drive it). | ||
| // The const char* overload binds legacy char[]/literals via array-to-pointer | ||
| // (a standard conversion), so it wins cleanly over the others rather than | ||
| // tying with them and producing an ambiguity. | ||
| bool operator==(const BoundedString& o) const { return s_ == o.s_; } | ||
| bool operator!=(const BoundedString& o) const { return s_ != o.s_; } | ||
| bool operator==(const std::string& o) const { return s_ == o; } | ||
| bool operator!=(const std::string& o) const { return s_ != o; } | ||
| bool operator==(const char* o) const { return s_ == o; } | ||
| bool operator!=(const char* o) const { return s_ != o; } | ||
| }; | ||
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
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.