Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
HenryAWE committed Nov 25, 2023
1 parent 31603ce commit 1f77e4d
Show file tree
Hide file tree
Showing 13 changed files with 205 additions and 108 deletions.
4 changes: 0 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
cmake_minimum_required(VERSION 3.20)

# Use C++ 20
if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 19.12.25835)
set(CMAKE_CXX20_STANDARD_COMPILE_OPTION "-std:c++latest")
set(CMAKE_CXX20_EXTENSION_COMPILE_OPTION "-std:c++latest")
endif()
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# Force UTF-8 on MSVC
Expand Down
39 changes: 0 additions & 39 deletions include/papilio/core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,45 +296,6 @@ namespace papilio
}
}

class slice
{
public:
using index_type = std::make_signed_t<std::size_t>; // ssize_t

static constexpr index_type npos = std::numeric_limits<index_type>::max();

constexpr slice() noexcept
: m_begin(0), m_end(npos) {}
constexpr slice(const slice&) noexcept = default;
constexpr explicit slice(index_type begin_, index_type end_) noexcept
: m_begin(begin_), m_end(end_) {}

slice& operator=(const slice&) noexcept = default;

constexpr void normalize(index_type length) noexcept
{
if(m_begin < 0)
m_begin = length + m_begin;
if(m_end)
m_end = length + m_end;
}

[[nodiscard]]
constexpr index_type begin() const noexcept
{
return m_begin;
}
[[nodiscard]]
constexpr index_type end() const noexcept
{
return m_end;
}

private:
index_type m_begin;
index_type m_end;
};

class indexing_value
{
public:
Expand Down
9 changes: 8 additions & 1 deletion include/papilio/locale.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

namespace papilio
{
// Reference to a locale object
// The member function "get()" will return the C locale if the reference is empty.
class locale_ref
{
public:
Expand All @@ -28,7 +30,12 @@ namespace papilio
}

[[nodiscard]]
std::locale get() const;
std::locale get() const
{
return m_loc == nullptr ?
std::locale("C") :
*m_loc;
}

operator std::locale() const
{
Expand Down
3 changes: 3 additions & 0 deletions include/papilio/macros.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <cassert>
#include <version>


Expand Down Expand Up @@ -28,3 +29,5 @@
#else
# define PAPILIO_ASSUME(expr)
#endif

#define PAPILIO_ASSERT(expr) assert(expr)
1 change: 0 additions & 1 deletion include/papilio/memory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <memory>
#include <utility>
#include "macros.hpp"
#include "type.hpp"


namespace papilio
Expand Down
79 changes: 78 additions & 1 deletion include/papilio/type.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// type traits and concepts
// concepts, type traits, tags, auxiliary types

#pragma once

Expand All @@ -7,6 +7,7 @@
#include <concepts>
#include <string>
#include <string_view>
#include "macros.hpp"


namespace papilio
Expand Down Expand Up @@ -36,8 +37,17 @@ namespace papilio
std::is_same_v<std::decay_t<T>, const CharT*> ||
std::is_same_v<T, std::basic_string<CharT>> ||
std::is_same_v<T, std::basic_string_view<CharT>>;

template <typename T>
concept string_like = basic_string_like<T, char>;
template <typename T>
concept u8string_like = basic_string_like<T, char8_t>;
template <typename T>
concept u16string_like = basic_string_like<T, char16_t>;
template <typename T>
concept u32string_like = basic_string_like<T, char32_t>;
template <typename T>
concept wstring_like = basic_string_like<T, wchar_t>;

template <typename T>
concept pointer_like =
Expand All @@ -48,4 +58,71 @@ namespace papilio

struct reverse_index_t {};
constexpr reverse_index_t reverse_index = {};

// ^^^ tags ^^^ / vvv auxiliary types vvv

// Signed size type
using ssize_t = std::make_signed_t<std::size_t>;

// [begin, end) range
// Negative value means reverse index like Python.
// For example, -1 refers to the last element, and -2 refers to the second to last element.
class slice :
public std::pair<ssize_t, ssize_t>
{
public:
using size_type = std::size_t;
using index_type = ssize_t;

static constexpr index_type npos = std::numeric_limits<index_type>::max();

constexpr slice() noexcept
: slice(0, npos) {}
constexpr slice(const slice&) noexcept = default;
constexpr explicit slice(index_type start, index_type stop = npos) noexcept
: pair(start, stop) {}

constexpr slice& operator=(const slice&) noexcept = default;

constexpr bool operator==(const slice&) const noexcept = default;

constexpr void normalize(std::in_place_t, size_type len) noexcept
{
PAPILIO_ASSERT(len <= std::numeric_limits<index_type>::max());

if(first < 0)
first = len + first;

if(second < 0)
second = len + second;
else if(second == npos)
second = len;
}
[[nodiscard]]
constexpr slice normalize(size_type len) const noexcept
{
slice result = *this;
result.normalize(std::in_place, len);
return result;
}

[[nodiscard]]
constexpr index_type begin() const noexcept
{
return first;
}
[[nodiscard]]
constexpr index_type end() const noexcept
{
return second;
}

[[nodiscard]]
constexpr size_type length() const noexcept
{
PAPILIO_ASSERT(first > 0);
PAPILIO_ASSERT(second > 0);
return second - first;
}
};
}
11 changes: 11 additions & 0 deletions natvis/papilio.natvis
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ License: MIT
<DisplayString>reverse_index</DisplayString>
</Type>

<Type Name="papilio::slice">
<DisplayString IncludeView="noparens" Condition="second == npos">{first}, npos</DisplayString>
<DisplayString ExcludeView="noparens" Condition="second == npos">({first}, npos)</DisplayString>
<DisplayString IncludeView="noparens" Condition="second != npos">{first}, {second}</DisplayString>
<DisplayString ExcludeView="noparens" Condition="second != npos">({first}, {second})</DisplayString>
<Expand>
<Item Name="first">first</Item>
<Item Name="second">second</Item>
</Expand>
</Type>

<Type Name="papilio::detail::compressed_pair_impl&lt;*,*,0&gt;">
<Intrinsic Name="first" Expression="m_first" />
<Intrinsic Name="second" Expression="m_second" />
Expand Down
12 changes: 0 additions & 12 deletions src/locale.cpp

This file was deleted.

6 changes: 2 additions & 4 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,14 @@ define_papilio_test(test_format)

define_papilio_test(test_locale)

if(${papilio_build_module})
define_papilio_test(test_module)
endif()

define_papilio_test(test_print)

define_papilio_test(test_script)

define_papilio_test(test_stl_container)

define_papilio_test(test_type)

define_papilio_test(test_utf8)

define_papilio_test(test_utf_common)
Expand Down
20 changes: 11 additions & 9 deletions test/test_locale.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,28 @@ namespace test_locale
};
}

