Program Listing for File to_string.hpp

Return to documentation for file (/tmp/ws/src/apex_containers/apex_containers/include/string/to_string.hpp)

// Copyright 2017-2018 Apex.AI, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef STRING__TO_STRING_HPP_
#define STRING__TO_STRING_HPP_

#include <apexutils/apexdef.h>
#include <apex_containers/visibility_control.hpp>

#include <stdexcept>
#include <cstring>
#include <algorithm>
#include <istream>
#include <ostream>
#include <string>

#include "base_string.hpp"
#include "string_strict.hpp"
#include "string_silent.hpp"

namespace apex
{

APEX_CONTAINERS_PUBLIC apex::string256_t to_string(const char8_t * const c_text_ptr) noexcept;

APEX_CONTAINERS_PUBLIC apex::string8_t to_string(const bool value) noexcept;

APEX_CONTAINERS_PUBLIC apex::string16_t to_string(const ::uint8_t value) noexcept;

APEX_CONTAINERS_PUBLIC apex::string16_t to_string(const ::uint16_t value) noexcept;

APEX_CONTAINERS_PUBLIC apex::string16_t to_string(const ::uint32_t value) noexcept;

APEX_CONTAINERS_PUBLIC apex::string32_t to_string(const uint64_t value) noexcept;

#ifndef APEX_WINDOWS
inline apex::string32_t to_string(const long long unsigned int value) noexcept  // NOLINT
{
  return apex::to_string(static_cast<uint64_t>(value));
}
#endif  // APEX_WINDOWS

APEX_CONTAINERS_PUBLIC apex::string16_t to_string(const ::int8_t value) noexcept;

APEX_CONTAINERS_PUBLIC apex::string16_t to_string(const ::int16_t value) noexcept;

APEX_CONTAINERS_PUBLIC apex::string16_t to_string(const ::int32_t value) noexcept;

APEX_CONTAINERS_PUBLIC apex::string32_t to_string(const ::int64_t value) noexcept;

#ifndef APEX_WINDOWS
inline apex::string32_t to_string(const long long int value) noexcept  // NOLINT
{
  return apex::to_string(static_cast<int64_t>(value));
}
#endif  // APEX_WINDOWS

APEX_CONTAINERS_PUBLIC apex::string16_t to_string(const ::float32_t value) noexcept;

APEX_CONTAINERS_PUBLIC apex::string32_t to_string(const ::float64_t value) noexcept;

inline auto to_string(const std::string & value) noexcept
{
  return apex::to_string(value.c_str());
}

template<::size64_t STRING_BUFFER_SIZE>
inline auto to_string(const StringStrict<STRING_BUFFER_SIZE> & value) noexcept
{
  return apex::to_string(value.c_str());
}

// this is implementation. See the declaration in the String class.
template<::size64_t STRING_BUFFER_SIZE>
inline StringStrict<STRING_BUFFER_SIZE>
StringStrict<STRING_BUFFER_SIZE>::to_string(const uint32_t value)
{
  StringStrict<STRING_BUFFER_SIZE> retval = apex::to_string(value);
  return retval;
}

// this is implementation. See the declaration in the String class.
template<::size64_t STRING_BUFFER_SIZE>
inline String<STRING_BUFFER_SIZE> String<STRING_BUFFER_SIZE>::to_string(const uint32_t value)
{
  apex::string16_t result = apex::to_string(value);
  if (result.size() >= String<STRING_BUFFER_SIZE>::get_buffer_size_static()) {
    throw std::overflow_error("result.size() >= this->capacity()");
  }
  String<STRING_BUFFER_SIZE> retval = result;
  return retval;
}

template<typename ... Args>
APEX_CONTAINERS_PUBLIC apex::string256_t varargs_to_string(Args const & ... args) noexcept
{
  // This implementation is based on variadic template parameter pack
  // See https://en.cppreference.com/w/cpp/language/parameter_pack
  apex::string256_t result_str;
  const size64_t argc = sizeof ... (args);
  size64_t counter = 0U;

  // On the every element of the array add a parameter to the result_str.
  // If the parameter is not the last one, add a space separator after
  char8_t const string_filler[]{
    '\0',      // The "value" to put into the string_filler array when the parameter list is empty
    (
      result_str += apex::to_string(args),      // Add parameter string representation to result
      result_str += (counter < (argc - 1U)) ? " " : "",                  // Add a separator
      counter++,                  // Increment the parameter counter
      '\0') ...                  // The "value" to put into the string_filler array
  };

  // We don't need string_filler array in the active code, we need its curved brackets initializer.
  // The below construct is a command to static analysis tools to ignore non-use of the array
  (void)string_filler;

  return result_str;
}

struct no_separator {};

template<typename ... Args>
APEX_CONTAINERS_PUBLIC
apex::string256_t varargs_to_string(no_separator tag, Args const & ... args) noexcept
{
  (void)tag;
  apex::string256_t result_str;
  char8_t const string_filler[]{'\0', (result_str += apex::to_string(args), '\0') ...};
  (void)string_filler;

  return result_str;
}

}  // namespace apex

#endif  // STRING__TO_STRING_HPP_