leftibot 46cf4cdef0
Fix #458: Cannot assign string to function that expects std::string_view (#697)
* Fix #458: Support std::string_view interop with ChaiScript strings

Register std::string_view as the "string_view" user type with a built-in
implicit conversion from std::string, so a ChaiScript string can be passed
directly to a C++ function taking std::string_view. Adds the reverse
explicit conversion via string(sv) / to_string(sv), plus basic queries
(size, length, empty, data) and comparison operators on string_view.
String-style methods that take size_t (substr, find, ...) are intentionally
not duplicated on string_view: with the implicit conversion in place they
would create dispatch ambiguity for calls like string.substr(int, int).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* Address review: expand string_view binding with substr/find/[]

Adds substr, find/rfind/find_first_of/find_last_of/find_first_not_of/
find_last_not_of, starts_with/ends_with, and operator[] to string_view_type,
mirroring string_type's search/substring surface so script authors can
traverse a buffer through string_view without allocating.

Sharing method names with string_type creates an ambiguity in
dispatch_with_conversions when the script call needs arithmetic conversion
(e.g. myString.substr(int, int)): both string::substr and string_view::substr
match-except-for-arithmetic, and the existing const/non-const tiebreaker
doesn't apply. Extend dispatch_with_conversions to prefer the candidate
whose first/receiver parameter type exactly matches the actual receiver,
mirroring the deprioritization already in dispatch(). With this in place,
myString.substr(1, 2) still resolves to string::substr while
mySV.substr(1, 2) resolves to string_view::substr (returning a string_view).

Updates the doc comment on string_view_type and adds a compiled test that
locks in both dispatch directions plus the new search/substring methods.

Requested by @lefticus in PR #697 review.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: leftibot <leftibot@users.noreply.github.com>
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-02 14:45:29 -06:00
..
any.hpp change .clang-format and reformat code with clang-format 11 2021-05-24 10:44:15 +02:00
bad_boxed_cast.hpp change .clang-format and reformat code with clang-format 11 2021-05-24 10:44:15 +02:00
bind_first.hpp change .clang-format and reformat code with clang-format 11 2021-05-24 10:44:15 +02:00
bootstrap_stl.hpp Fix #458: Cannot assign string to function that expects std::string_view (#697) 2026-05-02 14:45:29 -06:00
bootstrap.hpp Enable warnings as errors (#694) 2026-04-28 20:11:15 -06:00
boxed_cast_helper.hpp change .clang-format and reformat code with clang-format 11 2021-05-24 10:44:15 +02:00
boxed_cast.hpp change .clang-format and reformat code with clang-format 11 2021-05-24 10:44:15 +02:00
boxed_number.hpp Enable warnings as errors (#694) 2026-04-28 20:11:15 -06:00
boxed_value.hpp Fix #17: Add const variables in ChaiScript (#643) 2026-04-10 22:12:13 -06:00
callable_traits.hpp change .clang-format and reformat code with clang-format 11 2021-05-24 10:44:15 +02:00
dispatchkit.hpp Fix #633: [Bug] Stack-overflow due to infinite recursion in user-defined operator (string interpolation) (#700) 2026-05-02 11:38:01 -06:00
dynamic_object_detail.hpp Fix #690: Apply clang-format consistently with CI (#691) 2026-04-17 18:45:41 -06:00
dynamic_object.hpp Fix #690: Apply clang-format consistently with CI (#691) 2026-04-17 18:45:41 -06:00
exception_specification.hpp change .clang-format and reformat code with clang-format 11 2021-05-24 10:44:15 +02:00
function_call_detail.hpp Fix #493: Allow shared_ptr arguments through std::function callbacks (#696) 2026-05-02 10:36:18 -06:00
function_call.hpp change .clang-format and reformat code with clang-format 11 2021-05-24 10:44:15 +02:00
function_params.hpp Enable warnings as errors (#694) 2026-04-28 20:11:15 -06:00
function_signature.hpp Fix #690: Apply clang-format consistently with CI (#691) 2026-04-17 18:45:41 -06:00
handle_return.hpp Fix #690: Apply clang-format consistently with CI (#691) 2026-04-17 18:45:41 -06:00
operators.hpp Fix #601: Allow operator functions to accept any type with an add() method (#686) 2026-04-13 20:20:11 -06:00
proxy_constructors.hpp change .clang-format and reformat code with clang-format 11 2021-05-24 10:44:15 +02:00
proxy_functions_detail.hpp 🎨 Committing clang-format changes 2026-04-29 02:22:59 +00:00
proxy_functions.hpp Fix #458: Cannot assign string to function that expects std::string_view (#697) 2026-05-02 14:45:29 -06:00
register_function.hpp Fix #612: Allow fun<Signature>(&overloaded) for overload disambiguation (#701) 2026-05-02 14:43:11 -06:00
type_conversions.hpp Fix #374: Support nested vector type conversions (#692) 2026-04-18 12:57:20 -06:00
type_info.hpp Fix #17: Add const variables in ChaiScript (#643) 2026-04-10 22:12:13 -06:00