string_view lite - A C++17-like string_view for C++98, C++11 and later in a single-file header-only library

Overview

string_view lite: A single-file header-only version of a C++17-like string_view for C++98, C++11 and later

Language License Build Status Build status Version download Conan Try it on wandbox Try it on godbolt online

Contents

Example usage

#include "nonstd/string_view.hpp"
#include <iostream>

using namespace std::literals;
using namespace nonstd::literals;
using namespace nonstd;

void write( string_view sv )
{
    std::cout << sv;
}

int main()
{
    write( "hello"     );   // C-string
    write( ", "s       );   // std::string
    write( "world!"_sv );   // nonstd::string_view
}

Compile and run

prompt> g++ -Wall -std=c++14 -I../include/ -o 01-basic.exe 01-basic.cpp && 01-basic.exe
hello, world!

In a nutshell

string-view lite is a single-file header-only library to provide a non-owning reference to a string. The library provides a C++17-like string_view for use with C++98 and later. If available, std::string_view is used, unless configured otherwise.

Features and properties of string-view lite are ease of installation (single header), freedom of dependencies other than the standard library. To mimic C++17-like cooperation with std::string, nonstd::string_view provides several non-standard conversion functions. These functions may be omitted via configuration.

License

string-view lite is distributed under the Boost Software License.

Dependencies

string-view lite has no other dependencies than the C++ standard library.

Installation

string-view lite is a single-file header-only library. Put string_view.hpp in the include folder directly into the project source tree or somewhere reachable from your project.

Or, if you use the conan package manager, follow these steps:

  1. Add nonstd-lite to the conan remotes:

     conan remote add nonstd-lite https://api.bintray.com/conan/martinmoene/nonstd-lite
    
  2. Add a reference to string-view-lite to the requires section of your project's conanfile.txt file:

     [requires]
     string-view-lite/[~=1]@nonstd-lite/testing
    
  3. Run conan's install command:

     conan install
    

Synopsis

Contents
Documentation of std::string_view
C++20 extensions
Non-standard extensions
Configuration

Documentation of std::string_view

Depending on the compiler and C++-standard used, nonstd::string_view behaves less or more like std::string_view. To get an idea of the capabilities of nonstd::string_view with your configuration, look at the output of the tests, issuing string-view-lite.t --pass @. For std::string_view, see its documentation at cppreference.

C++20 extensions

string_view-lite provides the following C++20 extensions.

  • [[nodiscard]] constexpr bool empty() const noexcept;
  • constexpr bool starts_with( basic_string_view v ) const noexcept; // (1)
  • constexpr bool starts_with( CharT c ) const noexcept; // (2)
  • constexpr bool starts_with( CharT const * s ) const; // (3)
  • constexpr bool ends_with( basic_string_view v ) const noexcept; // (1)
  • constexpr bool ends_with( CharT c ) const noexcept; // (2)
  • constexpr bool ends_with( CharT const * s ) const; // (3)

Note: [[nodiscard]], constexpr and noexcept if available.

Non-standard extensions

string_view literals sv and _sv

clang compilers do not allow to write auto sv = "..."sv with string-view lite under C++11. To still provide a literal operator that can be used in that case, string-view lite also provides _sv. See section Configuration for how to control the presence of these operators.

The literal operators are declared in the namespace nonstd::literals::string_view_literals, where both literals and string_view_literals are inline namespaces, if supported. Access to these operators can be gained with using namespace nonstd::literals, using namespace nonstd::string_view_literals, and using namespace nonstd::literals::string_view_literals. If inline namespaces are not supported by the compiler, only the latter form is available.

Cooperation between std::string and nonstd::string_view

string-view lite can provide several methods and free functions to mimic the cooperation between std::string and nonstd::string_view that exists in C++17. See the table below. Several macros allow you to control the presence of these functions, see section Configuration.

Kind Std Function or method
Free functions   macro nssv_CONFIG_CONVERSION_STD_STRING_FREE_FUNCTIONS
std::string_view    
to_string() >=C++17 template< class CharT, class Traits, class Allocator=std::allocator<CharT> >
std::basic_string<CharT, Traits, Allocator>
to_string( std::basic_string_view<CharT, Traits> v, Allocator const & a=Allocator() );
to_string_view() >=C++17 template< class CharT, class Traits, class Allocator >
std::basic_string_view<CharT, Traits>
to_string_view( std::basic_string<CharT, Traits, Allocator> const & s );
nonstd::string_view    
to_string()
 
non-msvc14 (vs2015)
>=C++11 template< class CharT, class Traits, class Allocator = std::allocator<CharT> >
std::basic_string<CharT, Traits, Allocator>
to_string( basic_string_view<CharT, Traits> v, Allocator const & a = Allocator() );
to_string()
 
msvc14 (vs2015)
<C++11 template< class CharT, class Traits >
std::basic_string<CharT, Traits>
to_string( basic_string_view<CharT, Traits> v );
to_string()
 
msvc14 (vs2015)
<C++11 template< class CharT, class Traits, class Allocator >
std::basic_string<CharT, Traits, Allocator>
to_string( basic_string_view<CharT, Traits> v, Allocator const & a );
to_string_view() >=C++98 template< class CharT, class Traits, class Allocator >
basic_string_view<CharT, Traits>
to_string_view( std::basic_string<CharT, Traits, Allocator> const & s );
     
Class methods   macro nssv_CONFIG_CONVERSION_STD_STRING_CLASS_METHODS
nonstd::basic_string_view    
Constructor >=C++98 template< class Allocator >
basic_string_view( std::basic_string<CharT, Traits, Allocator> const & s ) nssv_noexcept;
Converting operator >=C++11 template< class Allocator >
explicit operator std::basic_string<CharT, Traits, Allocator>() const;
to_string() >=C++11 template< class Allocator = std::allocator<CharT> >
std::basic_string<CharT, Traits, Allocator>
to_string( Allocator const & a = Allocator() ) const;
to_string() <C++11 std::basic_string<CharT, Traits>
to_string() const;
to_string() <C++11 template< class Allocator >
std::basic_string<CharT, Traits, Allocator>
to_string( Allocator const & a ) const;
     
Literal operator sv >=C++11 macro nssv_CONFIG_STD_SV_OPERATOR
    constexpr string_view operator "" sv( const char* str, size_t len ) noexcept;
    constexpr u16string_view operator "" sv( const char16_t* str, size_t len ) noexcept;
    constexpr u32string_view operator "" sv( const char32_t* str, size_t len ) noexcept;
    constexpr wstring_view operator "" sv( const wchar_t* str, size_t len ) noexcept;
     
Literal operator _sv >=C++11 macro nssv_CONFIG_USR_SV_OPERATOR
    constexpr string_view operator "" _sv( const char* str, size_t len ) noexcept;
    constexpr u16string_view operator "" _sv( const char16_t* str, size_t len ) noexcept;
    constexpr u32string_view operator "" _sv( const char32_t* str, size_t len ) noexcept;
    constexpr wstring_view operator "" _sv( const wchar_t* str, size_t len ) noexcept;

Configuration

Tweak header

If the compiler supports __has_include(), string-view lite supports the tweak header mechanism. Provide your tweak header as nonstd/string_view.tweak.hpp in a folder in the include-search-path. In the tweak header, provide definitions as documented below, like #define nssv_CONFIG_NO_EXCEPTIONS 1.

Standard selection macro

-Dnssv_CPLUSPLUS=199711L
Define this macro to override the auto-detection of the supported C++ standard, if your compiler does not set the __cpluplus macro correctly.

Select std::string_view or nonstd::string_view

At default, string-view lite uses std::string_view if it is available and lets you use it via namespace nonstd. You can however override this default and explicitly request to use std::string_view as nonstd::string_view or use string-view lite's nonstd::string_view via the following macros.

-Dnssv_CONFIG_SELECT_STRING_VIEW=nssv_STRING_VIEW_DEFAULT
Define this to nssv_STRING_VIEW_STD to select std::string_view as nonstd::string_view. Define this to nssv_STRING_VIEW_NONSTD to select nonstd::string_view as nonstd::string_view. Default is undefined, which has the same effect as defining to nssv_STRING_VIEW_DEFAULT.

Note: nssv_CONFIG_SELECT_STD_STRING_VIEW and nssv_CONFIG_SELECT_NONSTD_STRING_VIEW are deprecated and have been removed.

Disable exceptions

-Dnssv_CONFIG_NO_EXCEPTIONS=0 Define this to 1 if you want to compile without exceptions. If not defined, the header tries and detect if exceptions have been disabled (e.g. via -fno-exceptions). Default is undefined.

Add or omit literal operators sv and _sv

-Dnssv_CONFIG_STD_SV_OPERATOR=1
Define this to 1 to provide literal operator sv to create a string_view from a literal string. Default is 0. Note that literal operators without leading underscore are reserved for the C++ standard.

-Dnssv_CONFIG_USR_SV_OPERATOR=0
Define this to 0 to omit literal operator _sv to create a string_view from a literal string. Default is 1.

Omit cooperation between std::stringnonstd::string_view

At default, string-view lite provides several methods and free functions to mimic the cooperation between std::string and nonstd::string_view (or std::string_view) that exists in C++17. See section Non-standard extensions. The following macros allow you to control the presence of these functions.

-Dnssv_CONFIG_CONVERSION_STD_STRING=1
Define this to 1 to provide std::stringnonstd::string_view interoperability via methods of nonstd::basic_string_view and free functions, define it to 0 to omit all said methods and functions. Default is undefined.

-Dnssv_CONFIG_CONVERSION_STD_STRING_CLASS_METHODS=1
Define this to 1 to provide std::stringnonstd::string_view interoperability via methods of nonstd::basic_string_view, define it to 0 to omit all said methods. Default is undefined.

-Dnssv_CONFIG_CONVERSION_STD_STRING_FREE_FUNCTIONS=1
Define this to 1 to provide std::stringnonstd::string_view interoperability via free functions, define it to 0 to omit all said functions. This also controls the presence of these function if std::string_view is used. Default is undefined.

Omit use of streams

At default, string-view lite provides operations to overload the operator<<. If you want to use the library without the use of standard streams, you can control this with the following macro:

-Dnssv_CONFIG_NO_STREAM_INSERTION=1
Define this to 1 to omit the use of standard streams. Default is undefined

Reported to work with

The table below mentions the compiler versions string-view lite is reported to work with.

OS Compiler Versions
Windows Clang/LLVM ?
  GCC 7.2.0
  Visual C++
(Visual Studio)
8 (2005), 9 (2008), 10 (2010), 11 (2012),
12 (2013), 14 (2015), 15 (2017)
GNU/Linux Clang/LLVM 3.5 - 6.0
  GCC 4.8 - 8
OS X Clang/LLVM Xcode 6, Xcode 7, Xcode 8, Xcode 9

Building the tests

To build the tests you need:

The lest test framework is included in the test folder.

The following steps assume that the string-view lite source code has been cloned into a directory named c:\string-view-lite.

  1. Create a directory for the build outputs for a particular architecture. Here we use c:\string-view-lite\build-win-x86-vc10.

     cd c:\string-view-lite
     md build-win-x86-vc10
     cd build-win-x86-vc10
    
  2. Configure CMake to use the compiler of your choice (run cmake --help for a list).

     cmake -G "Visual Studio 10 2010" -DSTRING_VIEW_LITE_OPT_BUILD_TESTS=ON ..
    
  3. Build the test suite in the Debug configuration (alternatively use Release).

     cmake --build . --config Debug
    
  4. Run the test suite.

     ctest -V -C Debug
    

All tests should pass, indicating your platform is supported and you are ready to use string-view lite.

Other implementations of string_view

Notes and references

Interface and specification

Presentations

Proposals

Appendix

A.1 Compile-time information

The version of string-view lite is available via tag [.version]. The following tags are available for information on the compiler and on the C++ standard library used: [.compiler], [.builtins], [.stdc++], [.stdlanguage] and [.stdlibrary].

A.2 string-view lite test specification

