Kaynağa Gözat

refactor: move simple macros to _macro.h, add IIF

Sam Jaffe 7 ay önce
ebeveyn
işleme
5c18d15254

+ 6 - 2
include/jvalidate/_config.h

@@ -1,9 +1,13 @@
 #pragma once
 
 #if __has_include(<unicode/std_string.h>)
-#define JVALIDATE_HAS_ICU
+#define JVALIDATE_HAS_ICU 1
+#else
+#define JVALIDATE_HAS_ICU 0
 #endif
 
 #if __has_include(<ada/idna/to_unicode.h>)
-#define JVALIDATE_HAS_IDNA
+#define JVALIDATE_HAS_IDNA 1
+#else
+#define JVALIDATE_HAS_IDNA 0
 #endif

+ 10 - 0
include/jvalidate/_macro.h

@@ -0,0 +1,10 @@
+#pragma once
+
+#include <jvalidate/_config.h>
+
+#define JVALIDATE_CONCAT2(A, B) A##B
+#define JVALIDATE_CONCAT(A, B) JVALIDATE_CONCAT2(A, B)
+
+#define JVALIDATE_IIF0(IF, ELSE) ELSE
+#define JVALIDATE_IIF1(IF, ELSE) IF
+#define JVALIDATE_IIF(CONDITIONAL, IF, ELSE) JVALIDATE_CONCAT(JVALIDATE_IIF, CONDITIONAL)(IF, ELSE)

+ 1 - 2
include/jvalidate/detail/scoped_state.h

@@ -3,8 +3,7 @@
 #include <functional>
 #include <type_traits>
 
-#define JVALIDATE_CONCAT2(A, B) A##B
-#define JVALIDATE_CONCAT(A, B) JVALIDATE_CONCAT2(A, B)
+#include <jvalidate/_macro.h>
 
 /**
  * @breif Create an anonymous scoped state object, which represents a temporary

+ 2 - 2
include/jvalidate/detail/string.h

@@ -9,7 +9,7 @@
 #include <string>
 #include <string_view>
 
-#ifdef JVALIDATE_HAS_IDNA
+#if JVALIDATE_HAS_IDNA
 #include <ada/idna/unicode_transcoding.h>
 #endif
 
@@ -23,7 +23,7 @@ inline std::string_view to_u8(std::string_view arg) { return arg; }
 inline std::u32string_view to_u32(std::u32string_view arg) { return arg; }
 }
 
-#ifdef JVALIDATE_HAS_IDNA
+#if JVALIDATE_HAS_IDNA
 namespace jvalidate::detail {
 /**
  * @brief Calclates the string-length of the argument, treating multi-byte

+ 3 - 7
include/jvalidate/format.h

@@ -1,5 +1,5 @@
 #pragma once
-#include <jvalidate/_config.h>
+#include <jvalidate/_macro.h>
 
 #include <cctype>
 #include <chrono>
@@ -24,11 +24,7 @@
 
 #define CONSTRUCTS(TYPE) format::ctor_as_valid<detail::TYPE>
 
-#ifdef JVALIDATE_HAS_IDNA
-#define UTF32(FN) format::utf32<format::FN<char32_t>>
-#else
-#define UTF32(FN) nullptr
-#endif
+#define UTF32(FN) JVALIDATE_IIF(JVALIDATE_HAS_IDNA, format::utf32<format::FN<char32_t>>, nullptr)
 
 namespace jvalidate::format::detail {
 struct result {
@@ -189,7 +185,7 @@ bool is_invalid_size_or_boundary_hostname(std::basic_string_view<CharT> name) {
           name[0] == '-' || name.back() == '-');
 }
 
-#ifndef JVALIDATE_HAS_IDNA
+#if !JVALIDATE_HAS_IDNA
 inline bool hostname_part(std::string_view name) {
   using delim = detail::char_delimiters<char>;