فهرست منبع

constexpr string

sunnycase 6 سال پیش
والد
کامیت
e44516322b
2فایلهای تغییر یافته به همراه11 افزوده شده و 8 حذف شده
  1. 9 6
      src/Native/natsu.runtime.h
  2. 2 2
      src/Natsu.Compiler/Program.cs

+ 9 - 6
src/Native/natsu.runtime.h

@@ -1355,14 +1355,17 @@ template <size_t N>
 struct string_literal : public System_Private_CorLib::System::Object
 {
     ::System_Private_CorLib::System::Int32 _stringLength;
-    ::System_Private_CorLib::System::Char _firstChar[N + 1];
+    std::array<::System_Private_CorLib::System::Char, N + 1> _firstChar;
 
-    constexpr string_literal(const char16_t *str)
-        : _stringLength((int32_t)N)
+    constexpr string_literal(std::u16string_view str)
+        : _stringLength((int32_t)N), _firstChar(init_array(str, std::make_index_sequence<N>()))
     {
-        for (size_t i = 0; i < N; i++)
-            _firstChar[i] = str[i];
-        _firstChar[N] = 0;
+    }
+
+    template <size_t... I>
+    constexpr std::array<::System_Private_CorLib::System::Char, N + 1> init_array(std::u16string_view str, std::index_sequence<I...>)
+    {
+        return { str[I]..., 0 };
     }
 };
 

+ 2 - 2
src/Natsu.Compiler/Program.cs

@@ -18,7 +18,7 @@ namespace Natsu.Compiler
             @"..\..\..\..\..\out\bin\netcoreapp3.0\Chino.Core.dll",
             //@"..\..\..\..\..\out\bin\netcoreapp3.0\Chino.Chip.K210.dll",
             @"..\..\..\..\..\out\bin\netcoreapp3.0\Chino.Chip.Emulator.dll",
-            //@"..\..\..\..\..\out\bin\netcoreapp3.0\System.Private.CorLib.dll",
+            @"..\..\..\..\..\out\bin\netcoreapp3.0\System.Private.CorLib.dll",
             //@"..\..\..\..\..\out\bin\netcoreapp3.0\System.Collections.dll",
             //@"..\..\..\..\..\out\bin\netcoreapp3.0\System.Memory.dll",
             //@"..\..\..\..\..\out\bin\netcoreapp3.0\System.Runtime.dll",
@@ -153,7 +153,7 @@ namespace Natsu.Compiler
         {
             for (int i = 0; i < _userStrings.Count; i++)
             {
-                writer.Ident(1).WriteLine($"static const natsu::static_object<::System_Private_CorLib::System::String, natsu::string_literal<{_userStrings[i].Length}>> user_string_{i}(uR\"NS({_userStrings[i]})NS\");");
+                writer.Ident(1).WriteLine($"static const constexpr natsu::static_object<::System_Private_CorLib::System::String, natsu::string_literal<{_userStrings[i].Length}>> user_string_{i}(uR\"NS({_userStrings[i]})NS\"sv);");
             }
 
             writer.WriteLine();