string_view: Allows to default construct an empty string_view
string_view: Allows to construct from pointer and size
string_view: Allows to construct from C-string
string_view: Allows to copy-construct from empty string_view
string_view: Allows to copy-construct from non-empty string_view
string_view: Allows to copy-assign from empty string_view
string_view: Allows to copy-assign from non-empty string_view
string_view: Allows forward iteration
string_view: Allows const forward iteration
string_view: Allows reverse iteration
string_view: Allows const reverse iteration
string_view: Allows to obtain the size of the view via size()
string_view: Allows to obtain the size of the view via length()
string_view: Allows to obtain the maximum size a view can be via max_size()
string_view: Allows to check for an empty string_view via empty()
string_view: Allows to observe an element via array indexing
string_view: Allows to observe an element via at()
string_view: Throws at observing an element via at() with an index of size() or larger
string_view: Allows to observe elements via data()
string_view: Yields nullptr (or NULL) with data() for an empty string_view
string_view: Allows to remove a prefix of n elements
string_view: Allows to remove a suffix of n elements
string_view: Allows to swap with other string_view
string_view: Allows to copy a substring of length n, starting at position pos (default: 0) via copy()
string_view: Throws if requested position of copy() exceeds string_view's size()
string_view: Allow to obtain a sub string, starting at position pos (default: 0) and of length n (default full), via substr()
string_view: Throws if requested position of substr() exceeds string_view's size()
string_view: Allows to lexically compare to another string_view via compare(), (1)
string_view: Allows to compare empty string_views as equal via compare(), (1)
string_view: Allows to constexpr-compare string_views via compare(), (1) (C++14)
string_view: Allows to compare a sub string to another string_view via compare(), (2)
string_view: Allows to compare a sub string to another string_view sub string via compare(), (3)
string_view: Allows to compare to a C-string via compare(), (4)
string_view: Allows to compare a sub string to a C-string via compare(), (5)
string_view: Allows to compare a sub string to a C-string prefix via compare(), (6)
string_view: Allows to check for a prefix string_view via starts_with(), (1)
string_view: Allows to check for a prefix character via starts_with(), (2)
string_view: Allows to check for a prefix C-string via starts_with(), (3)
string_view: Allows to check for a suffix string_view via ends_with(), (1)
string_view: Allows to check for a suffix character via ends_with(), (2)
string_view: Allows to check for a suffix C-string via ends_with(), (3)
string_view: Allows to search for a string_view substring, starting at position pos (default: 0) via find(), (1)
string_view: Allows to search for a character, starting at position pos (default: 0) via find(), (2)
string_view: Allows to search for a C-string substring, starting at position pos and of length n via find(), (3)
string_view: Allows to search for a C-string substring, starting at position pos (default: 0) via find(), (4)
string_view: Allows to search backwards for a string_view substring, starting at position pos (default: npos) via rfind(), (1)
string_view: Allows to search backwards for a character, starting at position pos (default: npos) via rfind(), (2)
string_view: Allows to search backwards for a C-string substring, starting at position pos and of length n via rfind(), (3)
string_view: Allows to search backwards for a C-string substring, starting at position pos (default: 0) via rfind(), (4)
string_view: Allows to search for the first occurrence of any of the characters specified in a string view, starting at position pos (default: 0) via find_first_of(), (1)
string_view: Allows to search for a character, starting at position pos (default: 0) via find_first_of(), (2)
string_view: Allows to search for the first occurrence of any of the characters specified in a C-string, starting at position pos and of length n via find_first_of(), (3)
string_view: Allows to search for the first occurrence of any of the characters specified in a C-string, starting at position pos via find_first_of(), (4)
string_view: Allows to search backwards for the last occurrence of any of the characters specified in a string view, starting at position pos (default: npos) via find_last_of(), (1)
string_view: Allows to search backwards for a character, starting at position pos (default: 0) via find_last_of(), (2)
string_view: Allows to search backwards for the first occurrence of any of the characters specified in a C-string, starting at position pos and of length n via find_last_of(), (3)
string_view: Allows to search backwards for the first occurrence of any of the characters specified in a C-string, starting at position pos via find_last_of(), (4)
string_view: Allows to search for the first character not specified in a string view, starting at position pos (default: 0) via find_first_not_of(), (1)
string_view: Allows to search for the first character not equal to the specified character, starting at position pos (default: 0) via find_first_not_of(), (2)
string_view: Allows to search for  the first character not equal to any of the characters specified in a C-string, starting at position pos and of length n via find_first_not_of(), (3)
string_view: Allows to search for  the first character not equal to any of the characters specified in a C-string, starting at position pos via find_first_not_of(), (4)
string_view: Allows to search backwards for the first character not specified in a string view, starting at position pos (default: npos) via find_last_not_of(), (1)
string_view: Allows to search backwards for the first character not equal to the specified character, starting at position pos (default: npos) via find_last_not_of(), (2)
string_view: Allows to search backwards for  the first character not equal to any of the characters specified in a C-string, starting at position pos and of length n via find_last_not_of(), (3)
string_view: Allows to search backwards for  the first character not equal to any of the characters specified in a C-string, starting at position pos via find_last_not_of(), (4)
string_view: Allows to create a string_view, wstring_view, u16string_view, u32string_view via literal "sv"
string_view: Allows to create a string_view via literal "sv", using namespace nonstd::literals::string_view_literals
string_view: Allows to create a string_view via literal "sv", using namespace nonstd::string_view_literals
string_view: Allows to create a string_view via literal "sv", using namespace nonstd::literals
string_view: Allows to create a string_view, wstring_view, u16string_view, u32string_view via literal "_sv"
string_view: Allows to create a string_view via literal "_sv", using namespace nonstd::literals::string_view_literals
string_view: Allows to create a string_view via literal "_sv", using namespace nonstd::string_view_literals
string_view: Allows to create a string_view via literal "_sv", using namespace nonstd::literals
string_view: Allows to compare a string_view with another string_view via comparison operators
string_view: Allows to compare a string_view with an object with implicit conversion to string_view via comparison operators
string_view: Allows to compare empty string_view-s as equal via compare() and via operator==()
operator<<: Allows printing a string_view to an output stream
std::hash<>: Hash value of string_view equals hash value of corresponding string object
std::hash<>: Hash value of wstring_view equals hash value of corresponding string object
std::hash<>: Hash value of u16string_view equals hash value of corresponding string object
std::hash<>: Hash value of u32string_view equals hash value of corresponding string object
string_view: construct from std::string [extension]
string_view: convert to std::string via explicit operator [extension]
string_view: convert to std::string via to_string() [extension]
to_string(): convert to std::string via to_string() [extension]
to_string_view(): convert from std::string via to_string_view() [extension]
tweak header: reads tweak header if supported [tweak]
Comments
  • Non constexpr operator==

    Non constexpr operator==

    First of all, great work. This is probably the most thorough and streamlined backport of string_view publicly available, and I thank you for making it such.

    I'm using it to replace Google's Abseil implementation, which still has some rough edges, and certainly drags in a lot of baggage for someone who just needs string_view.

    One of my biggest motivations for using string_view is to facilitate string keyed associative containers with compile-time lookup, which opens the door to many useful things with zero runtime overhead. However, this requires constexpr comparison to be available... This string_view delegates operator== to char_traits::compare() which is part of std, and as such is required NOT to be marked constexpr in pre-C++17 compilation. This is despite the compiler being perfectly able to perform constexpr comparisons.

    In my case then, there is little value in backporting string_view if it doesn't backport its benefits too.

    My workaround solution for this was deriving my own char_traits to implement a constexpr compare(), and then deriving my own string_view, as shown below. Perhaps you can think of a better way to backport constexpr comparison.

    Thx

    namespace my_own {
        namespace _detail {
            struct char_traits : public std::char_traits<char> {
                static constexpr int compare( const char_type* s1, const char_type* s2, std::size_t count ) {
                    ... compiler specific ...
                }
            };
            using plain_string_view = nonstd::basic_string_view<char>;
        }
        class string_view : public nonstd::basic_string_view<char, _detail::char_traits> {
            using Base = nonstd::basic_string_view<char, _detail::char_traits>;
        public:
            using Base::Base;
            // need to provide own implicit conversions with std::string, since it uses the default char_traits
            operator string() const noexcept { return string(data(), size()); }
            string_view(string const & s) noexcept : Base(s.data(), s.size()) {}
            // provide implicit conversions with a string view that uses standard char_traits
            constexpr operator _detail::plain_string_view() const {return {data(), size()};}
            constexpr string_view(_detail::plain_string_view p) : Base(p.data(), p.size()) {}
        };
        inline std::ostream & operator<<(std::ostream & os, string_view sv) { return os << _detail::plain_string_view(sv); }
    } // namespace
    
    opened by mcskatkat 25
  • added define to build without streams

    added define to build without streams

    Hello! Thanks for your great library.

    I need to use your library in an Intel SGX Enclave where we can't use standard streams (for security reasons).

    In order to use your library I needed to exclude those lines from the header

    opened by theShmoo 8
  • Error with incomplete type when compiling using NEC compiler

    Error with incomplete type when compiling using NEC compiler

    Hello,

    we are using string_view_lite to support the functionality in the GROMACS project (https://gitlab.com/gromacs/gromacs) for the current release before we are making the actual switch to C++17 and native support.

    We recently got a report (https://gitlab.com/gromacs/gromacs/-/issues/3533) that the compilation of our code fails in the string_view compatibility layer with the following error when using the NEC compiler

    "/gromacs-2020.1/src/gromacs/compat/string_view.h", line 894: error: incomplete type is not allowed
    const basic_string_view v;
    

    Are you aware of other compilers having similar issues?

    Thanks for the help!

    opened by acmnpv 5
  • Literal string constructors should be marked noexcept

    Literal string constructors should be marked noexcept

    If you declare a string constant in the global space you might see clang-tidy showing the warning that "Initialization of X with static storage duration may throw an exception that cannot be caught". Actually initialization of the constant literal string never throws an exception.

    Example:

    const char* path = "file:///path/to/file.ext";
    

    To fix it the following constructors should be marked noexcept:

    nssv_constexpr basic_string_view( CharT const * s, size_type count ) nssv_noexcept
    
    nssv_constexpr basic_string_view( CharT const * s) nssv_noexcept
    

    By the way Traits::length(s) is marked constexpr starting from C++17. So the following constructor can only be marked constexpr for C++17+.

    nssv_constexpr basic_string_view( CharT const * s)
        : data_( s )
        , size_( Traits::length(s) )
    {}
    
    opened by xirius 5
  • Do the work

    Do the work

    ... quoting Kate Gregory, Cpp2017.

    • [x] Create initial code files string_view.hpp end test files and compilation command.
    • [x] Create class template basic_string_view with method declarations.
    • [x] Implement constructors
    • [x] Implement assignment
    • [x] Implement iterators
    • [x] Implement element access
    • [x] Implement capacity
    • [x] Implement modifiers
    • [x] Implement operations
    • [x] Implement string conversion operations, class methods (optional extension)
    • [x] Implement string conversion operations, free functions (optional extension)
    • [x] Implement comparisons, primary
    • [x] Implement comparisons, extended
    • [x] Implement streaming
    • [x] Implement "sv" literals
    • [x] Implement std::hash<>
    • [x] Setup Travis
    • [x] Setup AppVeyor
    • [x] Setup 'on Wandbox'
    • [x] Setup 'on Godbolt'
    • [x] Create Readme
    • [x] Fine-tune Readme
    • [x] Release 0.2.0
    • [x] Setup 'on conan' (@agauniyal ?)
    • [x] Update Readme

    Build Status Build status

    Interface and implementation

    Other implementations on GitHub

    Presentations

    Proposals

    opened by martinmoene 5
  • -Wshadow fails

    -Wshadow fails

    It would be good if the header was compatible with -Wshadow. It has one error:

    nssv_constexpr explicit not_in_view( basic_string_view v ) : v( v ) {} string_view.hpp:858:68: warning: declaration of ‘v’ shadows a member of ‘nonstd::sv_lite::basic_string_view<CharT, Traits>::not_in_view’ [-Wshadow]

    The local argument just needs to be changed:

    nssv_constexpr explicit not_in_view( basic_string_view view ) : v( view ) {}

    opened by mattyclarkson 4
  • Fixes #48 - make find() constexpr in C++11

    Fixes #48 - make find() constexpr in C++11

    A naive implementation of a constexpr alternative to std::search() for pairs of basic_string_views, using the basic_string_view::starts_with() method.

    Note: Have not implemented this for rfind(); it would make sense to do both. Have also not touched the copyright notice.

    opened by eyalroz 3
  • Wrong check for stream vs sentry

    Wrong check for stream vs sentry

    In the write_to_stream method, the check on line 1384 is testing the stream.

        if ( !os )
            return os;
    

    Should this check test the sentry instead?

      if (!sentry) {
        return os;
      }
    
    opened by mprather 3
  • Comparison to C string doesn't compile with VC 2010

    Comparison to C string doesn't compile with VC 2010

    This doesn't compile with VC 2010:

    string_view s;
    s == "";
    

    Need to write s == string_view("");

    This looks like a problem of the compiler that's probably hard to work around.

    opened by ikolev21 3
  • CMake Release?

    CMake Release?

    Hey, big fan! Currently I'm using optional-lite, span-lite, and string-view-lite. I noticed you recently updated optional and span to support a CMake's find_package feature. It would be incredibly useful for me if you also updated string-view-lite, since it lets me throw out my hand-rolled find_package for these libraries.

    No worries if this isn't a high priority!

    opened by AndrewGaspar 3
  • Compile errors with clang on Windows

    Compile errors with clang on Windows

    When building with clang on Windows using nssv_CONFIG_SELECT_STRING_VIEW=nssv_STRING_VIEW_NONSTD, you can get compilation errors due to multiple functions defined by string-view-lite mapping to the same mangled symbol name.

    This does not occur when building with MSVC, because in that case string-view-lite ensures that the operator== functions have different signatures (see nssv_MSVC_ORDER).

    $ cat >test.cpp <<EOF
    #include "nonstd/string_view.hpp"
    // Ensure both operator== functions are instantiated
    int foo() { return "" == nonstd::string_view(""); }
    int bar() { return nonstd::string_view("") == ""; }
    EOF
    
    $  clang-cl -Dnssv_CONFIG_SELECT_STRING_VIEW=nssv_STRING_VIEW_NONSTD -Fotest.obj -c test.cpp
    In file included from test.cpp:1:
    nonstd/string_view.hpp(1274,21): error: definition with same mangled name
          '[email protected]@[email protected]@@[email protected]@@[email protected][email protected]@[email protected]@@[email protected]@Z' as another definition
    nssv_constexpr bool operator==(
                        ^
    nonstd/string_view.hpp(1280,21): note: previous definition is here
    nssv_constexpr bool operator==(
                        ^
    1 error generated.
    

    Tested under clang 11.0.1, MSVC 2019 19.28, string-view-lite 1.6.0.

    opened by nickhutchinson 2
  • std::search called in search() when compiling with C++14

    std::search called in search() when compiling with C++14

    I'm trying to compile one of the examples of my cuda-api-wrappers library, which uses string-view-lite, using C++14 instead of C++11 like I was compiling it so far. I get:

    ... snip ...
    /home/eyalroz/src/mine/cuda-api-wrappers/examples/other/jitify/string_view.hpp:572:23: error: call to non-‘constexpr’ function ‘_FIter1 std::search(_FIter1, _FIter1, _FIter2, _FIter2) [with _FIter1 = const char*; _FIter2 = const char*]’
      572 |     return std::search( haystack.begin(), haystack.end(), needle.begin(), needle.end() );
          |            ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    ... snip...
    

    and, indead, std::search is not constexpr before C++20.

    opened by eyalroz 1
  • Make find() constexpr in C++11

    Make find() constexpr in C++11

    Currently, the find() method of basic_string_view is nssv_constexpr14. It seems to me it should be possible to make it constexpr since C++11 with tail-recursion iteration.

    opened by eyalroz 7
  • Prevent construction/conversion from nullptr

    Prevent construction/conversion from nullptr

    When introducing string_view into an old codebase, it is quite easy to accidentally leave nonsensical assignments from or comparisons to nullptr in your code. Adding a deleted constructor as follows allows developers to easily identify the offending assignments and comparisons:

    
    #if nssv_CPP11_OR_GREATER
        nssv_constexpr basic_string_view( std::nullptr_t ) nssv_noexcept = delete;
    #endif
    

    C++23 will also disallow constructing std::basic_string_view from nullptr: https://github.com/cplusplus/papers/issues/887

    opened by mbs-c 1
  • Do some refinement work

    Do some refinement work

    • [x] on wandbox, update string_view.hpp to version 0.2.0
    • [x] on godbolt, update string_view.hpp to version 0.2.0
    • [ ] on godbolt, show string view lite's version number, see gsl lite
    • [x] in Readme, update badge links of godbolt, wandbox
    • [ ] review test/specification texts
    • [ ] ...
    opened by martinmoene 0
Releases(v1.7.0)
  • v1.7.0(Aug 10, 2022)

    This release of string_view lite contains the following additions, fixes and changes.

    Additions:

    • Add prevention to construct, convert from nullptr, p2166 (#47, thanks @mbs-c).
    • Add configuration option nssv_CONFIG_NO_STREAM_INSERTION to omit streams (#42, thanks @theShmoo).
    • Add export() to CMakeLists.txt, enabling importing targets, nonstd-lite-project issues 50.
    • Add script tc-cl.bat, nonstd-lite-project issues 54.
    • Add GitHub Actions CI.

    Fixes:

    • Fix prevention to construct, convert from nullptr for presence of =delete, p2166 (#47).
    • Fix check for stream to use sentry (#46, thanks mprather).
    • Fix parameter type from char to CharT for older compilers (#45, thanks @Elite-stay).
    • Fix link to LLVM libc++ implementation of string_view (thanks @striezel, #44).
    • Fix typos in string_view.hpp (thanks @striezel, #43).
    • Fix identical mangled names for clang on Windows (#40, thanks @nickhutchinson).

    Changes:

    Source code(tar.gz)
    Source code(zip)
    string_view.hpp(50.69 KB)
  • v1.6.0(Oct 9, 2020)

    This release of string_view lite adds configuration via the Tweak header mechanism as proposed by Colby Pike (@vector-of-bool).

    Additions:

    • Add 'Tweak header' support via nonstd/string_view.tweak.hpp

    Fixes:

    • Fix usage of builtin for MSVC 1920/14.2 (VS2019) for pre-C++17
    • Fix to use nssv prefix in configuration in t.bat, tc.bat, tg.bat batch files in test/

    Changes:

    • Add Visual Studio 2019 to the Appveyor build matrix (#39)
    Source code(tar.gz)
    Source code(zip)
    string_view.hpp(49.15 KB)
  • v1.5.1(Oct 4, 2020)

    This release of string_view lite fixes the presence of char type specializations for compare() and length() that use builtin functions memcmp and strlen in relation to the availability of constexpr.

    Fixes:

    • Fix usage of char type builtins or mem-functions with respect to constexpr-ness (#34, thanks to @mcskatkat) (#38)

    Changes:

    • Add tag [.builtins] to inspect relevant macros.
    Source code(tar.gz)
    Source code(zip)
    string_view.hpp(48.77 KB)
  • v1.5.0(Oct 3, 2020)

    This release of string_view lite provides constexpr comparison via compare() for C++14 (#34, thanks to @mcskatkat).

    It contains the following fixes and changes:

    Fixes:

    • Fix detail::length() for non-optimized compilation (#33, thanks to @augustheart)
    • Fix operator operator!=(basic_string_view, std::basic_string).

    Changes:

    • Provide constexpr comparison via compare() for C++14 (#34, thanks to @mcskatkat)
    • For char type, use __builtin_memcmp(), __builtin_strlen(), memcmp() or strlen() (#34, thanks to @mcskatkat).
    • Express operator!=() in operator==()
    • Add IDE folders/files and build folder to .gitignore (.vs, .vscode, CodeBlocks, build)
    • Add .editorconfig file
    Source code(tar.gz)
    Source code(zip)
    string_view.hpp(47.82 KB)
  • v1.4.0(Apr 29, 2020)

    This release of string_view lite contains the following fixes and changes:

    Fixes:

    • Fix operator==() on wstring_view and wchar_t raw string (#30, thanks to @gongminmin).

    Changes:

    • Add option -Wshadow for gcc, clang.
    • Avoid shadowing (#31, thanks @mattyclarkson).
    • Add string_view comparison with std::string and const char * for older compilers.
    • Non-recursive variant of detail::length() for compilers with maximum recursion depth in compile-time constexpr evaluation (#29, thanks @AdmiralCurtiss).
    • Improve MSVC version table, nonstd-lite-project issue 38.
    • Point conan badge to conan-center (thanks @flexferrum).
    • Use #include in code on godbolt, nonstd-lite-project issue 36.
    Source code(tar.gz)
    Source code(zip)
    string_view.hpp(45.92 KB)
  • v1.3.0(Jun 4, 2019)

    In this release of string_view lite, string_view.hpp is unchanged (apart from it's version number).

    Changes:

    • Change vcpkg install to use CMake
    • Replace underscore with dash in unit name
    • Add settings options to conanfile (thanks to @ngrodzitski)
    Source code(tar.gz)
    Source code(zip)
    string_view.hpp(40.20 KB)
  • v1.2.0(May 11, 2019)

  • v1.1.0(Oct 28, 2018)

  • v1.0.0(Jun 5, 2018)

  • v0.2.0(Jan 10, 2018)

  • v0.1.0(Jan 8, 2018)

    First alpha release.

    ToDo:

    • [x] Implement comparisons, extended
    • [x] Implement std::hash<>
    • [ ] Setup 'on conan' (@agauniyal ?)
    • [x] Setup 'on Wandbox'
    • [x] Setup 'on Godbolt'
    • [x] Fine-tune Readme
      • [x] Add example
    Source code(tar.gz)
    Source code(zip)
Owner
Martin Moene
C++ programmer at Universiteit Leiden, member and former webeditor of @accu-org.
Martin Moene
span lite - A C++20-like span for C++98, C++11 and later in a single-file header-only library

span lite: A single-file header-only version of a C++20-like span for C++98, C++11 and later Contents Example usage In a nutshell License Dependencies

Martin Moene 427 Dec 31, 2022
variant lite - A C++17-like variant, a type-safe union for C++98, C++11 and later in a single-file header-only library

variant lite: A single-file header-only version of a C++17-like variant, a type-safe union for C++98, C++11 and later Contents Example usage In a nuts

Martin Moene 225 Dec 29, 2022
expected lite - Expected objects in C++11 and later in a single-file header-only library

expected lite: expected objects for C++11 and later expected lite is a single-file header-only library for objects that either represent a valid value

Martin Moene 254 Jan 4, 2023
gsl-lite – A single-file header-only version of ISO C++ Guidelines Support Library (GSL) for C++98, C++11, and later

gsl-lite: Guidelines Support Library for C++98, C++11 up metadata build packages try online gsl-lite is an implementation of the C++ Core Guidelines S

gsl-lite 774 Jan 7, 2023
gsl-lite – A single-file header-only version of ISO C++ Guidelines Support Library (GSL) for C++98, C++11, and later

gsl-lite: Guidelines Support Library for C++98, C++11 up metadata build packages try online gsl-lite is an implementation of the C++ Core Guidelines S

gsl-lite 772 Dec 31, 2022
Single-header header-only C++11 / C++14 / C++17 library for easily managing set of auto-generated type-safe flags.

Single-header header-only C++11 / C++14 / C++17 library for easily managing set of auto-generated type-safe flags. Quick start #include <bitflags/bitf

Marin Peko 76 Nov 22, 2022
A collection of std-like single-header C++ libraries

itlib: iboB's Template Libraries A collection of small single-header C++ libraries similar to or extending the C++ standard library. See below for a l

Borislav Stanimirov 98 Dec 29, 2022
Libft is an individual project at 42 that requires us to re-create some standard C library functions including some additional ones that can be used later to build a library of useful functions for the rest of the program.

?? Index What is Libft? List of Functions Technologies ✨ What is Libft? Libft is an individual project at 42 that requires us to re-create some standa

Paulo Rafael Ramalho 7 Jan 17, 2022
A header-only, unobtrusive, almighty alternative to the C++ switch statement that looks just like the original.

uberswitch A header-only, unobtrusive, almighty alternative to the C++ switch statement that looks just like the original. Sample usage (incomplete -

Fabio 85 Jan 3, 2023
jkds is a modern header-only C++20 library that complements the standard library.

jkds is a modern header-only C++20 library that complements the standard library. It provides generic atypical data structures, ergonomic functional programming abstractions, and then some.

Alberto Schiabel 6 Nov 16, 2022
Bsl - Rust 2018 and C++20, "constexpr everything", AUTOSAR compliant header-only library intended to support the development of critical systems applications

Description The Bareflank Support Library (BSL) is a Rust 2018 and C++20, "constexpr everything", AUTOSAR compliant header-only library intended to su

Bareflank 76 Dec 8, 2022
Lightweight single-file utilities for C99. Portable & zero dependency

plainlibs Lightweight single-file utilities for C99. Key Features Portable across Unix & Windows (including MSVC) Zero dependencies (besides C stdlib)

null 5 Oct 5, 2022
Modern C++ generic header-only template library.

nytl A lightweight and generic header-only template library for C++17. Includes various utility of all kind that i needed across multiple projects: Ex

Jan 95 Nov 3, 2022
Library that simplify to find header for class from STL library.

Library that simplify to find header for class from STL library. Instead of searching header for some class you can just include header with the class name.

null 6 Jun 7, 2022
Allows a programmer to print table-like outputs over std::ostream.

tableprinter Allows a programmer to print table-like outputs over std::ostream. It is a header only library. No other dependency than STL. Provides re

null 24 Jul 7, 2022
Improved and configurable drop-in replacement to std::function that supports move only types, multiple overloads and more

fu2::function an improved drop-in replacement to std::function Provides improved implementations of std::function: copyable fu2::function move-only fu

Denis Blank 429 Dec 12, 2022
SCST is a SCSI target software stack that allows to export any block device or file via iSCSI, FC or RDMA (SRP).

Overview This is the source code repository of the SCST project. SCST is a collection of Linux kernel drivers that implement SCSI target functionality

SCST 29 Dec 21, 2022
Thrust is a C++ parallel programming library which resembles the C++ Standard Library.

Thrust: Code at the speed of light Thrust is a C++ parallel programming library which resembles the C++ Standard Library. Thrust's high-level interfac

NVIDIA Corporation 4.3k Dec 31, 2022
A template C project using CMAKE, logging library and basic memory handling.

C Project template Aim of this Repository is to create a template repository for C executable projects with following properties: Cmake project Loggin

Aditya Singh Rathore 6 May 23, 2022