Skip to content

Adding support for cmake and char##_t chars for CsChar. #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ aclocal.m4
configure
config.log

# Reserved for build dirs with CMAKE
[bB]uild/*

/bin/
/docs/
/lib/
/lib/
15 changes: 15 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
cmake_minimum_required(VERSION 3.14)

project(CsString LANGUAGES CXX VERSION 1.2.1)

add_library(cs_string INTERFACE)
target_include_directories(cs_string INTERFACE src)
target_compile_features(cs_string INTERFACE cxx_std_14)

file(GLOB SOURCES src/*.cpp)

add_executable(cs_string_test test/main.cpp)
target_link_libraries(cs_string_test cs_string)

# TODO: Remove the need for this
target_compile_definitions(cs_string_test PRIVATE CS_STRING_ALLOW_UNSAFE)
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
AC_INIT([cs_string], [1.2.0], [[email protected]])
AC_INIT([cs_string], [1.2.1], [[email protected]])

# derive CS hex version from "@PACKAGE_VERSION@"
HEX_VERSION=$(printf "0x%02x%02x%02x" `echo $PACKAGE_VERSION | tr '.' ' '`)
Expand Down
64 changes: 39 additions & 25 deletions src/cs_char.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,99 +45,113 @@ class CsBasicString;
class CsChar
{
public:
CsChar()
constexpr CsChar() noexcept
: m_char(0)
{

}

template <typename T = int>
CsChar(char c)
constexpr CsChar(char c) noexcept
: m_char(static_cast<unsigned char>(c))
{
#ifndef CS_STRING_ALLOW_UNSAFE
static_assert(! std::is_same<T, T>::value, "Unsafe operations not allowed, unknown encoding for this operation");
#endif
}

CsChar(char32_t c)
/* UTF-8 characters - Available only with C++20 */
#ifdef __cpp_char8_t
constexpr CsChar(char8_t c) noexcept
: m_char(c)
{
}
#endif

CsChar(int c)
/* UTF-16 characters - as specified at http://eel.is/c++draft/lex.ccon#4 */
constexpr CsChar(char16_t c) noexcept
: m_char(c)
{
}

constexpr CsChar(char32_t c) noexcept
: m_char(c)
{
}

bool operator!=(const CsChar &other) const;
bool operator==(const CsChar &other) const;
constexpr CsChar(int c) noexcept
: m_char(c)
{
}

bool operator<(const CsChar &other) const;
bool operator<=(const CsChar &other) const;
bool operator>(const CsChar &other) const;
bool operator>=(const CsChar &other) const;
constexpr bool operator!=(const CsChar &other) const noexcept;
constexpr bool operator==(const CsChar &other) const noexcept;

CsChar &operator=(char c) &;
CsChar &operator=(char32_t c) &;
CsChar &operator=(CsChar c) &;
constexpr bool operator<(const CsChar &other) const noexcept;
constexpr bool operator<=(const CsChar &other) const noexcept;
constexpr bool operator>(const CsChar &other) const noexcept;
constexpr bool operator>=(const CsChar &other) const noexcept;

uint32_t unicode() const;
constexpr CsChar &operator=(char c) & noexcept;
constexpr CsChar &operator=(char32_t c) & noexcept;
constexpr CsChar &operator=(CsChar c) & noexcept;

constexpr uint32_t unicode() const noexcept;

private:
uint32_t m_char;
};

// comparisons
inline bool CsChar::operator!=(const CsChar &other) const
inline constexpr bool CsChar::operator!=(const CsChar &other) const noexcept
{
return m_char != other.m_char;
}

inline bool CsChar::operator==(const CsChar &other) const
inline constexpr bool CsChar::operator==(const CsChar &other) const noexcept
{
return m_char == other.m_char;
}

inline bool CsChar::operator<(const CsChar &other) const
inline constexpr bool CsChar::operator<(const CsChar &other) const noexcept
{
return m_char < other.m_char;
}

inline bool CsChar::operator<=(const CsChar &other) const
inline constexpr bool CsChar::operator<=(const CsChar &other) const noexcept
{
return m_char <= other.m_char;
}

inline bool CsChar::operator>(const CsChar &other) const
inline constexpr bool CsChar::operator>(const CsChar &other) const noexcept
{
return m_char > other.m_char;
}

inline bool CsChar::operator>=(const CsChar &other) const
inline constexpr bool CsChar::operator>=(const CsChar &other) const noexcept
{
return m_char >= other.m_char;
}

inline CsChar &CsChar::operator=(char c) &
inline constexpr CsChar &CsChar::operator=(char c) & noexcept
{
m_char = c;
return *this;
}

inline CsChar &CsChar::operator=(char32_t c) &
inline constexpr CsChar &CsChar::operator=(char32_t c) & noexcept
{
m_char = c;
return *this;
}

inline CsChar &CsChar::operator=(CsChar c) &
inline constexpr CsChar &CsChar::operator=(CsChar c) & noexcept
{
m_char = c.m_char;
return *this;
}

inline uint32_t CsChar::unicode() const
inline constexpr uint32_t CsChar::unicode() const noexcept
{
return m_char;
}
Expand All @@ -155,4 +169,4 @@ namespace std {
};
}

#endif
#endif
8 changes: 4 additions & 4 deletions test/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,20 +93,20 @@ void test_2()
// CsChar internationalization test

// unicode 00 42, data type char
CsString::CsChar c0 = 'B';
CsString::CsChar c0 = u'B';

// unicode 00 BF, data type maybe char or int, compile will return the value
CsString::CsChar c127 = '¿';
CsString::CsChar c127 = u'¿';
CsString::CsChar u127 = UCHAR('¿');

// unicode 21 B4, data type int or a compile error, not safe
CsString::CsChar c256 = '↴';
CsString::CsChar c256 = u'↴';

// unicode 21 B4, data type char32_t, guaranteed to be the proper unicode value
CsString::CsChar u256 = UCHAR('↴');

// unicode 01 D1 60, data type int or a compile error, not safe
CsString::CsChar cX = '𝅘𝅥𝅮';
CsString::CsChar cX = U'𝅘𝅥𝅮';

// unicode 01 D1 60, data type char32_t, guaranteed to be the proper unicode value
CsString::CsChar uX = UCHAR('𝅘𝅥𝅮');
Expand Down