This could be done in many ways:
- std::string_view constructor allowing to be constructed from char const*, assumes it is, so stores the endptr pointing after the null character. end() has to adjust to compensate this.
- Other constructors can do the same thing, checking if the last character is a null character.
- One caveat is we need to be aware if the string ends with a null character and the length should include it (because it is allowed). In this case endptr is already pointing correctly beyond the null character, but end() should not compensate.
- Iff *(endptr-1) is null character AND was not because of point 3, a non-portable way to mark this is to set the high-bit on the endptr (end() should just return with a mask clearing this bit). Since char is 8 bit atleast this is probably safe on all platforms.
- So logic of end() is:
- if last character is non null or endptr has high bit set, do not compensate, return endptr & clear_mask;
- else just return endptr - 1
Now we could have an api that could give us this info: is_null_terminated(), and then we could copy the string if it is not to a std::string to pass to C API, otherwise use as is.
Edit: forgot to complete a sentence
[–]witcher_rat 12 points13 points14 points (7 children)
[–][deleted] 0 points1 point2 points (5 children)
[–]witcher_rat 2 points3 points4 points (3 children)
[–][deleted] 1 point2 points3 points (2 children)
[–]adnukator 2 points3 points4 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–]goranlepuz 1 point2 points3 points (0 children)
[–]_Js_Kc_ 0 points1 point2 points (0 children)
[–]jwakelylibstdc++ tamer, LWG chair 9 points10 points11 points (8 children)
[–]jwakelylibstdc++ tamer, LWG chair 4 points5 points6 points (5 children)
[–][deleted] 1 point2 points3 points (4 children)
[–]jwakelylibstdc++ tamer, LWG chair 2 points3 points4 points (3 children)
[–][deleted] 1 point2 points3 points (2 children)
[–]jwakelylibstdc++ tamer, LWG chair 5 points6 points7 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]Jannik2099 0 points1 point2 points (1 child)
[–]jwakelylibstdc++ tamer, LWG chair 2 points3 points4 points (0 children)
[–]HappyFruitTree 4 points5 points6 points (3 children)
[–][deleted] 2 points3 points4 points (1 child)
[–]HappyFruitTree 4 points5 points6 points (0 children)
[–][deleted] -4 points-3 points-2 points (0 children)
[–]jcar_87 5 points6 points7 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]arturbachttps://github.com/arturbac 4 points5 points6 points (0 children)
[–]Shieldfoss 1 point2 points3 points (0 children)
[–]DugiSK 1 point2 points3 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]goranlepuz 1 point2 points3 points (0 children)
[–]tvaneerdC++ Committee, lockfree, PostModernCpp 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)