Przeglądaj źródła

Compile time string joiner

Kajetan Johannes Hammerle 5 dni temu
rodzic
commit
6011df96c7
4 zmienionych plików z 66 dodań i 13 usunięć
  1. 1 0
      CMakeLists.txt
  2. 12 13
      modules/Logger.cppm
  3. 51 0
      modules/StringJoin.cppm
  4. 2 0
      test/Main.cpp

+ 1 - 0
CMakeLists.txt

@@ -35,6 +35,7 @@ set(PUBLIC_MODULES
     "modules/ReadLine.cppm"
     "modules/Std.cppm"
     "modules/StringFormat.cppm"
+    "modules/StringJoin.cppm"
     "modules/Terminal.cppm"
     "modules/TerminalConstants.cppm"
     "modules/Test.cppm"

+ 12 - 13
modules/Logger.cppm

@@ -4,6 +4,7 @@ import Core.TerminalConstants;
 import Core.Meta;
 import Core.ToString;
 import Core.Std;
+import Core.StringJoin;
 
 #define SOURCE const std::source_location& sl = std::source_location::current()
 
@@ -56,37 +57,35 @@ export namespace Core {
     template<typename... Args>
     void logError(const FormatLocation& format, Args&&... args) noexcept {
         if constexpr(LOG_LEVEL >= 1) {
-            String<32> s;
-            s.addFormat("{}[ERROR] ", Terminal::FG_RED);
-            log(LogLevel::ERROR, s, format, forward<Args>(args)...);
+            log(LogLevel::ERROR, mergeStrings(Terminal::FG_RED, "[ERROR] "),
+                format, forward<Args>(args)...);
         }
     }
 
     template<typename... Args>
     void logWarning(const FormatLocation& format, Args&&... args) noexcept {
         if constexpr(LOG_LEVEL >= 2) {
-            String<32> s;
-            s.addFormat("{}[WARNING] ", Terminal::FG_BRIGHT_YELLOW);
-            log(LogLevel::WARNING, s, format, forward<Args>(args)...);
+            log(LogLevel::WARNING,
+                mergeStrings(Terminal::FG_BRIGHT_YELLOW, "[WARNING] "), format,
+                forward<Args>(args)...);
         }
     }
 
     template<typename... Args>
     void logInfo(const FormatLocation& format, Args&&... args) noexcept {
         if constexpr(LOG_LEVEL >= 3) {
-            String<32> s;
-            s.addFormat("{}[INFO] ", Terminal::BOLD);
-            log(LogLevel::INFO, s, format, forward<Args>(args)...);
+            log(LogLevel::INFO, mergeStrings(Terminal::BOLD, "[INFO] "), format,
+                forward<Args>(args)...);
         }
     }
 
     template<typename... Args>
     void logDebug(const FormatLocation& format, Args&&... args) noexcept {
         if constexpr(LOG_LEVEL >= 4) {
-            String<32> s;
-            s.addFormat(
-                "{}{}[DEBUG] ", Terminal::BOLD, Terminal::FG_BRIGHT_BLACK);
-            log(LogLevel::DEBUG, s, format, forward<Args>(args)...);
+            log(LogLevel::DEBUG,
+                mergeStrings(
+                    Terminal::FG_BRIGHT_BLACK, Terminal::BOLD, "[DEBUG] "),
+                format, forward<Args>(args)...);
         }
     }
 }

+ 51 - 0
modules/StringJoin.cppm

@@ -0,0 +1,51 @@
+export module Core.StringJoin;
+
+import Core.Meta;
+
+namespace Core {
+    template<int N>
+    struct MergedString {
+        char s[N];
+
+        operator const char*() const noexcept {
+            return s;
+        }
+    };
+
+    template<int N1, int N2>
+    consteval auto mergeStringsBase(const char (&a)[N1], const char (&b)[N2]) {
+        static_assert(N1 > 0);
+        MergedString<N1 + N2 - 1> s;
+        for(int i = 0; i < N1; i++) {
+            s.s[i] = a[i];
+        }
+        for(int i = 0; i < N2; i++) {
+            s.s[i + N1 - 1] = b[i];
+        }
+        return s;
+    }
+
+    template<int N1, int N2>
+    consteval auto mergeStringsBase(
+        const MergedString<N1>& a, const char (&b)[N2]) {
+        return mergeStringsBase<N1, N2>(a.s, b);
+    }
+
+    template<int N1, int N2>
+    consteval auto mergeStringsBase(
+        const char (&a)[N1], const MergedString<N2>& b) {
+        return mergeStringsBase<N1, N2>(a, b.s);
+    }
+
+    export template<typename A, typename B>
+    consteval auto mergeStrings(const A& a, const B& b) {
+        return mergeStringsBase(a, b);
+    }
+
+    export template<typename A, typename B, typename... Args>
+    consteval auto mergeStrings(const A& a, const B& b, Args&&... args) {
+        auto c = mergeStringsBase(a, b);
+        return mergeStrings<decltype(c), Args...>(
+            c, Core::forward<Args>(args)...);
+    }
+}

+ 2 - 0
test/Main.cpp

@@ -96,6 +96,8 @@ int main(int argAmount, const char** args) {
     Core::logLevel = Core::LogLevel::WARNING;
     Core::logDebug("You won't see this!");
     Core::logLevel = Core::LogLevel::DEBUG;
+    Core::logDebug("Some debug message");
+    Core::logInfo("Great information");
 
     unsigned int data = 123'456'789;
     Core::setExitHandler(onExit, &data);