Commit 51847a21 authored by Xavier Thompson's avatar Xavier Thompson

Implement GIL-free string formatting

parent 21f67d41
from stdlib.string cimport Str
cdef extern from * nogil:
"""
#include <fmt/format.h>
#include <string_view>
template <typename T, typename S>
struct Cy_Str_Adapter {
using type = T;
constexpr static auto adapt(T t) { return t; }
};
template <typename T>
struct Cy_Str_Adapter<T*, T> {
using type = std::string_view;
static std::string_view adapt(T* t) {
return t->operator std::string_view();
}
};
template <typename S, typename... T>
S * format(fmt::format_string<typename Cy_Str_Adapter<T, S>::type...> fmt, T... args) {
S *s = new S();
s->_str = fmt::format(fmt, Cy_Str_Adapter<T, S>::adapt(args)...);
return s;
}
"""
Str format "format<Cy_Str>" (const char *, ...) except +
......@@ -22,7 +22,7 @@ cdef extern from "<cctype>" namespace "std" nogil:
cdef cypclass Str:
cdef cypclass Str "Cy_Str":
string _str
__init__(self, const char *s):
......@@ -137,6 +137,9 @@ cdef cypclass Str:
int __int__(self) except +:
return stoi(self._str)
string_view __string_view__(self):
return string_view(self._str.data(), self._str.size())
@staticmethod
const char * to_c_str(Str s):
if s is NULL:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment