Skip to content

Commit

Permalink
chore: cleanup header files (#1011)
Browse files Browse the repository at this point in the history
* chore: cleanup header files

* chore: reformat code

* chore: fix code formatting
  • Loading branch information
cieslarmichal authored Dec 5, 2024
1 parent 25283fc commit 1f76b3d
Show file tree
Hide file tree
Showing 26 changed files with 833 additions and 780 deletions.
1 change: 0 additions & 1 deletion include/faker-cxx/helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

namespace faker::helper
{

template <typename T>
concept input_range_with_faster_size_compute_than_linear_rng =
std::ranges::input_range<T> && (std::ranges::sized_range<T> || std::ranges::forward_range<T>);
Expand Down
11 changes: 3 additions & 8 deletions include/faker-cxx/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <chrono>
#include <cstdint>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <limits>
#include <map>
Expand All @@ -14,15 +15,12 @@
#include <sstream>
#include <string>
#include <string_view>
#include <time.h>

#include "faker-cxx/export.h"
#include "helpers/ulid/ulid.h"
#include "random_generator.h"

namespace faker::string
{

enum class Uuid
{
V1, // Version 1: UUIDs using a timestamp and monotonic counter.
Expand All @@ -47,9 +45,6 @@ struct FAKER_CXX_EXPORT CharCount
unsigned int atMostCount{(std::numeric_limits<unsigned>::max)()};
};

/**
* Specify the count required for specific chars
*/
using GuaranteeMap = std::map<char, CharCount>;

/**
Expand Down Expand Up @@ -116,7 +111,7 @@ FAKER_CXX_EXPORT std::string uuid(Uuid uuid = Uuid::V4);
* faker::string::ulid(1484581420) // "0001C7STHC0G2081040G208104"
* @endcode
*/
FAKER_CXX_EXPORT std::string ulid(time_t refDate = std::time(nullptr)); // Based on https://github.com/suyash/ulid
FAKER_CXX_EXPORT std::string ulid(time_t refDate = std::time(nullptr));

/**
* @brief Returns a string containing UTF-16 chars between 33 and 125 (`!` to `}`).
Expand Down Expand Up @@ -350,4 +345,4 @@ FAKER_CXX_EXPORT std::string nanoId();
* @endcode
*/
FAKER_CXX_EXPORT std::string nanoId(int minLength, int maxLength);
}
}
52 changes: 0 additions & 52 deletions include/faker-cxx/word.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,56 +173,4 @@ FAKER_CXX_EXPORT std::string_view preposition(std::optional<unsigned> length = s
* @endcode
*/
FAKER_CXX_EXPORT std::string_view verb(std::optional<unsigned> length = std::nullopt, Locale locale = Locale::en_US);

/**
* @brief Returns random element of length
*
* @param length The length of the elements to be picked from
* @param locale The locale. Defaults to `Locale::en_US`.
*
* @ range The range of elements
*
* @returns An element of the range value type
*
* @code
* faker::word::sortedSizeRandomElement(3, {"hi, "hello", "hey"}) // "hey" - Since "hey" is the only element of length 3
* @endcode
*/
template <std::ranges::range Range>
auto sortedSizeRandomElement(std::optional<unsigned int> length, Range&& range) -> decltype(auto)
{
if (!length)
{
return helper::randomElement(range);
}

size_t length_64 = *length;
auto start = range.begin();
auto end = range.end();

auto lower_it = ::std::lower_bound(start, end, length_64,
[](const auto& lhs, const auto& value) { return lhs.size() < value; });

if (lower_it == end)
{
return helper::randomElement(range);
}

if (lower_it->size() != length)
{
return *lower_it;
}

auto upper_it = lower_it;

for (; upper_it != end; upper_it++)
{
if (upper_it->size() != lower_it->size())
{
break;
}
}

return helper::randomElement(std::ranges::subrange(lower_it, upper_it));
}
}
Loading

0 comments on commit 1f76b3d

Please sign in to comment.