TEST(TestLocale, LocaleRef)
TEST(locale_ref, locale_ref)
{
using namespace papilio;

auto bool_helper = [](bool value, const std::locale& loc) -> std::string
{
auto& f = std::use_facet<std::numpunct<char>>(loc);
return value ?
f.truename() :
f.falsename();
};

{
locale_ref c_loc;
EXPECT_TRUE(std::isalpha('A', c_loc));
EXPECT_FALSE(std::isalpha('1', c_loc));

auto bool_helper = [](bool value, const std::locale& loc)->std::string
{
auto& f = std::use_facet<std::numpunct<char>>(loc);
return value ?
f.truename() :
f.falsename();
};

EXPECT_EQ(bool_helper(true, c_loc), "true");
EXPECT_EQ(bool_helper(false, c_loc), "false");
}

{
std::locale custom(std::locale("C"), new test_locale::yes_no);
locale_ref custom_ref = custom;

Expand Down
8 changes: 1 addition & 7 deletions test/test_memory.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <gtest/gtest.h>
#include <cstring>
#include <papilio/memory.hpp>
#include <papilio/type.hpp>


namespace test_memory
Expand All @@ -11,13 +12,6 @@ namespace test_memory

static_assert(papilio::pointer_like<papilio::optional_unique_ptr<int>>);
static_assert(papilio::pointer_like<papilio::optional_unique_ptr<int[]>>);
static_assert(papilio::pointer_like<std::unique_ptr<int>>);
static_assert(papilio::pointer_like<std::unique_ptr<int[]>>);
static_assert(papilio::pointer_like<std::shared_ptr<int>>);
static_assert(papilio::pointer_like<std::shared_ptr<int[]>>);
static_assert(papilio::pointer_like<int*>);
static_assert(papilio::pointer_like<int[]>);
static_assert(!papilio::pointer_like<int>);

TEST(utilities, independent_proxy)
{
Expand Down
30 changes: 0 additions & 30 deletions test/test_module.cpp

This file was deleted.

Loading

0 comments on commit 1f77e4d

Please sign in to comment.