Ignore:
Timestamp:
Sep 22, 2021, 9:50:18 AM (2 years ago)
Author:
Peter
Message:

add wrapper function that calls std::string::contains, ::starts_with, and ::ends_with, if available; otherwise use home-brewed code. Add autoconf tests that check if these functions are available in CXX.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/yat/utility/utility.h

    r4098 r4102  
    166166
    167167  /**
     168     \return true if string \a str contains \a s
     169
     170     \see https://en.cppreference.com/w/cpp/string/basic_string/comtains
     171
     172     \since New in yat 0.20
     173   */
     174  template<typename charT, class Traits, class Allocator>
     175  bool contains(const std::basic_string<charT, Traits, Allocator>& str,
     176                const std::basic_string<charT, Traits, Allocator>& substr);
     177
     178  /// since New in yat 0.20
     179  template<typename charT, class Traits, class Allocator>
     180  bool contains(const std::basic_string<charT, Traits, Allocator>& str,
     181                charT substr);
     182
     183  /// since New in yat 0.20
     184  template<typename charT, class Traits, class Allocator>
     185  bool contains(const std::basic_string<charT, Traits, Allocator>& str,
     186                const charT* substr);
     187
     188
     189  /**
    168190     \brief convert T to a string
    169191
     
    220242   */
    221243  std::string dirname(const std::string& fn);
     244
     245  /**
     246     \return true if string \a str ends with \a suffix
     247
     248     \see https://en.cppreference.com/w/cpp/string/basic_string/ends_with
     249
     250     \since New in yat 0.20
     251   */
     252  template<typename charT, class Traits, class Allocator>
     253  bool ends_with(const std::basic_string<charT, Traits, Allocator>& str,
     254                 const std::basic_string<charT, Traits, Allocator>& suffix);
     255
     256  /// since New in yat 0.20
     257  template<typename charT, class Traits, class Allocator>
     258  bool ends_with(const std::basic_string<charT, Traits, Allocator>& str,
     259                 charT suffix);
     260
     261  /// since New in yat 0.20
     262  template<typename charT, class Traits, class Allocator>
     263  bool ends_with(const std::basic_string<charT, Traits, Allocator>& str,
     264                 const charT* suffix);
    222265
    223266  /**
     
    451494
    452495  /**
     496     \return true if string \a str starts with \a prefix
     497
     498     \see https://en.cppreference.com/w/cpp/string/basic_string/starts_with
     499
     500     \since New in yat 0.20
     501   */
     502  template<typename charT, class Traits, class Allocator>
     503  bool starts_with(const std::basic_string<charT, Traits, Allocator>& str,
     504                   const std::basic_string<charT, Traits, Allocator>& prefix);
     505
     506  /// since New in yat 0.20
     507  template<typename charT, class Traits, class Allocator>
     508  bool starts_with(const std::basic_string<charT, Traits, Allocator>& str,
     509                   charT prefix);
     510
     511  /// since New in yat 0.20
     512  template<typename charT, class Traits, class Allocator>
     513  bool starts_with(const std::basic_string<charT, Traits, Allocator>& str,
     514                   const charT* prefix);
     515
     516  /**
    453517     Calculate sum of weights in range [first, last). The complexity
    454518     is linear except in the important case when \c Iterator is
     
    570634
    571635
     636  template<typename charT, class Traits, class Allocator>
     637  bool contains(const std::basic_string<charT, Traits, Allocator>& str,
     638                const std::basic_string<charT, Traits, Allocator>& substr)
     639  {
     640    return str.find(substr)!=std::basic_string<charT, Traits,Allocator>::npos;
     641  }
     642
     643
     644  template<typename charT, class Traits, class Allocator>
     645  bool contains(const std::basic_string<charT, Traits, Allocator>& str,
     646                charT substr)
     647  {
     648    return str.find(substr)!=std::basic_string<charT, Traits,Allocator>::npos;
     649  }
     650
     651
     652  template<typename charT, class Traits, class Allocator>
     653  bool contains(const std::basic_string<charT, Traits, Allocator>& str,
     654                const charT* substr)
     655  {
     656    return str.find(substr)!=std::basic_string<charT, Traits,Allocator>::npos;
     657  }
     658
     659
    572660  template<typename T>
    573661  std::string convert(T input)
     
    587675                          std::string("\")"));
    588676    return result;
     677  }
     678
     679
     680  template<typename charT, class Traits, class Allocator>
     681  bool ends_with(const std::basic_string<charT, Traits, Allocator>& str,
     682                 const std::basic_string<charT, Traits, Allocator>& suffix)
     683  {
     684#ifdef YAT_HAVE_FUNC_STRING_ENDS_WITH
     685    return str.ends_with(suffix);
     686#else
     687    size_t n = suffix.size();
     688    return n <= str.size() && !str.compare(str.size()-n, n, suffix);
     689#endif
     690  }
     691
     692
     693  template<typename charT, class Traits, class Allocator>
     694  bool ends_with(const std::basic_string<charT, Traits, Allocator>& str,
     695                 charT suffix)
     696  {
     697#ifdef YAT_HAVE_FUNC_STRING_ENDS_WITH
     698    return str.ends_with(suffix);
     699#else
     700    Traits traits;
     701    return str.size() && traits.eq(str.back(), suffix);
     702#endif
     703  }
     704
     705
     706  template<typename charT, class Traits, class Allocator>
     707  bool ends_with(const std::basic_string<charT, Traits, Allocator>& str,
     708                 const charT* suffix)
     709  {
     710#ifdef YAT_HAVE_FUNC_STRING_ENDS_WITH
     711    return str.ends_with(suffix);
     712#else
     713    Traits traits;
     714    size_t n = traits.length(suffix);
     715    return n <= str.size() && !str.compare(str.size()-n, n, suffix);
     716#endif
    589717  }
    590718
     
    647775      pusher(std::move(element), vec);
    648776    }
     777  }
     778
     779
     780  template<typename charT, class Traits, class Allocator>
     781  bool starts_with(const std::basic_string<charT, Traits, Allocator>& str,
     782                   const std::basic_string<charT, Traits, Allocator>& prefix)
     783  {
     784#ifdef YAT_HAVE_FUNC_STRING_STARTS_WITH
     785    return str.starts_with(prefix);
     786#else
     787    size_t n = prefix.size();
     788    return n <= str.size() && !str.compare(0, n, prefix);
     789#endif
     790  }
     791
     792
     793  template<typename charT, class Traits, class Allocator>
     794  bool starts_with(const std::basic_string<charT, Traits, Allocator>& str,
     795                   charT prefix)
     796  {
     797#ifdef YAT_HAVE_FUNC_STRING_STARTS_WITH
     798    return str.starts_with(prefix);
     799#else
     800    Traits traits;
     801    return str.size() && traits.eq(str[0], prefix);
     802#endif
     803  }
     804
     805
     806  template<typename charT, class Traits, class Allocator>
     807  bool starts_with(const std::basic_string<charT, Traits, Allocator>& str,
     808                   const charT* prefix)
     809  {
     810#ifdef YAT_HAVE_FUNC_STRING_STARTS_WITH
     811    return str.starts_with(prefix);
     812#else
     813    Traits traits;
     814    size_t n = traits.length(prefix);
     815    return n <= str.size() && !str.compare(0, n, prefix);
     816#endif
    649817  }
    650818
Note: See TracChangeset for help on using the changeset viewer.