---
title: llarp/util/str.hpp

---

# llarp/util/str.hpp



## Namespaces

| Name           |
| -------------- |
| **[llarp](Namespaces/namespacellarp.md)** <br>[crypto.hpp] |

## Classes

|                | Name           |
| -------------- | -------------- |
| struct | **[llarp::CaselessLessThan](Classes/structllarp_1_1CaselessLessThan.md)**  |




## Source code

```cpp
#pragma once

#include <string_view>
#include <vector>
#include <chrono>
#include <iterator>
#include <charconv>
#include <fmt/format.h>

namespace llarp
{
  bool
  IsFalseValue(std::string_view str);

  struct CaselessLessThan
  {
    bool
    operator()(std::string_view lhs, std::string_view rhs) const;
  };

  bool
  IsTrueValue(std::string_view str);

  [[nodiscard]] std::string_view
  TrimWhitespace(std::string_view str);

  using namespace std::literals;

  inline bool
  string_iequal(std::string_view s1, std::string_view s2)
  {
    return std::equal(s1.begin(), s1.end(), s2.begin(), s2.end(), [](char a, char b) {
      return std::tolower(static_cast<unsigned char>(a))
          == std::tolower(static_cast<unsigned char>(b));
    });
  }

  template <typename S1, typename... S>
  bool
  string_iequal_any(const S1& s1, const S&... s)
  {
    return (... || string_iequal(s1, s));
  }

  inline constexpr bool
  starts_with(std::string_view str, std::string_view prefix)
  {
    return str.substr(0, prefix.size()) == prefix;
  }

  inline constexpr bool
  ends_with(std::string_view str, std::string_view suffix)
  {
    return str.size() >= suffix.size() && str.substr(str.size() - suffix.size()) == suffix;
  }

  inline constexpr std::string_view
  strip_prefix(std::string_view str, std::string_view prefix)
  {
    if (starts_with(str, prefix))
      return str.substr(prefix.size());
    return str;
  }

  std::vector<std::string_view>
  split(std::string_view str, std::string_view delim, bool trim = false);

  std::vector<std::string_view>
  split_any(std::string_view str, std::string_view delims, bool trim = false);

  template <typename It>
  std::string
  join(std::string_view delimiter, It begin, It end)
  {
    return fmt::format("{}", fmt::join(delimiter, begin, end));
  }

  template <typename Container>
  std::string
  join(std::string_view delimiter, const Container& c)
  {
    return join(delimiter, c.begin(), c.end());
  }

  void
  trim(std::string_view& s);

  template <typename T>
  bool
  parse_int(const std::string_view str, T& value, int base = 10)
  {
    T tmp;
    auto* strend = str.data() + str.size();
    auto [p, ec] = std::from_chars(str.data(), strend, tmp, base);
    if (ec != std::errc() || p != strend)
      return false;
    value = tmp;
    return true;
  }

  std::string
  lowercase_ascii_string(std::string src);

  std::string
  friendly_duration(std::chrono::nanoseconds dur);

  std::wstring
  to_wide(std::string data);

}  // namespace llarp
```


-------------------------------

Updated on 2022-10-17 at 13:14:51 +0000
