mirror of
https://github.com/ChaiScript/ChaiScript.git
synced 2025-12-06 16:57:04 +08:00
move towards string_view
This commit is contained in:
parent
9596e15049
commit
d115dbfd79
@ -21,6 +21,7 @@ static_assert(_MSC_FULL_VER >= 190024210, "Visual C++ 2015 Update 3 or later req
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <string_view>
|
||||||
|
|
||||||
#if defined( _LIBCPP_VERSION )
|
#if defined( _LIBCPP_VERSION )
|
||||||
#define CHAISCRIPT_LIBCPP
|
#define CHAISCRIPT_LIBCPP
|
||||||
@ -152,10 +153,10 @@ namespace chaiscript {
|
|||||||
|
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
constexpr auto parse_num(const char *t_str) noexcept -> typename std::enable_if<std::is_integral<T>::value, T>::type
|
constexpr auto parse_num(const std::string_view &t_str) noexcept -> typename std::enable_if<std::is_integral<T>::value, T>::type
|
||||||
{
|
{
|
||||||
T t = 0;
|
T t = 0;
|
||||||
for (char c = *t_str; (c = *t_str) != 0; ++t_str) {
|
for (const auto c : t_str) {
|
||||||
if (c < '0' || c > '9') {
|
if (c < '0' || c > '9') {
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
@ -167,7 +168,7 @@ namespace chaiscript {
|
|||||||
|
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
auto parse_num(const char *t_str) noexcept -> typename std::enable_if<!std::is_integral<T>::value, T>::type
|
auto parse_num(const std::string_view &t_str) noexcept -> typename std::enable_if<!std::is_integral<T>::value, T>::type
|
||||||
{
|
{
|
||||||
T t = 0;
|
T t = 0;
|
||||||
T base = 0;
|
T base = 0;
|
||||||
@ -183,8 +184,7 @@ namespace chaiscript {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
for(; *t_str != '\0'; ++t_str) {
|
for (const auto c : t_str) {
|
||||||
char c = *t_str;
|
|
||||||
if (c == '.') {
|
if (c == '.') {
|
||||||
decimal_place = 10;
|
decimal_place = 10;
|
||||||
} else if (c == 'e' || c == 'E') {
|
} else if (c == 'e' || c == 'E') {
|
||||||
@ -210,11 +210,6 @@ namespace chaiscript {
|
|||||||
return final_value(t, base, exponent, neg_exponent);
|
return final_value(t, base, exponent, neg_exponent);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
T parse_num(const std::string &t_str) noexcept
|
|
||||||
{
|
|
||||||
return parse_num<T>(t_str.c_str());
|
|
||||||
}
|
|
||||||
|
|
||||||
enum class Options
|
enum class Options
|
||||||
{
|
{
|
||||||
|
|||||||
@ -19,6 +19,7 @@
|
|||||||
#include <set>
|
#include <set>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <string_view>
|
||||||
#include <typeinfo>
|
#include <typeinfo>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
@ -180,9 +181,9 @@ namespace chaiscript
|
|||||||
|
|
||||||
|
|
||||||
//Add a bit of ChaiScript to eval during module implementation
|
//Add a bit of ChaiScript to eval during module implementation
|
||||||
Module &eval(const std::string &str)
|
Module &eval(std::string str)
|
||||||
{
|
{
|
||||||
m_evals.push_back(str);
|
m_evals.push_back(std::move(str));
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -196,7 +197,7 @@ namespace chaiscript
|
|||||||
apply_globals(m_globals.begin(), m_globals.end(), t_engine);
|
apply_globals(m_globals.begin(), m_globals.end(), t_engine);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool has_function(const Proxy_Function &new_f, const std::string &name) noexcept
|
bool has_function(const Proxy_Function &new_f, const std::string_view &name) noexcept
|
||||||
{
|
{
|
||||||
return std::any_of(m_funcs.begin(), m_funcs.end(),
|
return std::any_of(m_funcs.begin(), m_funcs.end(),
|
||||||
[&](const std::pair<Proxy_Function, std::string> &existing_f) {
|
[&](const std::pair<Proxy_Function, std::string> &existing_f) {
|
||||||
@ -438,7 +439,7 @@ namespace chaiscript
|
|||||||
{
|
{
|
||||||
|
|
||||||
public:
|
public:
|
||||||
typedef std::map<std::string, chaiscript::Type_Info> Type_Name_Map;
|
typedef std::map<std::string, chaiscript::Type_Info, std::less<>> Type_Name_Map;
|
||||||
typedef std::vector<std::pair<std::string, Boxed_Value>> Scope;
|
typedef std::vector<std::pair<std::string, Boxed_Value>> Scope;
|
||||||
typedef Stack_Holder::StackData StackData;
|
typedef Stack_Holder::StackData StackData;
|
||||||
|
|
||||||
@ -447,7 +448,7 @@ namespace chaiscript
|
|||||||
std::vector<std::pair<std::string, std::shared_ptr<std::vector<Proxy_Function>>>> m_functions;
|
std::vector<std::pair<std::string, std::shared_ptr<std::vector<Proxy_Function>>>> m_functions;
|
||||||
std::vector<std::pair<std::string, Proxy_Function>> m_function_objects;
|
std::vector<std::pair<std::string, Proxy_Function>> m_function_objects;
|
||||||
std::vector<std::pair<std::string, Boxed_Value>> m_boxed_functions;
|
std::vector<std::pair<std::string, Boxed_Value>> m_boxed_functions;
|
||||||
std::map<std::string, Boxed_Value> m_global_objects;
|
std::map<std::string, Boxed_Value, std::less<>> m_global_objects;
|
||||||
Type_Name_Map m_types;
|
Type_Name_Map m_types;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -654,7 +655,7 @@ namespace chaiscript
|
|||||||
/// Searches the current stack for an object of the given name
|
/// Searches the current stack for an object of the given name
|
||||||
/// includes a special overload for the _ place holder object to
|
/// includes a special overload for the _ place holder object to
|
||||||
/// ensure that it is always in scope.
|
/// ensure that it is always in scope.
|
||||||
Boxed_Value get_object(const std::string &name, std::atomic_uint_fast32_t &t_loc, Stack_Holder &t_holder) const
|
Boxed_Value get_object(const std::string_view &name, std::atomic_uint_fast32_t &t_loc, Stack_Holder &t_holder) const
|
||||||
{
|
{
|
||||||
enum class Loc : uint_fast32_t {
|
enum class Loc : uint_fast32_t {
|
||||||
located = 0x80000000,
|
located = 0x80000000,
|
||||||
@ -719,7 +720,7 @@ namespace chaiscript
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the type info for a named type
|
/// Returns the type info for a named type
|
||||||
Type_Info get_type(const std::string &name, bool t_throw = true) const
|
Type_Info get_type(const std::string_view &name, bool t_throw = true) const
|
||||||
{
|
{
|
||||||
chaiscript::detail::threading::shared_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
|
chaiscript::detail::threading::shared_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
|
||||||
|
|
||||||
@ -776,7 +777,7 @@ namespace chaiscript
|
|||||||
|
|
||||||
|
|
||||||
/// Return a function by name
|
/// Return a function by name
|
||||||
std::pair<size_t, std::shared_ptr<std::vector< Proxy_Function>>> get_function(const std::string &t_name, const size_t t_hint) const
|
std::pair<size_t, std::shared_ptr<std::vector< Proxy_Function>>> get_function(const std::string_view &t_name, const size_t t_hint) const
|
||||||
{
|
{
|
||||||
chaiscript::detail::threading::shared_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
|
chaiscript::detail::threading::shared_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
|
||||||
|
|
||||||
@ -804,7 +805,7 @@ namespace chaiscript
|
|||||||
/// \returns a function object (Boxed_Value wrapper) if it exists
|
/// \returns a function object (Boxed_Value wrapper) if it exists
|
||||||
/// \throws std::range_error if it does not
|
/// \throws std::range_error if it does not
|
||||||
/// \warn does not obtain a mutex lock. \sa get_function_object for public version
|
/// \warn does not obtain a mutex lock. \sa get_function_object for public version
|
||||||
std::pair<size_t, Boxed_Value> get_function_object_int(const std::string &t_name, const size_t t_hint) const
|
std::pair<size_t, Boxed_Value> get_function_object_int(const std::string_view &t_name, const size_t t_hint) const
|
||||||
{
|
{
|
||||||
const auto &funs = get_boxed_functions_int();
|
const auto &funs = get_boxed_functions_int();
|
||||||
|
|
||||||
@ -814,13 +815,13 @@ namespace chaiscript
|
|||||||
{
|
{
|
||||||
return std::make_pair(std::distance(funs.begin(), itr), itr->second);
|
return std::make_pair(std::distance(funs.begin(), itr), itr->second);
|
||||||
} else {
|
} else {
|
||||||
throw std::range_error("Object not found: " + t_name);
|
throw std::range_error("Object not found: " + std::string(t_name));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Return true if a function exists
|
/// Return true if a function exists
|
||||||
bool function_exists(const std::string &name) const
|
bool function_exists(const std::string_view &name) const
|
||||||
{
|
{
|
||||||
chaiscript::detail::threading::shared_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
|
chaiscript::detail::threading::shared_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
|
||||||
|
|
||||||
@ -1076,7 +1077,7 @@ namespace chaiscript
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
Boxed_Value call_function(const std::string &t_name, std::atomic_uint_fast32_t &t_loc, const std::vector<Boxed_Value> ¶ms,
|
Boxed_Value call_function(const std::string_view &t_name, std::atomic_uint_fast32_t &t_loc, const std::vector<Boxed_Value> ¶ms,
|
||||||
const Type_Conversions_State &t_conversions) const
|
const Type_Conversions_State &t_conversions) const
|
||||||
{
|
{
|
||||||
uint_fast32_t loc = t_loc;
|
uint_fast32_t loc = t_loc;
|
||||||
@ -1166,7 +1167,7 @@ namespace chaiscript
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// return true if the Boxed_Value matches the registered type by name
|
/// return true if the Boxed_Value matches the registered type by name
|
||||||
bool is_type(const Boxed_Value &r, const std::string &user_typename) const noexcept
|
bool is_type(const Boxed_Value &r, const std::string_view &user_typename) const noexcept
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
if (get_type(user_typename).bare_equal(r.get_type_info()))
|
if (get_type(user_typename).bare_equal(r.get_type_info()))
|
||||||
@ -1445,7 +1446,7 @@ namespace chaiscript
|
|||||||
{
|
{
|
||||||
return std::find_if(t_c.begin(), t_c.end(),
|
return std::find_if(t_c.begin(), t_c.end(),
|
||||||
[&t_key](const typename Container::value_type &o) {
|
[&t_key](const typename Container::value_type &o) {
|
||||||
return o.first == t_key;
|
return std::equal(o.first.begin(), o.first.end(), t_key.begin(), t_key.end());
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1555,7 +1556,7 @@ namespace chaiscript
|
|||||||
return m_engine.get().add_object(t_name, std::move(obj), m_stack_holder.get());
|
return m_engine.get().add_object(t_name, std::move(obj), m_stack_holder.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
Boxed_Value get_object(const std::string &t_name, std::atomic_uint_fast32_t &t_loc) const {
|
Boxed_Value get_object(const std::string_view &t_name, std::atomic_uint_fast32_t &t_loc) const {
|
||||||
return m_engine.get().get_object(t_name, t_loc, m_stack_holder.get());
|
return m_engine.get().get_object(t_name, t_loc, m_stack_holder.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user