GithubHelp home page GithubHelp logo

Comments (9)

oliverlee avatar oliverlee commented on June 6, 2024 1

Although I suppose it would make sense to allow a user to select std::search via a preprocessor macro (or vice-versa).

from string-view-lite.

eyalroz avatar eyalroz commented on June 6, 2024

Oh, actually, maybe it's a dupe of my other bug.

from string-view-lite.

oliverlee avatar oliverlee commented on June 6, 2024

You can try defining __OPTIMIZE__ before including string_view.hpp or build with optimizations enabled.

#if defined(__OPTIMIZE__)
// gcc, clang provide __OPTIMIZE__
// Expect tail call optimization to make search() non-recursive:
template< class CharT, class Traits = std::char_traits<CharT> >
constexpr const CharT* search( basic_string_view<CharT, Traits> haystack, basic_string_view<CharT, Traits> needle )
{
return haystack.starts_with( needle ) ? haystack.begin() :
haystack.empty() ? haystack.end() : search( haystack.substr(1), needle );
}
#else // OPTIMIZE
// non-recursive:
template< class CharT, class Traits = std::char_traits<CharT> >
constexpr const CharT* search( basic_string_view<CharT, Traits> haystack, basic_string_view<CharT, Traits> needle )
{
return std::search( haystack.begin(), haystack.end(), needle.begin(), needle.end() );
}
#endif // OPTIMIZE

https://stackoverflow.com/questions/14618249/is-there-a-builtin-macro-defined-when-optimisation-is-enabled-in-clang

from string-view-lite.

martinmoene avatar martinmoene commented on June 6, 2024

@eyalroz, @oliverlee

The unconditional constexpr in the non-optimize case of constexpr const CharT* search() should perhaps be replaced with something like nssv_constexpr20_lib.

I'd like nssv_HAVE_CONSTEXPR_20_LIB to be not overly restrictive as it may be as show below. Suggestions are quite welcome.

// Presence of C++20 library features:

#define nssv_HAVE_CONSTEXPR_20_LIB  nssv_CPP20_000

// ...

#if  nssv_HAVE_CONSTEXPR_20_LIB
# define nssv_constexpr20_lib  constexpr
#else
# define nssv_constexpr20_lib  /*constexpr*/
#endif

// ...

#else // OPTIMIZE

// non-recursive:

template< class CharT, class Traits = std::char_traits<CharT> >
nssv_constexpr20_lib const CharT* search( basic_string_view<CharT, Traits> haystack, basic_string_view<CharT, Traits> needle )
{
    return std::search( haystack.begin(), haystack.end(), needle.begin(), needle.end() );
}

#endif // OPTIMIZE

from string-view-lite.

oliverlee avatar oliverlee commented on June 6, 2024

I think it makes sense to remove the branch on _OPTIMIZE_. By removing the call to std::search, this would allow use in constant expressions with debug builds.

from string-view-lite.

martinmoene avatar martinmoene commented on June 6, 2024

However, a possibly recursive search, possibly exhausting the stack doesn't look like the right thing to do at default.

from string-view-lite.

oliverlee avatar oliverlee commented on June 6, 2024

Ah right. In that case, what about replacing the second implementation with something like:

template< class CharT, class Traits = std::char_traits<CharT> >
nssv_constexpr14 const CharT* search( basic_string_view<CharT, Traits> haystack, basic_string_view<CharT, Traits> needle )
{
    while (needle.size() <= haystack.size()) {
        if (haystack.starts_with(needle)) {
            return haystack.cbegin();
        }

        haystack = basic_string_view<CharT, Traits>{ haystack.begin() + 1, haystack.size() - 1U };
    }

    return haystack.cend();
}

from string-view-lite.

martinmoene avatar martinmoene commented on June 6, 2024

@oliverlee Thanks for your suggestions!

from string-view-lite.

oliverlee avatar oliverlee commented on June 6, 2024

Thanks for sharing your implementation of string view!

from string-view-lite.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.