Add data and start_offset methods for StringView#2090
Add data and start_offset methods for StringView#2090peter-jerry-ye merged 1 commit intomoonbitlang:mainfrom
data and start_offset methods for StringView#2090Conversation
Documentation for StringView::data() could be more specific about lifetime implicationsCategory /// Returns the original string that is being viewed.
/// Note: This returns the entire underlying string, not just the viewed portion.
/// The returned string remains valid as long as the original string exists.
pub fn data(self : View) -> String {
self.str
}```
**Reasoning**
For APIs dealing with string views and raw data access, it's important to be explicit about lifetime and ownership semantics to prevent misuse
</details>
<details>
<summary> The test case for start_offset() uses a string literal in inspect! that could be misleading </summary>
**Category**
Correctness
**Code Snippet**
test "stringview start_offset" {
let str = "Hello🤣🤣🤣"
let view = str.view(start_offset=1, end_offset=7)
inspect!(view.start_offset(), content="1")
}
**Recommendation**
Use a numeric literal instead of string:
```moonbit
inspect!(view.start_offset(), content=1)Reasoning The test case for data() could be more comprehensiveCategory test "stringview data" {
let str = "Hello🤣🤣🤣"
let view = str.view(start_offset=1, end_offset=7)
inspect!(view.data(), content="Hello🤣🤣🤣")
// Verify that it returns the full string
assert(view.data().length() > view.length())
// Verify identity
assert(view.data() === str)
}```
**Reasoning**
The current test only verifies basic functionality. Additional assertions would help ensure the method behaves correctly with respect to string identity and full string access
</details> |
Pull Request Test Coverage Report for Build 7073Details
💛 - Coveralls |
|
Is there a reason that you are using |
|
The reason for using
|
|
Although I don't think it's a bad idea to add these functions, I don't think your reasons hold. You can pass the |
|
Yes, the C prototype of |
|
Solely for your case, I think you don't even need the encoder or |
|
Right, in this func, it can be handled better as you suggested. |
|
However, in other cases, when the amount of data contained in |
|
I think it's fine to expose them. @Yu-zh |
Then it looks good to me |
7b6f698 to
5963853
Compare
The
StringViewcurrently lacks methods to obtain raw data and starting offset, which hinders seamless switching betweenStringViewandString.Here is an example: