57 Commits
Author SHA1 Message Date
Supan Roy 1922c8f2b4 Add additional replace overlap edge case tests (#63)
* add additional replace overlap edge case tests

* Add additional Python compatibility edge case tests
2026-06-13 16:01:32 -07:00
Danny Greenstein 266d23c9b4 Add permanent header-only and compiled library targets with CMake export support (#62)
* updating version numbers

* no need for meson options now that we are building both compiled and header only together and providing them as options

* no need for header only ci tasks, each ci task now builds and tests two different targets, regular pystring and pystring_header_only

* no need for header only ci tasks, each ci task now builds and tests two different targets, regular pystring and pystring_header_only

* updated meson and cmake build systems to have two available targets for the user to pick from compiled and header only

* adding cmake configuration file

* removing empty ci target

* adding bit in pystringConfig to prevent double definition error

* adding newline in file end for consistency
2026-06-13 15:38:34 -07:00
Danny Greenstein 381829c2c1 Match our isupper with python behavior (#55)
* tightening compatbility with python's behavior in edge cases

* a couple of more edge cases
2026-02-23 17:04:14 -08:00
Danny Greenstein f64d7e3130 enforcing min c++ 11 to match the new meson build system (#56) 2026-02-22 16:33:16 -08:00
Danny Greenstein 64a428f622 header only build option (#54)
* new header only build option

* removed extra semi colon

* remaing marcos in new header file with a PYSTRING prefix so that they don't clash with other definitions when used in different contexts
2026-02-22 15:30:28 -08:00
Zephyr Lykos a9df4d1bb3 ci: only bypass b_lundef if using clang+sanitize (#53) 2025-12-04 19:32:22 -08:00
Larry Gritz c195f66709 Install locally by default to avoid naively overwriting /usr/local (#52)
Use `-DCMAKE_INSTALL_PREFIX=...` to set a non-local install
destination.

Signed-off-by: Larry Gritz <lg@larrygritz.com>
2025-11-11 18:41:00 -08:00
Zephyr Lykos a09708a487 Add meson build system (#50)
* Add meson build system

* Sync cmake's installation layout to Makefile and Meson builds

* meson: remove cmake config target generation

this pulls in an extra dependency on the cmake binary,
CMake users can use the PkgConfig module instead.

* ci: add meson tests

* build: default to c++17

* ci: fix cmake builds

* tests: fix memory leak in tests

* ci: workaround clang sanitizer linking bug

* build: install headers into a subdir
2025-10-05 21:30:32 -07:00
Danny Greenstein 02ef1186d6 Simplify github actions CI yaml file to use stock github runners and latest versions of windows/mac/ubuntu (#48)
* simplified github actions to include just 8 tasks, 4 linux, 2 macos, 2 windows, always on the latest version of the os/runners that github gives us

* using specific sha when pointing to the actions/checkout repo instead of a tag, a tag can be moved around, but a sha can't so it's more stable
2025-06-22 19:44:53 -07:00
Danny Greenstein 962fd25b25 Merge pull request #42 from svenstaro/patch-1
Proper installation paths
2025-05-13 23:48:06 -07:00
Danny Greenstein f7cdd17584 Merge pull request #47 from tgurr/cmake-4.0
cmake: fix build with cmake 4.0.0
2025-05-13 23:37:13 -07:00
Timo Gurr e5df7dd77f cmake: fix build with cmake 4.0.0 2025-05-13 16:30:22 +02:00
Danny Greenstein c5ca4f569d Merge pull request #43 from marza-sergey/cmake-improvements
Added option to build static lib and installing header using cmake
2024-08-18 13:45:46 -07:00
Danny Greenstein 76a2024e13 Merge pull request #30 from niclasr/removestarfix
Add removeprefix and removesuffix to pystring
2023-07-22 19:37:05 -07:00
Morozov Sergey 4b8029a55c Added option to build static lib and installing header 2023-05-15 15:15:31 +09:00
Sven-Hendrik Haase 74700b2c04 Proper installation paths
It's Makefile convention to be able to install to `DESTDIR`. This is especially important for packaging on Linux distributions but might be convenient for other users as well. Also this introduces `PREFIX`, another convention.
2023-05-04 03:42:21 +02:00
Danny Greenstein 7d16bc814c Merge pull request #34 from jayvdb/readme-shield
README: Add repology shield
2022-09-26 17:28:46 -07:00
Danny Greenstein 1f56d0f745 Merge pull request #37 from imageworks/grdanny-ci-tests
adding ci.yml with cross platform tests.
2022-09-25 16:58:33 -07:00
Daniel Greenstein 87f735237b additional comment for future self 2022-08-22 00:47:39 +01:00
Daniel Greenstein e7b4ab950a trying out dynamic build type again 2022-08-22 00:44:57 +01:00
Daniel Greenstein 3b85d48cf1 adding aswf ci yaml file 2022-08-22 00:39:52 +01:00
Danny Greenstein 089194ddaf Merge pull request #18 from chadrik/patch-1
Allow default values in Makefile to be overridden
2022-07-23 19:46:38 -07:00
Danny Greenstein 7631f72651 Merge pull request #15 from clhsieh/direct_return_wrapper
Implement the wrapper for direct return vector<string> for issue #12
2022-07-23 19:46:18 -07:00
Danny Greenstein b62203a4b6 Merge pull request #36 from imageworks/basic_cmake_setup
initial version of cmake config
2022-07-17 09:41:02 -07:00
Daniel Greenstein fd8b46f79b moving cmake version to 3.2 2022-07-11 23:38:17 +01:00
Daniel Greenstein bf5cd2d3b1 initial version of cmake config 2022-07-11 01:26:19 +01:00
John Vandenberg 22839311d1 README: Add repology shield 2022-06-13 12:08:27 +08:00
Larry Gritz cce54a1bcb Revise license formatting to match other modern projects. (#33)
No change to the fact that this project is BSD-3-clause licensed.

But this slight rewording/reformatting:

* Brings the notices in line with the exact wording we use in other
  projects such as OSL.

* Now will be correctly recognized by GitHub and other automated
  license checkers as being EXACTLY the same as the canonical BSD
  3-clause license.

This was approved by SPI legal.
2022-06-08 21:51:45 -07:00
Niclas Rosenvik e8e34d02a6 Add tests for removeprefix and removesuffix 2022-03-04 13:39:47 +01:00
Niclas Rosenvik 5a0840a737 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.
2022-03-04 13:21:02 +01:00
Danny Greenstein 281419de2f Merge pull request #26 from grdanny/master
#25 declaring global string constants for performance improvement
2020-02-03 22:24:44 -08:00
Daniel Greenstein 9bfc0b94ec more efficient comparison with the colon string 2020-02-04 06:19:00 +00:00
Daniel Greenstein b9b2a2eb46 declaring global string constants for performance improvement 2020-02-04 05:32:18 +00:00
Danny Greenstein 4af4a86520 Merge pull request #24 from imageworks/issue_20_deadlock_in_split_path
accepting fix proposed in issue #20 - for deadlock in path split when…
2019-03-20 21:17:31 -07:00
Daniel Greenstein 8c0f1738fc accepting fix proposed in issue #20 - for deadlock in path split when the path ends in a double slash 2019-03-04 02:20:11 +00:00
Danny Greenstein 20dac44746 Merge pull request #23 from grdanny/abspath-tests-for-both-nt-and-posix
testing both posix and nt version of the abspath call
2019-02-25 22:30:16 -08:00
Daniel Greenstein 3670fab511 testing both posix and nt version of the abspath call 2019-02-24 19:01:53 +00:00
Chad Dombrova 851abee07a Allow default values in Makefile to be overridden
This is handy when building this within an automated build system where creating and applying a patch is a PITA.
2015-10-21 16:03:04 -07:00
Changlin Hsieh 8d1bdb2e43 Implement the wrapper for direct return vector<string> for issue #12 2014-02-11 11:12:29 +08:00
Jeremy Selan c2de99deb4 fix replace on empty strings 2012-10-16 14:31:17 -07:00
Jeremy Selan f38e93591b fix rsplit for non-space characters 2012-10-16 14:30:43 -07:00
Jeremy Selan 9dc73e8533 switch ssize_t -> Py_ssize_t for windows friendliness 2012-04-13 10:19:29 -07:00
Jeremy Selan ae50dda1ea exposed all os::path posix vs. nt varients 2012-04-10 17:05:23 -07:00
Jeremy Selan a5c3cfa274 do_strip windows bugfix
switched to use ssize_t (signed) for internal counters, to match the python
source.  (see Modules/stropmodule.c : do_strip)

This addresses a bug uncovered by Laszlo Sebo on Windows, when stripping an
empty string.
2012-04-10 16:29:12 -07:00
Jeremy Selan 6c7eadc6c2 added os::path::abspath 2011-10-25 10:52:49 -07:00
Jeremy Selan 1080f1a199 os::path::join : added addl vector signature for joining >2 dir paths 2011-06-21 13:39:11 -07:00
Jeremy Selan 235bcf5ddf windows #define belongs in .cpp, not .h 2011-06-20 18:51:38 -07:00
Jeremy Selan fef753074e bugfix : find/rfind dont return bad valid results when start/end points smaller
than the seach string are specified.

Addressed github #7
2011-06-20 18:44:37 -07:00
Jeremy Selan aa52269ea2 added os.path functionality (limited subset)
Exposed a limited set of function from os.path (most of those which rely on pur
string processing have been implemented).  These have been added in an
architecture specific manner to match the python implementation. (I.e., join on
windows uses '\', and joing on linux uses '/'.  The python 2.6 implementation
were used as reference, and for unit test validation.
2011-06-20 16:53:56 -07:00
Jeremy Selan 53e5b88e08 added unittests for pystring::find
This exposes two previously unknown bugs in the pystring::find implementation:

pystring::find("", "") -> -1
    Correct: 0
pystring::find("abcabcabc", "bc", 4, 5) -> 4
    Correct: -1
2011-06-20 16:00:40 -07:00
Jeremy Selan 0f4ab0dd3f bugfix: startswith / endswith does not overflow when suffix size > str size
The previous implementation of startswith / endswith would overflow when the
suffix size > str size, due to an issue with the __substrcmp(str, suffix, lower
) implementation. The updated fix was to re-implement both of these functions looking at the
python 2.7 source, which relies on the _string_tailmatch convenience function.

Addresses issue #2
2011-06-20 15:15:13 -07:00
Jeremy Selan 38b83f0373 bugfix: pystring::translate works when using both table + deletechars
Fixed bug in translate, documented in github issue #3, and previously at
http://code.google.com/p/pystring/issues/detail?id=4
2011-06-20 13:41:32 -07:00
Jeremy Selan b09b4ae913 added unittest framework
The unittests can be run with the
`make test`
build target.

They currently fail on a single test for pystring::translate as a demonstration
of the unit-tests fail behavior. (This error is a known issue, documented as #3)
2011-06-20 13:39:27 -07:00
Jeremy Selan af3cdaaa7c added pystring::mul
Add the __mul__ function, the string multiplication operator.
2011-06-20 12:00:38 -07:00
Jeremy Selan 1076a670f2 misc compilation warning fixes
This makes pystring compile without warnings on gcc44, when compiled using the da
-Wall flag. Most of the changes are issues related to the intermixing of size_type / int
types, where the updated version of the code attempts to do as few type changes
to the user arguments as possible. This code has been running cross-platform
in OpenColorIO for the last few months, and is well tested.
2011-06-20 11:52:45 -07:00
Jeremy Selan 4e785229c4 Edited README via GitHub 2011-04-21 12:10:05 -07:00
Jeremy Selan 2ded9b2604 Edited README via GitHub 2011-04-21 12:09:37 -07:00
16 changed files with 3978 additions and 1159 deletions
+225
View File
@@ -0,0 +1,225 @@
# simple ci config the idea is to use the latest ubuntu, macos, and windows that github runners give us
# test a few different compilers (gcc/clang) in debug and release
# and run our tests via ctest
name: CI
on:
push:
# Jobs are skipped when ONLY Markdown (*.md) files are changed
paths-ignore:
- '**.md'
pull_request:
paths-ignore:
- '**.md'
jobs:
linux:
name: '${{ matrix.os }} / ${{ matrix.build-type }} / ${{ matrix.compiler-desc }} '
runs-on: ubuntu-latest
strategy:
matrix:
build: [1, 2, 3, 4]
include:
# -------------------------------------------------------------------
# CLANG, Release
# -------------------------------------------------------------------
- build: 1
build-type: Release
build-shared: 'ON'
cxx-standard: 17
cxx-compiler: clang++
cxx-flags: ''
cc-compiler: clang
compiler-desc: clang
os: ubuntu-latest
# -------------------------------------------------------------------
# CLANG, Debug
# -------------------------------------------------------------------
- build: 2
build-type: Debug
build-shared: 'ON'
cxx-standard: 17
cxx-compiler: clang++
cxx-flags: ''
cc-compiler: clang
compiler-desc: clang
os: ubuntu-latest
# -------------------------------------------------------------------
# gcc, Release
# -------------------------------------------------------------------
- build: 3
build-type: Release
build-shared: 'ON'
cxx-standard: 17
cxx-compiler: g++
cxx-flags: ''
cc-compiler: gcc
compiler-desc: gcc
os: ubuntu-latest
# -------------------------------------------------------------------
# gcc, Debug
# -------------------------------------------------------------------
- build: 4
build-type: Debug
build-shared: 'ON'
cxx-standard: 17
cxx-compiler: g++
cxx-flags: ''
cc-compiler: gcc
compiler-desc: gcc
os: ubuntu-latest
env:
CXX: ${{ matrix.cxx-compiler }}
CC: ${{ matrix.cc-compiler }}
steps:
- name: Checkout
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- name: Create build directories
run: |
mkdir _install
mkdir _build
- name: Configure
run: |
cmake .. \
-DCMAKE_INSTALL_PREFIX=${{ github.workspace }}/_install \
-DCMAKE_BUILD_TYPE=${{ matrix.build-type }} \
-DCMAKE_CXX_STANDARD=${{ matrix.cxx-standard }} \
-DCMAKE_CXX_FLAGS=${{ matrix.cxx-flags }} \
-DCMAKE_VERBOSE_MAKEFILE:BOOL='OFF' \
-DBUILD_SHARED_LIBS=${{ matrix.build-shared }} \
working-directory: _build
- name: Build
run: |
cmake --build . \
--config ${{ matrix.build-type }}
working-directory: _build
- name: Test
run: |
ctest --build-config ${{ matrix.build-type }} --verbose
working-directory: _build
# ---------------------------------------------------------------------------
# macOS
# ---------------------------------------------------------------------------
macos:
name: '${{ matrix.os }} / ${{ matrix.build-type }}'
runs-on: macos-latest
strategy:
matrix:
build: [1, 2]
include:
# Release
- build: 1
build-type: Release
build-shared: 'ON'
cxx-standard: 17
cxx-flags: ''
os: macos-latest
# Debug
- build: 2
build-type: Debug
build-shared: 'ON'
build-docs: 'OFF'
cxx-standard: 17
cxx-flags: ''
os: macos-latest
steps:
- name: Checkout
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- name: Create build directories
run: |
mkdir _install
mkdir _build
- name: Configure
run: |
cmake ../. \
-DCMAKE_INSTALL_PREFIX=${{ github.workspace }}/_install \
-DCMAKE_BUILD_TYPE=${{ matrix.build-type }} \
-DCMAKE_CXX_STANDARD=${{ matrix.cxx-standard }} \
-DCMAKE_CXX_FLAGS=${{ matrix.cxx-flags }} \
-DCMAKE_VERBOSE_MAKEFILE:BOOL='OFF' \
-DBUILD_SHARED_LIBS=${{ matrix.build-shared }} \
working-directory: _build
- name: Build
run: |
cmake --build . \
--config ${{ matrix.build-type }} \
working-directory: _build
- name: Test
run: |
ctest --build-config ${{ matrix.build-type }} --verbose
working-directory: _build
# ---------------------------------------------------------------------------
# Windows
# ---------------------------------------------------------------------------
windows:
name: '${{ matrix.os }} / ${{ matrix.build-type }}'
runs-on: windows-latest
strategy:
matrix:
build: [1, 2]
include:
# Release
- build: 1
build-type: Release
build-shared: 'ON'
cxx-standard: 17
cxx-flags: ''
os: windows-latest
# Debug
- build: 2
build-type: Debug
build-shared: 'ON'
cxx-standard: 17
cxx-flags: ''
os: windows-latest
steps:
- name: Checkout
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- name: Create build directories
run: |
mkdir _install
mkdir _build
shell: bash
- name: Configure
# the windows build needs the -DCMAKE_WINDOWS_EXPORT_ALL_SYMBOLS to work
run: |
cmake ../. \
-DCMAKE_INSTALL_PREFIX=${{ github.workspace }}/_install \
-DCMAKE_WINDOWS_EXPORT_ALL_SYMBOLS='ON'\
-DCMAKE_BUILD_TYPE=${{ matrix.build-type }} \
-DCMAKE_CXX_STANDARD=${{ matrix.cxx-standard }} \
-DCMAKE_CXX_FLAGS=${{ matrix.cxx-flags }} \
-DCMAKE_VERBOSE_MAKEFILE:BOOL='OFF' \
-DBUILD_SHARED_LIBS=${{ matrix.build-shared }} \
shell: bash
working-directory: _build
- name: Build
run: |
cmake --build . \
--config ${{ matrix.build-type }}
shell: bash
working-directory: _build
- name: Test
run: |
ctest -C ${{ matrix.build-type }}
shell: bash
working-directory: _build
+145
View File
@@ -0,0 +1,145 @@
name: Meson
on:
pull_request:
push:
jobs:
meson-build-and-tests:
runs-on: ${{ matrix.platform }}
name: ${{ matrix.platform }}, ${{ matrix.mode.name }} ${{ matrix.flavor }}
strategy:
fail-fast: false
matrix:
flavor:
- debug
- release
mode:
- name: default
extra_envs: {}
# Alternative compiler setups
- name: gcc
extra_envs:
CC: gcc
CXX: g++
- name: clang
extra_envs:
CC: clang
CXX: clang++
- name: sanitize
args: >-
"-Db_sanitize=address,undefined"
extra_envs: {}
# This is for MSVC, which only supports AddressSanitizer.
# https://learn.microsoft.com/en-us/cpp/sanitizers/
- name: sanitize+asanonly
args: -Db_sanitize=address
extra_envs:
ASAN_OPTIONS: report_globals=0:halt_on_error=1:abort_on_error=1:print_summary=1
- name: clang+sanitize
args: >-
-Db_lundef=false "-Db_sanitize=address,undefined"
extra_envs:
CC: clang
CXX: clang++
# default clang on GitHub hosted runners is from MSYS2.
# Use Visual Studio supplied clang-cl instead.
- name: clang-cl+sanitize
args: >-
"-Db_sanitize=address,undefined"
extra_envs:
CC: clang-cl
CXX: clang-cl
platform:
- ubuntu-22.04
- windows-2022
- macos-latest
exclude:
# clang-cl only makes sense on windows.
- platform: ubuntu-22.04
mode:
name: clang-cl+sanitize
- platform: macos-latest
mode:
name: clang-cl+sanitize
# Use clang-cl instead of MSYS2 clang.
#
# we already tested clang+sanitize on linux,
# if this doesn't work, it should be an issue for MSYS2 team to consider.
- platform: windows-2022
mode:
name: clang
- platform: windows-2022
mode:
name: clang+sanitize
# MSVC-only sanitizers
- platform: ubuntu-22.04
mode:
name: sanitize+asanonly
- platform: macos-latest
mode:
name: sanitize+asanonly
- platform: windows-2022
mode:
name: sanitize
# clang is the default on macos
# also gcc is an alias to clang
- platform: macos-latest
mode:
name: clang
- platform: macos-latest
mode:
name: gcc
# gcc is the default on linux
- platform: ubuntu-22.04
mode:
name: gcc
# only run sanitizer tests on linux
#
# gcc/clang's codegen shouldn't massively change across platforms,
# and linux supports most of the sanitizers.
- platform: macos-latest
mode:
name: clang+sanitize
- platform: macos-latest
mode:
name: sanitize
steps:
- name: Setup meson
run: |
pipx install meson ninja
- name: Checkout
uses: actions/checkout@v4
- name: Activate MSVC and Configure
if: ${{ matrix.platform == 'windows-2022' }}
env: ${{ matrix.mode.extra_envs }}
run: |
meson setup build-${{ matrix.flavor }} --buildtype=${{ matrix.flavor }} -Ddefault_library=static ${{ matrix.mode.args }} --vsenv
- name: Configuring
if: ${{ matrix.platform != 'windows-2022' }}
env: ${{ matrix.mode.extra_envs }}
run: |
meson setup build-${{ matrix.flavor }} --buildtype=${{ matrix.flavor }} ${{ matrix.mode.args }}
- name: Building
run: |
meson compile -C build-${{ matrix.flavor }}
- name: Running tests
env: ${{ matrix.mode.extra_envs }}
run: |
meson test -C build-${{ matrix.flavor }} --timeout-multiplier 0
- uses: actions/upload-artifact@v4
if: failure()
with:
name: ${{ matrix.platform }}-${{ matrix.mode.name }}-${{ matrix.flavor }}-logs
path: build-${{ matrix.flavor }}/meson-logs
+5
View File
@@ -0,0 +1,5 @@
test
.libs
libpystring.la
pystring.lo
pystring.o
+103
View File
@@ -0,0 +1,103 @@
cmake_minimum_required(VERSION 3.10)
project(pystring LANGUAGES CXX VERSION 1.2.0)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
option (BUILD_SHARED_LIBS "Build shared libraries (set to OFF to build static libs)" ON)
# If the user hasn't configured cmake with an explicit
# -DCMAKE_INSTALL_PREFIX=..., then set it to safely install into ./dist, to
# help prevent the user from accidentally writing over /usr/local or whatever.
if (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT
AND PROJECT_IS_TOP_LEVEL)
set (CMAKE_INSTALL_PREFIX "${PROJECT_SOURCE_DIR}/dist" CACHE PATH
"Installation location" FORCE)
endif()
message (STATUS "Installation path will be ${CMAKE_INSTALL_PREFIX}")
include(GNUInstallDirs)
# --- Compiled library target: pystring::pystring ---
add_library(pystring
pystring.cpp
pystring.h
)
set_target_properties(pystring PROPERTIES
VERSION ${PROJECT_VERSION}
SOVERSION ${PROJECT_VERSION_MAJOR}
)
target_include_directories(pystring PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
install(TARGETS pystring
EXPORT pystringTargets
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
)
# --- Header-only target: pystring::pystring_header_only ---
add_library(pystring_header_only INTERFACE)
target_compile_definitions(pystring_header_only INTERFACE PYSTRING_HEADER_ONLY)
target_include_directories(pystring_header_only INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
install(TARGETS pystring_header_only
EXPORT pystringTargets
)
install(FILES pystring.h pystring_impl.h
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}
)
# --- Export & package config ---
install(EXPORT pystringTargets
FILE pystringTargets.cmake
NAMESPACE pystring::
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/pystring
)
include(CMakePackageConfigHelpers)
configure_package_config_file(
cmake/pystringConfig.cmake.in
pystringConfig.cmake
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/pystring
)
write_basic_package_version_file(
pystringConfigVersion.cmake
VERSION ${PROJECT_VERSION}
COMPATIBILITY SameMajorVersion
)
install(FILES
${CMAKE_CURRENT_BINARY_DIR}/pystringConfig.cmake
${CMAKE_CURRENT_BINARY_DIR}/pystringConfigVersion.cmake
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/pystring
)
# --- Tests ---
add_executable(pystring_test test.cpp)
target_link_libraries(pystring_test pystring)
add_executable(pystring_test_header_only test.cpp)
target_link_libraries(pystring_test_header_only pystring_header_only)
# Compile-time check that PYSTRING_HEADER_ONLY propagates correctly
add_executable(pystring_test_header_only_define test_header_only_define.cpp)
target_link_libraries(pystring_test_header_only_define pystring_header_only)
enable_testing()
add_test(NAME PyStringTest COMMAND pystring_test)
add_test(NAME PyStringTestHeaderOnly COMMAND pystring_test_header_only)
+24 -27
View File
@@ -1,30 +1,27 @@
Copyright (c) 2008-2010, Sony Pictures Imageworks Inc
All rights reserved.
Copyright (c) 2008-present Contributors to the Pystring project.
All Rights Reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
Neither the name of the organization Sony Pictures Imageworks nor the
names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+14 -5
View File
@@ -1,7 +1,9 @@
LIBTOOL = libtool
LIBDIR = /usr/lib
CXX = g++
CXXFLAGS = -g -O3 -Wall
LIBTOOL ?= libtool
PREFIX ?= /usr
INCLUDEDIR ?= ${PREFIX}/include/pystring
LIBDIR ?= ${PREFIX}/lib
CXX ?= g++
CXXFLAGS ?= -g -O3 -Wall -Wextra -Wshadow -Wconversion -Wcast-qual -Wformat=2
all: libpystring.la
@@ -12,7 +14,14 @@ libpystring.la: pystring.lo
$(LIBTOOL) --mode=link --tag=CXX $(CXX) -o $@ $< -rpath $(LIBDIR)
install: libpystring.la
$(LIBTOOL) --mode=install install -c $< $(LIBDIR)/$<
$(LIBTOOL) --mode=install install -Dm755 $< $(DESTDIR)$(LIBDIR)/$<
$(LIBTOOL) --mode=install install -Dm644 pystring.h $(DESTDIR)$(INCLUDEDIR)/pystring.h
clean:
$(RM) -fr pystring.lo pystring.o libpystring.la .libs
.PHONY: test
test:
$(RM) -fr test
$(CXX) pystring.cpp test.cpp $(CXXFLAGS) -DPYSTRING_UNITTEST=1 -o test
./test
-6
View File
@@ -1,6 +0,0 @@
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/
+17
View File
@@ -0,0 +1,17 @@
[![Repology](https://img.shields.io/repology/repositories/pystring)](https://repology.org/project/pystring/versions)
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.
+8
View File
@@ -0,0 +1,8 @@
@PACKAGE_INIT@
# Prevent double-definition errors
if(NOT TARGET pystring::pystring AND NOT TARGET pystring::pystring_header_only)
include("${CMAKE_CURRENT_LIST_DIR}/pystringTargets.cmake")
endif()
check_required_components(pystring)
+75
View File
@@ -0,0 +1,75 @@
project(
'pystring',
'cpp',
version: '1.2.0',
license: 'BSD-3-Clause',
license_files: 'LICENSE',
meson_version: '>=1.3',
default_options: ['cpp_std=c++17,c++11', 'warning_level=3'],
)
inc = include_directories('.')
hdrs = files('pystring.h')
# --- Compiled library target ---
pystring_lib = library(
'pystring',
files('pystring.cpp'),
implicit_include_directories: false,
include_directories: inc,
version: meson.project_version(),
install: true,
)
pystring_dep = declare_dependency(
link_with: pystring_lib,
include_directories: inc,
)
install_headers(hdrs, subdir: 'pystring')
pkgconfig = import('pkgconfig')
pkgconfig.generate(
pystring_lib,
description: 'C++ functions matching the interface and behavior of python string methods with std::string',
)
# --- Header-only target ---
pystring_header_only_dep = declare_dependency(
include_directories: inc,
compile_args: ['-DPYSTRING_HEADER_ONLY'],
)
install_headers(hdrs, files('pystring_impl.h'), subdir: 'pystring')
# --- Override default dependency ---
meson.override_dependency('pystring', pystring_dep)
# --- Tests ---
test(
'PyStringTest',
executable(
'pystring_test',
'test.cpp',
dependencies: pystring_dep,
build_by_default: false,
),
)
test(
'PyStringTestHeaderOnly',
executable(
'pystring_test_header_only',
'test.cpp',
dependencies: pystring_header_only_dep,
build_by_default: false,
),
)
# Compile-time check that PYSTRING_HEADER_ONLY propagates correctly
executable(
'pystring_test_header_only_define',
'test_header_only_define.cpp',
dependencies: pystring_header_only_dep,
build_by_default: true,
)
+8 -1052
View File
File diff suppressed because it is too large Load Diff
+231 -69
View File
@@ -1,39 +1,17 @@
///////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2008-2010, Sony Pictures Imageworks Inc
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// Neither the name of the organization Sony Pictures Imageworks nor the
// names of its contributors
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS
// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER
// OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
///////////////////////////////////////////////////////////////////////////////
// Copyright Contributors to the Pystring project.
// SPDX-License-Identifier: BSD-3-Clause
// https://github.com/imageworks/pystring/blob/master/LICENSE
#ifndef INCLUDED_PYSTRING_H
#define INCLUDED_PYSTRING_H
#ifdef PYSTRING_HEADER_ONLY
#define PYSTRING_INLINE inline
#else
#define PYSTRING_INLINE
#endif
#include <string>
#include <vector>
@@ -60,102 +38,102 @@ namespace pystring
//////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Return a copy of the string with only its first character capitalized.
///
std::string capitalize( const std::string & str );
PYSTRING_INLINE std::string capitalize( const std::string & str );
//////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Return centered in a string of length width. Padding is done using spaces.
///
std::string center( const std::string & str, int width );
PYSTRING_INLINE std::string center( const std::string & str, int width );
//////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Return the number of occurrences of substring sub in string S[start:end]. Optional
/// arguments start and end are interpreted as in slice notation.
///
int count( const std::string & str, const std::string & substr, int start = 0, int end = MAX_32BIT_INT);
PYSTRING_INLINE int count( const std::string & str, const std::string & substr, int start = 0, int end = MAX_32BIT_INT);
//////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Return True if the string ends with the specified suffix, otherwise return False. With
/// optional start, test beginning at that position. With optional end, stop comparing at that position.
///
bool endswith( const std::string & str, const std::string & suffix, int start = 0, int end = MAX_32BIT_INT );
PYSTRING_INLINE bool endswith( const std::string & str, const std::string & suffix, int start = 0, int end = MAX_32BIT_INT );
//////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Return a copy of the string where all tab characters are expanded using spaces. If tabsize
/// is not given, a tab size of 8 characters is assumed.
///
std::string expandtabs( const std::string & str, int tabsize = 8);
PYSTRING_INLINE std::string expandtabs( const std::string & str, int tabsize = 8);
//////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Return the lowest index in the string where substring sub is found, such that sub is
/// contained in the range [start, end). Optional arguments start and end are interpreted as
/// in slice notation. Return -1 if sub is not found.
///
int find( const std::string & str, const std::string & sub, int start = 0, int end = MAX_32BIT_INT );
PYSTRING_INLINE int find( const std::string & str, const std::string & sub, int start = 0, int end = MAX_32BIT_INT );
//////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Synonym of find right now. Python version throws exceptions. This one currently doesn't
///
int index( const std::string & str, const std::string & sub, int start = 0, int end = MAX_32BIT_INT );
PYSTRING_INLINE int index( const std::string & str, const std::string & sub, int start = 0, int end = MAX_32BIT_INT );
//////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Return true if all characters in the string are alphanumeric and there is at least one
/// character, false otherwise.
///
bool isalnum( const std::string & str );
PYSTRING_INLINE bool isalnum( const std::string & str );
//////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Return true if all characters in the string are alphabetic and there is at least one
/// character, false otherwise
///
bool isalpha( const std::string & str );
PYSTRING_INLINE bool isalpha( const std::string & str );
//////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Return true if all characters in the string are digits and there is at least one
/// character, false otherwise.
///
bool isdigit( const std::string & str );
PYSTRING_INLINE bool isdigit( const std::string & str );
//////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Return true if all cased characters in the string are lowercase and there is at least one
/// cased character, false otherwise.
///
bool islower( const std::string & str );
PYSTRING_INLINE bool islower( const std::string & str );
//////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Return true if there are only whitespace characters in the string and there is at least
/// one character, false otherwise.
///
bool isspace( const std::string & str );
PYSTRING_INLINE bool isspace( const std::string & str );
//////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Return true if the string is a titlecased string and there is at least one character,
/// i.e. uppercase characters may only follow uncased characters and lowercase characters only
/// cased ones. Return false otherwise.
///
bool istitle( const std::string & str );
PYSTRING_INLINE bool istitle( const std::string & str );
//////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Return true if all cased characters in the string are uppercase and there is at least one
/// cased character, false otherwise.
///
bool isupper( const std::string & str );
PYSTRING_INLINE bool isupper( const std::string & str );
//////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Return a string which is the concatenation of the strings in the sequence seq.
/// The separator between elements is the str argument
///
std::string join( const std::string & str, const std::vector< std::string > & seq );
PYSTRING_INLINE std::string join( const std::string & str, const std::vector< std::string > & seq );
//////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Return the string left justified in a string of length width. Padding is done using
/// spaces. The original string is returned if width is less than str.size().
///
std::string ljust( const std::string & str, int width );
PYSTRING_INLINE std::string ljust( const std::string & str, int width );
//////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Return a copy of the string converted to lowercase.
///
std::string lower( const std::string & str );
PYSTRING_INLINE std::string lower( const std::string & str );
//////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Return a copy of the string with leading characters removed. If chars is omitted or None,
@@ -163,40 +141,64 @@ namespace pystring
/// characters in the string will be stripped from the beginning of the string this method
/// is called on (argument "str" ).
///
std::string lstrip( const std::string & str, const std::string & chars = "" );
PYSTRING_INLINE std::string lstrip( const std::string & str, const std::string & chars = "" );
//////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Return a copy of the string, concatenated N times, together.
/// Corresponds to the __mul__ operator.
///
PYSTRING_INLINE std::string mul( const std::string & str, int n);
//////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Split the string around first occurance of sep.
/// Three strings will always placed into result. If sep is found, the strings will
/// be the text before sep, sep itself, and the remaining text. If sep is
/// not found, the original string will be returned with two empty strings.
///
void partition( const std::string & str, const std::string & sep, std::vector< std::string > & result );
PYSTRING_INLINE void partition( const std::string & str, const std::string & sep, std::vector< std::string > & result );
inline std::vector< std::string > partition( const std::string & str, const std::string & sep )
{
std::vector< std::string > result;
partition( str, sep, result );
return result;
}
//////////////////////////////////////////////////////////////////////////////////////////////
/// @brief If str starts with prefix return a copy of the string with prefix at the start
/// removed otherwise return an unmodified copy of the string.
///
PYSTRING_INLINE std::string removeprefix( const std::string & str, const std::string & prefix );
//////////////////////////////////////////////////////////////////////////////////////////////
/// @brief If str ends with suffix return a copy of the string with suffix at the end removed
/// otherwise return an unmodified copy of the string.
///
PYSTRING_INLINE std::string removesuffix( const std::string & str, const std::string & suffix );
//////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Return a copy of the string with all occurrences of substring old replaced by new. If
/// the optional argument count is given, only the first count occurrences are replaced.
///
std::string replace( const std::string & str, const std::string & oldstr, const std::string & newstr, int count = -1);
PYSTRING_INLINE std::string replace( const std::string & str, const std::string & oldstr, const std::string & newstr, int count = -1);
//////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Return the highest index in the string where substring sub is found, such that sub is
/// contained within s[start,end]. Optional arguments start and end are interpreted as in
/// slice notation. Return -1 on failure.
///
int rfind( const std::string & str, const std::string & sub, int start = 0, int end = MAX_32BIT_INT );
PYSTRING_INLINE int rfind( const std::string & str, const std::string & sub, int start = 0, int end = MAX_32BIT_INT );
//////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Currently a synonym of rfind. The python version raises exceptions. This one currently
/// does not
///
int rindex( const std::string & str, const std::string & sub, int start = 0, int end = MAX_32BIT_INT );
PYSTRING_INLINE int rindex( const std::string & str, const std::string & sub, int start = 0, int end = MAX_32BIT_INT );
//////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Return the string right justified in a string of length width. Padding is done using
/// spaces. The original string is returned if width is less than str.size().
///
std::string rjust( const std::string & str, int width);
PYSTRING_INLINE std::string rjust( const std::string & str, int width);
//////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Split the string around last occurance of sep.
@@ -204,21 +206,33 @@ namespace pystring
/// be the text before sep, sep itself, and the remaining text. If sep is
/// not found, the original string will be returned with two empty strings.
///
void rpartition( const std::string & str, const std::string & sep, std::vector< std::string > & result );
PYSTRING_INLINE void rpartition( const std::string & str, const std::string & sep, std::vector< std::string > & result );
inline std::vector< std::string > rpartition ( const std::string & str, const std::string & sep )
{
std::vector< std::string > result;
rpartition( str, sep, result );
return result;
}
//////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Return a copy of the string with trailing characters removed. If chars is "", whitespace
/// characters are removed. If not "", the characters in the string will be stripped from the
/// end of the string this method is called on.
///
std::string rstrip( const std::string & str, const std::string & chars = "" );
PYSTRING_INLINE std::string rstrip( const std::string & str, const std::string & chars = "" );
//////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Fills the "result" list with the words in the string, using sep as the delimiter string.
/// If maxsplit is > -1, at most maxsplit splits are done. If sep is "",
/// any whitespace string is a separator.
///
void split( const std::string & str, std::vector< std::string > & result, const std::string & sep = "", int maxsplit = -1);
PYSTRING_INLINE void split( const std::string & str, std::vector< std::string > & result, const std::string & sep = "", int maxsplit = -1);
inline std::vector< std::string > split( const std::string & str, const std::string & sep = "", int maxsplit = -1)
{
std::vector< std::string > result;
split( str, result, sep, maxsplit );
return result;
}
//////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Fills the "result" list with the words in the string, using sep as the delimiter string.
@@ -227,65 +241,213 @@ namespace pystring
/// If maxsplit is > -1, at most maxsplit splits are done. If sep is "",
/// any whitespace string is a separator.
///
void rsplit( const std::string & str, std::vector< std::string > & result, const std::string & sep = "", int maxsplit = -1);
PYSTRING_INLINE void rsplit( const std::string & str, std::vector< std::string > & result, const std::string & sep = "", int maxsplit = -1);
inline std::vector< std::string > rsplit( const std::string & str, const std::string & sep = "", int maxsplit = -1)
{
std::vector< std::string > result;
rsplit( str, result, sep, maxsplit);
return result;
}
//////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Return a list of the lines in the string, breaking at line boundaries. Line breaks
/// are not included in the resulting list unless keepends is given and true.
///
void splitlines( const std::string & str, std::vector< std::string > & result, bool keepends = false );
PYSTRING_INLINE void splitlines( const std::string & str, std::vector< std::string > & result, bool keepends = false );
inline std::vector< std::string > splitlines( const std::string & str, bool keepends = false )
{
std::vector< std::string > result;
splitlines( str, result, keepends);
return result;
}
//////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Return True if string starts with the prefix, otherwise return False. With optional start,
/// test string beginning at that position. With optional end, stop comparing string at that
/// position
///
bool startswith( const std::string & str, const std::string & prefix, int start = 0, int end = MAX_32BIT_INT );
PYSTRING_INLINE bool startswith( const std::string & str, const std::string & prefix, int start = 0, int end = MAX_32BIT_INT );
//////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Return a copy of the string with leading and trailing characters removed. If chars is "",
/// whitespace characters are removed. If given not "", the characters in the string will be
/// stripped from the both ends of the string this method is called on.
///
std::string strip( const std::string & str, const std::string & chars = "" );
PYSTRING_INLINE std::string strip( const std::string & str, const std::string & chars = "" );
//////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Return a copy of the string with uppercase characters converted to lowercase and vice versa.
///
std::string swapcase( const std::string & str );
PYSTRING_INLINE std::string swapcase( const std::string & str );
//////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Return a titlecased version of the string: words start with uppercase characters,
/// all remaining cased characters are lowercase.
///
std::string title( const std::string & str );
PYSTRING_INLINE std::string title( const std::string & str );
//////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Return a copy of the string where all characters occurring in the optional argument
/// deletechars are removed, and the remaining characters have been mapped through the given
/// translation table, which must be a string of length 256.
///
std::string translate( const std::string & str, const std::string & table, const std::string & deletechars = "");
PYSTRING_INLINE std::string translate( const std::string & str, const std::string & table, const std::string & deletechars = "");
//////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Return a copy of the string converted to uppercase.
///
std::string upper( const std::string & str );
PYSTRING_INLINE std::string upper( const std::string & str );
//////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Return the numeric string left filled with zeros in a string of length width. The original
/// string is returned if width is less than str.size().
///
std::string zfill( const std::string & str, int width );
PYSTRING_INLINE std::string zfill( const std::string & str, int width );
//////////////////////////////////////////////////////////////////////////////////////////////
/// @brief function matching python's slice functionality.
///
std::string slice( const std::string & str, int start = 0, int end = MAX_32BIT_INT);
PYSTRING_INLINE std::string slice( const std::string & str, int start = 0, int end = MAX_32BIT_INT);
///
/// @ }
///
}
namespace os
{
namespace path
{
// All of the function below have three versions.
// Example:
// join(...)
// join_nt(...)
// join_posix(...)
//
// The regular function dispatches to the other versions - based on the OS
// at compile time - to match the result you'd get from the python
// interepreter on the same operating system
//
// Should you want to 'lock off' to a particular version of the string
// manipulation across *all* operating systems, use the version with the
// _OS you are interested in. I.e., you can use posix style path joining,
// even on Windows, with join_posix.
//
// The naming, (nt, posix) matches the cpython source implementation.
//////////////////////////////////////////////////////////////////////////////////////////////
/// @defgroup functions pystring::os::path
/// @{
//////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Return the base name of pathname path. This is the second half of the pair returned
/// by split(path). Note that the result of this function is different from the Unix basename
/// program; where basename for '/foo/bar/' returns 'bar', the basename() function returns an
/// empty string ('').
PYSTRING_INLINE std::string basename(const std::string & path);
PYSTRING_INLINE std::string basename_nt(const std::string & path);
PYSTRING_INLINE std::string basename_posix(const std::string & path);
//////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Return the directory name of pathname path. This is the first half of the pair
/// returned by split(path).
PYSTRING_INLINE std::string dirname(const std::string & path);
PYSTRING_INLINE std::string dirname_nt(const std::string & path);
PYSTRING_INLINE std::string dirname_posix(const std::string & path);
//////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Return True if path is an absolute pathname. On Unix, that means it begins with a
/// slash, on Windows that it begins with a (back)slash after chopping off a potential drive
/// letter.
PYSTRING_INLINE bool isabs(const std::string & path);
PYSTRING_INLINE bool isabs_nt(const std::string & path);
PYSTRING_INLINE bool isabs_posix(const std::string & s);
//////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Return a normalized absolutized version of the pathname path.
///
/// NOTE: This differs from the interface of the python equivalent in that it requires you
/// to pass in the current working directory as an argument.
PYSTRING_INLINE std::string abspath(const std::string & path, const std::string & cwd);
PYSTRING_INLINE std::string abspath_nt(const std::string & path, const std::string & cwd);
PYSTRING_INLINE std::string abspath_posix(const std::string & path, const std::string & cwd);
//////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Join one or more path components intelligently. If any component is an absolute
/// path, all previous components (on Windows, including the previous drive letter, if there
/// was one) are thrown away, and joining continues. The return value is the concatenation of
/// path1, and optionally path2, etc., with exactly one directory separator (os.sep) inserted
/// between components, unless path2 is empty. Note that on Windows, since there is a current
/// directory for each drive, os.path.join("c:", "foo") represents a path relative to the
/// current directory on drive C: (c:foo), not c:\foo.
/// This dispatches based on the compilation OS
PYSTRING_INLINE std::string join(const std::string & path1, const std::string & path2);
PYSTRING_INLINE std::string join_nt(const std::string & path1, const std::string & path2);
PYSTRING_INLINE std::string join_posix(const std::string & path1, const std::string & path2);
PYSTRING_INLINE std::string join(const std::vector< std::string > & paths);
PYSTRING_INLINE std::string join_nt(const std::vector< std::string > & paths);
PYSTRING_INLINE std::string join_posix(const std::vector< std::string > & paths);
//////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Normalize a pathname. This collapses redundant separators and up-level references
/// so that A//B, A/B/, A/./B and A/foo/../B all become A/B. It does not normalize the case
/// (use normcase() for that). On Windows, it converts forward slashes to backward slashes.
/// It should be understood that this may change the meaning of the path if it contains
/// symbolic links!
PYSTRING_INLINE std::string normpath(const std::string & path);
PYSTRING_INLINE std::string normpath_nt(const std::string & path);
PYSTRING_INLINE std::string normpath_posix(const std::string & path);
//////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Split the pathname path into a pair, (head, tail) where tail is the last pathname
/// component and head is everything leading up to that. The tail part will never contain a
/// slash; if path ends in a slash, tail will be empty. If there is no slash in path, head
/// will be empty. If path is empty, both head and tail are empty. Trailing slashes are
/// stripped from head unless it is the root (one or more slashes only). In all cases,
/// join(head, tail) returns a path to the same location as path (but the strings may
/// differ).
PYSTRING_INLINE void split(std::string & head, std::string & tail, const std::string & path);
PYSTRING_INLINE void split_nt(std::string & head, std::string & tail, const std::string & path);
PYSTRING_INLINE void split_posix(std::string & head, std::string & tail, const std::string & path);
//////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Split the pathname path into a pair (drive, tail) where drive is either a drive
/// specification or the empty string. On systems which do not use drive specifications,
/// drive will always be the empty string. In all cases, drive + tail will be the same as
/// path.
PYSTRING_INLINE void splitdrive(std::string & drivespec, std::string & pathspec, const std::string & path);
PYSTRING_INLINE void splitdrive_nt(std::string & drivespec, std::string & pathspec, const std::string & p);
PYSTRING_INLINE void splitdrive_posix(std::string & drivespec, std::string & pathspec, const std::string & path);
//////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Split the pathname path into a pair (root, ext) such that root + ext == path, and
/// ext is empty or begins with a period and contains at most one period. Leading periods on
/// the basename are ignored; splitext('.cshrc') returns ('.cshrc', '').
PYSTRING_INLINE void splitext(std::string & root, std::string & ext, const std::string & path);
PYSTRING_INLINE void splitext_nt(std::string & root, std::string & ext, const std::string & path);
PYSTRING_INLINE void splitext_posix(std::string & root, std::string & ext, const std::string & path);
///
/// @ }
///
} // namespace path
} // namespace os
} // namespace pystring
#if PYSTRING_HEADER_ONLY
#include "pystring_impl.h"
#endif
#endif
+1693
View File
File diff suppressed because it is too large Load Diff
+1300
View File
File diff suppressed because it is too large Load Diff
+5
View File
@@ -0,0 +1,5 @@
// test_header_only_mode.cpp
#ifndef PYSTRING_HEADER_ONLY
#error "PYSTRING_HEADER_ONLY must be defined when using the header-only target"
#endif
int main() { return 0; }
+125
View File
@@ -0,0 +1,125 @@
// Copyright Contributors to the Pystring project.
// SPDX-License-Identifier: BSD-3-Clause
// https://github.com/imageworks/pystring/blob/master/LICENSE
#ifndef INCLUDED_PYSTRING_UNITTEST_H
#define INCLUDED_PYSTRING_UNITTEST_H
#include <iostream>
#include <cmath>
#include <vector>
extern int unit_test_failures;
void unittest_fail();
typedef void (*PYSTRINGTestFunc)();
struct PYSTRINGTest
{
PYSTRINGTest(std::string testgroup, std::string testname, PYSTRINGTestFunc test) :
group(testgroup), name(testname), function(test) { };
std::string group, name;
PYSTRINGTestFunc function;
};
typedef std::vector<PYSTRINGTest> UnitTests;
UnitTests& GetUnitTests();
struct AddTest { AddTest(PYSTRINGTest&& test); };
/// PYSTRING_CHECK_* macros checks if the conditions is met, and if not,
/// prints an error message indicating the module and line where the
/// error occurred, but does NOT abort. This is helpful for unit tests
/// where we do not want one failure.
#define PYSTRING_CHECK_ASSERT(x) \
((x) ? ((void)0) \
: ((std::cout << __FILE__ << ":" << __LINE__ << ":\n" \
<< "FAILED: " << #x << "\n"), \
(void)++unit_test_failures))
#define PYSTRING_CHECK_EQUAL(x,y) \
(((x) == (y)) ? ((void)0) \
: ((std::cout << __FILE__ << ":" << __LINE__ << ":\n" \
<< "FAILED: " << #x << " == " << #y << "\n" \
<< "\tvalues were '" << (x) << "' and '" << (y) << "'\n"), \
(void)++unit_test_failures))
#define PYSTRING_CHECK_NE(x,y) \
(((x) != (y)) ? ((void)0) \
: ((std::cout << __FILE__ << ":" << __LINE__ << ":\n" \
<< "FAILED: " << #x << " != " << #y << "\n" \
<< "\tvalues were '" << (x) << "' and '" << (y) << "'\n"), \
(void)++unit_test_failures))
#define PYSTRING_CHECK_LT(x,y) \
(((x) < (y)) ? ((void)0) \
: ((std::cout << __FILE__ << ":" << __LINE__ << ":\n" \
<< "FAILED: " << #x << " < " << #y << "\n" \
<< "\tvalues were '" << (x) << "' and '" << (y) << "'\n"), \
(void)++unit_test_failures))
#define PYSTRING_CHECK_GT(x,y) \
(((x) > (y)) ? ((void)0) \
: ((std::cout << __FILE__ << ":" << __LINE__ << ":\n" \
<< "FAILED: " << #x << " > " << #y << "\n" \
<< "\tvalues were '" << (x) << "' and '" << (y) << "'\n"), \
(void)++unit_test_failures))
#define PYSTRING_CHECK_LE(x,y) \
(((x) <= (y)) ? ((void)0) \
: ((std::cout << __FILE__ << ":" << __LINE__ << ":\n" \
<< "FAILED: " << #x << " <= " << #y << "\n" \
<< "\tvalues were '" << (x) << "' and '" << (y) << "'\n"), \
(void)++unit_test_failures))
#define PYSTRING_CHECK_GE(x,y) \
(((x) >= (y)) ? ((void)0) \
: ((std::cout << __FILE__ << ":" << __LINE__ << ":\n" \
<< "FAILED: " << #x << " >= " << #y << "\n" \
<< "\tvalues were '" << (x) << "' and '" << (y) << "'\n"), \
(void)++unit_test_failures))
#define PYSTRING_CHECK_CLOSE(x,y,tol) \
((std::abs((x) - (y)) < tol) ? ((void)0) \
: ((std::cout << __FILE__ << ":" << __LINE__ << ":\n" \
<< "FAILED: abs(" << #x << " - " << #y << ") < " << #tol << "\n" \
<< "\tvalues were '" << (x) << "', '" << (y) << "' and '" << (tol) << "'\n"), \
(void)++unit_test_failures))
#define PYSTRING_CHECK_THOW(S, E) \
try { S; throw "throwanything"; } catch( E const& ex ) { } catch (...) { \
std::cout << __FILE__ << ":" << __LINE__ << ":\n" \
<< "FAILED: " << #E << " is expected to be thrown\n"; \
++unit_test_failures; }
#define PYSTRING_CHECK_NO_THOW(S) \
try { S; } catch (...) { \
std::cout << __FILE__ << ":" << __LINE__ << ":\n" \
<< "FAILED: exception thrown from " << #S <<"\n"; \
++unit_test_failures; }
#define PYSTRING_ADD_TEST(group, name) \
static void pystringtest_##group##_##name(); \
AddTest pystringaddtest_##group##_##name(PYSTRINGTest(#group, #name, pystringtest_##group##_##name)); \
static void pystringtest_##group##_##name()
#define PYSTRING_TEST_SETUP() \
int unit_test_failures = 0
#define PYSTRING_TEST_APP(app) \
std::vector<PYSTRINGTest>& GetUnitTests() { \
static std::vector<PYSTRINGTest> pystring_unit_tests; \
return pystring_unit_tests; } \
AddTest::AddTest(PYSTRINGTest&& test){GetUnitTests().emplace_back(test);} \
PYSTRING_TEST_SETUP(); \
int main(int, char **) { std::cerr << "\n" << #app <<"\n\n"; \
for(size_t i = 0; i < GetUnitTests().size(); ++i) { \
int _tmp = unit_test_failures; GetUnitTests()[i].function(); \
std::cerr << "Test [" << GetUnitTests()[i].group << "] [" << GetUnitTests()[i].name << "] - "; \
std::cerr << (_tmp == unit_test_failures ? "PASSED" : "FAILED") << "\n"; } \
std::cerr << "\n" << unit_test_failures << " tests failed\n\n"; \
return unit_test_failures; }
#endif