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

Related tags

CLI pystring
Overview
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.
Issues
  • 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

    opened by wrosecrans 5
  • [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
    opened by ghost 4
  • 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 :)

    opened by cchrisman 4
  • 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). ;)

    opened by jeremyselan 4
  • 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.

    opened by grdanny 2
  • 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);

    opened by vjjjv 2
  • 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.

    opened by ssteinbach 2
  • 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.

    opened by jeremyselan 2
  • License is not

    License is not "detected" as BSD-3 Clause by github

    Hi,

    The license of this project seems to match the overall license of OpenColorIO (the only difference is that it mentions "Sony" directly, instead of referring only to the copyright holders.

    It would help to have it explicitly detected as [BSD 3-Clause "New" or "Revised" License] by github.


    More context, I'm updating Blender's 3rd party license document for Blender 3.2. We now depend on Pystrings since OCIO itselfs depends on it during build time.

    It helps a lot to quickly find what is the license of a dependency without having to compare the license line-by-line to see if it is compatible with GPL :)

    opened by dfelinto 1
  • #25 declaring global string constants for performance improvement

    #25 declaring global string constants for performance improvement

    following up on issue #25 there is indeed a noticeable performance improvement when std strings like "/" or "." are all pre-defined vs making them again and again. I declared them all as constants at the top.

    opened by grdanny 1
  • isabs performance improvement

    isabs performance improvement

    Functions like pystring::os::path::isabs_posix call pystring::startswith:

    bool isabs_posix(const std::string & s) { return pystring::startswith(s, "/"); }

    And it seems that this will create a new std::string for "/" each time this is called which will dominate the execution time of code that calls isabs() repeatedly. Would it be reasonable to declare some of these constants to amortize this cost?

    bool isabs_posix(const std::string & s) { static const std::string slash("/"); return pystring::startswith(s, slash); }

    It seems "/" and "\" is used several times in pystring as the arguments to startswith, endswith, strip, etc. which take references to std::string as arguments and this could be more optimal for the cases where these functions get called a lot?

    opened by swahtz 1
  • 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.

    opened by niclasr 4
  • 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?

    opened by hjmallon 1
  • 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.

    opened by hobbes1069 2
Owner
Sony Pictures Imageworks
Sony Pictures Imageworks
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.

Malcolm Smith 1.1k Jul 4, 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

null 17 Feb 18, 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

Tanish Porwal 1 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

null 63 Jun 6, 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

Sean Middleditch 10 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 Declare a variable or define a function and register it in s

Pedro Moreira 96 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: 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

null 2.1k Jul 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

Taylor C. Richberger 986 Jul 4, 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

Taylor C. Richberger 896 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

Kyle Chui 1 Jan 25, 2022
: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

Arthur Sonzogni 3.3k Jul 3, 2022
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

Georgi Gerganov 1.9k Jul 1, 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

A.P. Jo. 12 Sep 18, 2021
Collection of human friendly terminal interface for git.

A collection of human friendly terminal user interface for git.

Arthur Sonzogni 61 Jun 26, 2022
Add a command-line interface to any C++ program

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

Empirical Software Solutions, LLC 405 Jun 8, 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

Tomer Gibor 1 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:

Steven Rostedt 39 Jun 16, 2022
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.

Wudi 162 Jun 27, 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

ArangoDB 335 Jun 28, 2022