- Notifications
You must be signed in to change notification settings - Fork0
What if c++ strings where as easy to use as Python strings?
License
schmouk/pythonic-cpp-strings
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Let's just use c++ strings as are Python ones, with same API or as similar API as possible.
Librarycpp-strings is fully templated. Just download header filecppstrings.h and put it anywhere in your project. Notice: all of its stuff is declared and defined in namespacepcs - which stands forPythonicC++Strings.
- The templated class
pcs::CppStringT<>defines all constructors and methods that implement the equivalent of Python strings API. - Class
pcs::CppStringspecializes the templated class withcharcharacters. - Class
pcs::CppWStringspecializes the templated class withwchar_tcharacters.
The cpp-stringsHTML documentation is available in subdirectorycpp-strings/html. Click on fileindex.html there from your local file manager to browse its content.
Librarycpp-strings is implemented with the currently most recent c++ standard available with gcc, clang and msvc c++ compilers, i.e. standardc++20.
- directory
cpp-stringscontains the header filecppstring.h.
This is the header file to include in your projects. - directory
cpp-strings-testscontains the unit-tests filecpp-strings-tests.cpp
This file tests all the library stuff. It is a valuable code container with so many examples of use of the library, the classes, the methods and the functions it defines.
The code has been developed using VS2022 IDE. As such, unitary tests have been coded using Microsoft Visual Studio Cpp Unit Test Framework. The related VS project is provided with this library.
Notice: no clang, gcc or msvc specific declarations or goodies have been use to code librarycpp-strings. It should then be compilable with any of these compilers. To be able to use it with your project, just ensure that the c++ standard used with you favorite compiler isc++20:
- options
-std=c++20or-std=c++latestfor clang and gcc (v. 10 and above),
or option-std=c++2afor clang and gcc (v. 9 and earlier); - options
/std=c++20or/std=c++latestfor Visual Studio 2019 and later.
If you want to run the unitary tests, well, use the last version of Visual Studio (i.e. VS 2022, by July 2025). The Community free version will truly be fine.
The vcproject associated with the unit tests incpp-strings is already configured to use the option flag/std:c++latest since the implemented code uses a very few but very useful c++23 goodies.
github repository:https://github.com/schmouk/pythonic-cpp-strings
pythonic-cpp-strings github web pages:https://schmouk.github.io/pythonic-cpp-strings/
Library cpp-strings"What if c++ strings where as easy to use as Python strings?"Copyright (C) 2023-2025 Philippe Schmoukercontact - ph (dot) schmouker (at) gmail (dot) comThis program is free software: you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation, either version 3 of the License, or(at your option) any later version.This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with this program. If not, see <https://www.gnu.org/licenses/>.The creation of this library has been started with the aim at easing the use of strings in c++. Other projects exist, even aiming also to implement a Python-like strings API, but the only ones we have found were either started but not completed, or were not implementing the whole Python API.
So, we started to work onPythonic c++ strings. Librarycpp-strings is now ready. It fully implements the Python API (even with a c++ implementation ofslices) and it is fully tested withunitary tests.
The implementation of librarycpp-strings is fully templated and is contained into a single header file:cpp-strings/cppstrings.h. To take a whole benefit of it, just add this header file to your project and include it in your own files. It very fastly compiles. That's all!
Header filecpp-strings/cppstrings.h defines:
- templated class
template<typename CharT> class pcs::CppStringTwhich implements all the stuff about pythonic c++ strings. It inherits from c++ STL classstd::basic_string<CharT>, so it gets access to all c++ STL methods, functions and goodies about strings; - this class is specialized by:
using pcs::CppString> = pcs::CppStringT<char>for pythonic c++ strings based oncharcharacters; - it is also specialized by:
using pcs::CppWString> = pcs::CppStringT<wchar_t>for pythonic c++ strings based onwchar_tcharacters.
The unitary tests are provided in directorycpp-strings-tests/. Filecpp-strings-tests/cpp-strings-tests.cpp contains the related code. Tests have been coded using VS2022 IDE and as such, are using the Microsoft Visual Studio Cpp Unit Test Framework. The related VS project can be found in the same directory. It is already configured to create code for Release as well as for Debug configurations, and for 32-bits or 64-bits platforms at your choice.
This.cpp file is a great place to seecpp-strings code in action. Each structure, class, method, litteral operator and function being unitary tested in here, you should find there many examples of its use for allcpp-strings stuff.
The coding of this project started by March 2023 and had been put in standby mode for some months - no time to work on it. The very first release is now availble (since July 2025), asRelease 1.0.4.
This release has been fully tested. Code coverage is 100%.
Release 1.0 implements all Python strings API but one feature (see below).
Pythonslices are implemented via c++operator() as declared and defined in the templated classpcs::CppStringT<>. Python slices have next specific notation:[start : stop : step] and allow the running step by step through range [start, stop) (notice: stop is excluded from the range). Operator(start, stop, step) acts the same way while running through the content of pythonic c++ strings. A dedicated base classSlice is also provided and can be passed as argument tooperator(). It is derived in many simpler slices classes, since Python slices may not define eitherstart,stop orstep which then get default values (resp. 0,end-of-sequence, and 1).
Notice: Python 3.14 (released by Oct. 2025) implements Template Strings (T-Strings), seestring.templatelib — Support for template string literals in Python 3.14 documentation andPEP 750 for full explanations. Meanwhile, T-strings are not implemented in CppStrings library: the templates concept is already available in c++ language. The "translation" of T-Strings into CppStrings library is then left as an (easy) exercice to the user.
Python strings are based on Unicode chars. This is currently not the case for pythonic c++ strings incpp-strings.
Unicode encoding allows for a specific comparison mode on strings, thecasefold mode. There, Unicode chars are transformed into their lower equivalent char in a standardized manner which is more "agressive" than the simpler lowering methodlower(). Comparing strings without taking into account the case of their chars is then performed in a far more accurate manner.
This feature is currently NOT implemented in librarycpp-strings.
So up to now, if you want to compare pythonic c++ strings fromcpp-strings on whatever the case is for each of their chars, compare them applying method.lower() to both strings. This will do the job for chars and for wchar_t also for a majority of languages (but might fail for very few signs of specific languages in this last case).
Notice: dealing with Unicode chars and implementing methodcasefold() as it is the case in Python is planned for a next release of librarycpp-strings.
N.B. "planned for a next release" does not imply that a fixed date is planned either.
Thecpp-strings HTML documentation is available in subdirectory./documentation. Click on fileindex.html there from your local file manager to browse its content.
It has then been produced with the great utilitydoxygen developed by Vicente Hernando (the doxygen Github repository can be accessedhere, and the utility can be downloaded from there:https://www.doxygen.nl/download.html).
This is a short documentation of the library. For some of its part it is a copy of the Python original documentation on strings. For its other part it documents the c++ implementation ofcpp-strings.
And remember: to better understand how to use this pythonic c++ strings library, have a look also atcpp-strings-tests/cpp-strings-tests.cpp to seecpp-strings library code in action!
About
What if c++ strings where as easy to use as Python strings?
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
![[Library banner]](/image.pl?url=http%3a%2f%2fgithub.com%2fschmouk%2fpythonic-cpp-strings%2fraw%2fmain%2fbanner-image.jpg&f=jpg&w=240)