Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
stdlib
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
cython-plus
stdlib
Commits
e1949632
Commit
e1949632
authored
Aug 31, 2022
by
Xavier Thompson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add string formatting
parent
f68a8341
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
41 additions
and
0 deletions
+41
-0
format.pxd
format.pxd
+41
-0
No files found.
format.pxd
0 → 100644
View file @
e1949632
# distutils: libraries = fmt
from
.string
cimport
Str
# Description:
# This defines a format() function that accepts Str arguments for formatting
# and returns a Str. The format string itself must still be a string literal.
#
# Explanation:
# Cy_Str is a template parameter in this C++ code only because this code is
# inserted before the definition of Cy_Str (C++ name of cypclass Str) in the
# generated C++ output. The actual Cy_Str type is injected at each call site
# of the format function. This is achieved by explicitly setting the C++ name
# of the format function to `format<Cy_Str>`.
cdef
extern
from
*
nogil
:
"""
#include <fmt/format.h>
#include <string_view>
template <typename T, typename Cy_Str>
struct Cy_Str_Adapter {
constexpr static auto adapt(T t) { return t; }
};
template <typename Cy_Str>
struct Cy_Str_Adapter<Cy_Str*, Cy_Str> {
static std::string_view adapt(Cy_Str* s) {
return s->operator std::string_view();
}
};
template <typename Cy_Str, typename Fmt, typename... T>
Cy_Str * format(const Fmt & fmt, T... args) {
Cy_Str *s = new Cy_Str();
s->_str = fmt::format(fmt, Cy_Str_Adapter<T, Cy_Str>::adapt(args)...);
return s;
}
"""
Str
format
"format<Cy_Str>"
(
const
char
*
,
...)
except
+
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment