C++ functions matching the interface and behavior of python string methods with std::string

Pystring is a collection of C++ functions which match the interface and behavior of python's string class methods using std::string. Implemented in C++, it does not require or make use of a python interpreter. It provides convenience and familiarity for common string operations not included in the standard C++ library. It's also useful in environments where both C++ and python are used.

Overlapping functionality (such as index and slice/substr) of std::string is included to match python interfaces.

Originally developed at Sony Pictures Imageworks.
http://opensource.imageworks.com/

Note: Despite the infrequent updates, this repo is not dead/abandoned - just stable!  We use it every day at Imageworks.
Owner
Sony Pictures Imageworks
Sony Pictures Imageworks
Comments
  • Directly return vector<string> where appropriate

    Directly return vector where appropriate

    Is there a strong reason why the current API shies away from ever returning a vector of strings as a direct return value, like the actual Python API?

    This is particularly useful with C++11 implementations, where auto segs = split(s, delim) is much more concise than having to make a separate and fully-qualified declaration for the std::vector<std::string> segs object before passing it as a mutable reference. In my understanding, modern compilers have no trouble optimizing away this pattern into an equivalent in-place mutation of the return object.

  • Tests for abspath don't pass on Windows

    Tests for abspath don't pass on Windows

    This isn't really a show stopper, but on Windows the tests don't agree with the behavior, so the abspath tests fail. The actual functionality seems sane and intentional, so I dunno if the fix is to change the behavior of the implementation to be consistent across platforms, or probably just change the expectation of the tests on Windows (or skip the abspath tests on Windows entirely).

    Anyhow, here's what it is doing: In test.cpp: PYSTRING_CHECK_EQUAL(pystring::os::path::abspath("", "/net"), "/net");

    But on windows the implementation uses "" in canonical names rather than "/", because Windows. So it fails because "\net" isn't equal to "/net" The implementation of abspath on Windows runs return normpath_nt(p); to convert the '/' to '' here https://github.com/wrosecrans/pystring/blob/build/pystring.cpp#L1456

    Here is a snippet of the output from running the tests:

    Test [pystring] [translate] - PASSED D:\a\1\s\test.cpp:516: FAILED: pystring::os::path::abspath("", "/net") == "/net" values were '\net' and '/net' D:\a\1\s\test.cpp:517: FAILED: pystring::os::path::abspath("../jeremys", "/net/soft_scratch/users/stevel") == "/net/soft_scratch/users/jeremys" values were '\net\soft_scratch\users\jeremys' and '/net/soft_scratch/users/jeremys' D:\a\1\s\test.cpp:518: FAILED: pystring::os::path::abspath("../../../../tmp/a", "/net/soft_scratch/users/stevel") == "/tmp/a" values were '\tmp\a' and '/tmp/a' Test [pystring] [abspath] - FAILED Test [pystring_os_path] [splitdrive] - PASSED Test [pystring_os_path] [isabs] - PASSED

    In the context of doing CI for a thing that uses PyString, it's convenient to be able to use the tests as a sanity check to make sure I haven't screwed up something completely in my build setup. I am using CMake to build with the MSVC compiler instead of the Makefile that uses G++, but it's not doing anything particularly exciting:

    https://github.com/wrosecrans/pystring/blob/build/CMakeLists.txt

  • [SOLVED] pystring::split misunderstanding

    [SOLVED] pystring::split misunderstanding

    My little app crashes with segfault, here's Valgrind diagnosis (after using pystring::split in code):

    6,253 bytes in 117 blocks are possibly lost in loss record 13 of 14
      in pystring::(anonymous namespace)::split_whitespace(std::string const&, std::vector<std::string, std::allocator<std::string> >&, int) in pystring/pystring.cpp:109
      1: operator new(unsigned long) in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so
      2: std::string::_Rep::_S_create(unsigned long, unsigned long, std::allocator<char> const&) in /usr/lib/libstdc++.so.6.0.18
      3: char* std::string::_S_construct<char*>(char*, char*, std::allocator<char> const&, std::forward_iterator_tag) in /usr/lib/libstdc++.so.6.0.18
      4: std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::string const&, unsigned long, unsigned long) in /usr/lib/libstdc++.so.6.0.18
      5: std::string::substr(unsigned long, unsigned long) const in /usr/lib/libstdc++.so.6.0.18
      6: pystring::(anonymous namespace)::split_whitespace(std::string const&, std::vector<std::string, std::allocator<std::string> >&, int) in pystring/pystring.cpp:109
      7: pystring::split(std::string const&, std::vector<std::string, std::allocator<std::string> >&, std::string const&, int) in pystring/pystring.cpp:172
      8: ProcessLine(std::string const&, datastorage&) in main.cpp:54
      9: ProcessFile(char const*) in main.cpp:41
      10: main in main.cpp:28
  • Current implementation of 'find' has a bug that causes it to infinite loop

    Current implementation of 'find' has a bug that causes it to infinite loop

    I fixed the bug so that behavior matches python's implementation of find with an empty string as the source string, and added a small suite of unit tests for the find function (which includes cases for the fixed bugs).

    I tried to keep the code style consistent with what's there already, but feel free to alter it in any way you see fit :)

  • Need python bindings

    Need python bindings

    Sure, the python string functions are awesome if you need them in C++, but what is one to do if you need them in python? Perhaps boost python would be appropriate? (Please ping the developers before attempting this task, though). ;)

  • Add CMake build option.

    Add CMake build option.

    I need to package this for Fedora Linux in preparation of OpenColorIO 2.0 release. The simple Makefile is not very packager friendly. This should work for any Linux distro and can be updated for MacOS and Windows if needed.

  • Add a CMake configuration

    Add a CMake configuration

    This is intended as an alternative to https://github.com/imageworks/pystring/pull/27

    It is a fairly complete CMake implementation (which does start to look a little silly vs the 25 lines of Make 😄 ). I needed it for putting the package in Hunter and maybe it is useful here?

  • testing both posix and nt version of the abspath call

    testing both posix and nt version of the abspath call

    Looks like the way the code is structured, there is a top level abspath call which would call a lower level abspath_posix and abspath_nt depending on which os you built the library on.

    As far as testing, all the other tests call the $func_nt and $func_posix directly with appropriate test data which works on both windows/linux system (though arguably you may be testing more than you need to test).

    For some reason, abspath was the only one testing the top level abspath call. So I just changed to test both nt and posix like the others + appropriate test inputs for the windows version.

  • Deadlock in split_path

    Deadlock in split_path

    This line deadlocks if path has double slash, like in "D:/dir//"

    https://github.com/imageworks/pystring/blob/c2de99deb4f0bd13751f8436400b5e8662301769/pystring.cpp#L1364

    I think the fix is to slice head2, not head: head2 = pystring::slice(head2,0,-1);

  • Switch from makefile to wscript

    Switch from makefile to wscript

    I noticed the libtool arguments don't work on mac os x. I provided a small wscript for using with the waf build system:

    https://waf.io

    Thought I would push in case you folks would be interested.

    I also added a rule to build a pkg-config file for the library.

  • strip function crashes with empty string argument

    strip function crashes with empty string argument

    http://code.google.com/p/pystring/issues/detail?id=2

    Reported by [email protected], Jul 19, 2010

    What steps will reproduce the problem?

    1. strip("")

    What is the expected output? What do you see instead? The expected output is empty string. Instead the function crashes.

    What version of the product are you using? On what operating system? My version is from pystring_snapshot_012810.zip. Operating system is Windows XP, compiler is Visual Studio 2008.

    Comment 1 by project member [email protected], Feb 8, 2011 The Python strip function returns the original string unaltered when the strip characters is empty.

  • Add removeprefix and removesuffix to pystring

    Add removeprefix and removesuffix to pystring

    Add removeprefix and removesuffix to pystring. Python 3.9 added removeprefix and removesuffix methods to Pythons string class. Add them to pystring to match the interfaces.

Related tags
Yori is a CMD replacement shell that supports backquotes, job control, and improves tab completion, file matching, aliases, command history, and more.

Yori is a CMD replacement shell that supports backquotes, job control, and improves tab completion, file matching, aliases, command history, and more.

Aug 7, 2022
A wrapper of C++ std::vector for functional programming

Say hello to functional C++ vectors A wrapper for C++ std::vector geared towards functional programming and fluent APIs. The primary focus is readabil

Jul 26, 2022
A dynamic array similar to std::vector

Vector [] vector is a dynamic array implemented using c++ and it has almost all the features of std::vector including iterators. Usage can be used in

Dec 21, 2021
Bitset Sort, a faster std::sort replacement.

Bitset Sort Bitset Sort is a variant of quick sort, specifically BlockQuickSort. Bitset Sort uses a carefully written partition function to let the co

Jul 7, 2022
Cut down and minimalistic C++ string formatting library

nanofmt https://github.com/potatoengine/nanofmt License Copyright (c) Sean Middleditch and contributors nanofmt is released under the MIT license. nan

Jun 7, 2022
Shpp - Call c++ functions from a shell with any arguments of any types parsed automatically
Shpp - Call c++ functions from a shell with any arguments of any types parsed automatically

shpp Call c++ functions from a shell with any arguments of any types parsed automatically Declare a variable or define a function and register it in s

Jun 8, 2022
CLI11 is a command line parser for C++11 and beyond that provides a rich feature set with a simple and intuitive interface.
CLI11 is a command line parser for C++11 and beyond that provides a rich feature set with a simple and intuitive interface.

CLI11: Command line parser for C++11 What's new • Documentation • API Reference CLI11 is a command line parser for C++11 and beyond that provides a ri

Aug 15, 2022
:computer: C++ Functional Terminal User Interface. :heart:
:computer: C++ Functional Terminal User Interface. :heart:

FTXUI Functional Terminal (X) User interface A simple C++ library for terminal based user interface. Demo: Feature Functional style. Inspired by [1] a

Aug 17, 2022
ImTui: Immediate Mode Text-based User Interface
ImTui: Immediate Mode Text-based User Interface

imtui ImTui is an immediate mode text-based user interface library. Supports 256 ANSI colors and mouse/keyboard input. Live demo in the browser Eventh

Aug 12, 2022
CLIp is a clipboard emulator for a command line interface written in 100% standard C only. Pipe to it to copy, pipe from it to paste.

CLIp v2 About CLIp is a powerful yet easy to use and minimal clipboard manager for a command line environment, with no dependencies or bloat. Usage Sy

Sep 18, 2021
Collection of human friendly terminal interface for git.
Collection of human friendly terminal interface for git.

A collection of human friendly terminal user interface for git.

Jul 20, 2022
Add a command-line interface to any C++ program

Add a command-line interface to any C++ program

Aug 12, 2022
A Command-Line-Interface Debugger for 64-bit Windows written in C.

Debugger-For-Windows A command-line-interface debugger for 64-bit Windows. [email protected]:/mnt/c/Projects/C/Debugger$ ./Debugger.exe ./Tests/test.ex

Nov 3, 2021
This is a simple CLI interface helper library for C.

LIBCCLI This is a very simple shell like interface for CLI activities. More will be added to this, but for now, this is the basic idea:

Jun 16, 2022
A simple header-only C++ argument parser library. Supposed to be flexible and powerful, and attempts to be compatible with the functionality of the Python standard argparse library (though not necessarily the API).

args Note that this library is essentially in maintenance mode. I haven't had the time to work on it or give it the love that it deserves. I'm not add

Aug 5, 2022
A simple header-only C++ argument parser library. Supposed to be flexible and powerful, and attempts to be compatible with the functionality of the Python standard argparse library (though not necessarily the API).

args Note that this library is essentially in maintenance mode. I haven't had the time to work on it or give it the love that it deserves. I'm not add

Aug 31, 2021
A CLI program that helps you find classes and plan pre-requisites. Written in C++ and Python.

CourseHelper A CLI program created to help you prepare for course registration. Note: At the moment, this project is built specifically for other UCLA

Jan 25, 2022
Windows command line program for Spleeter, written in pure C, no need of Python.
Windows command line program for Spleeter, written in pure C, no need of Python.

SpleeterMsvcExe is a Windows command line program for Spleeter, which can be used directly. It is written in pure C language, using ffmpeg to read and write audio files, and using Tensorflow C API to make use of Spleeter models. No need to install Python environment, and it does not contain anything related to Python.

Aug 4, 2022
A small self-contained alternative to readline and libedit that supports UTF-8 and Windows and is BSD licensed.

Linenoise Next Generation A small, portable GNU readline replacement for Linux, Windows and MacOS which is capable of handling UTF-8 characters. Unlik

Jun 28, 2022