瀏覽代碼

AK: Add optional format string parameter to String{,Builder}::join()

Allow specifying a custom format string that's being used for each item
instead of hardcoding "{}".
Linus Groh 3 年之前
父節點
當前提交
b253bca807
共有 2 個文件被更改,包括 4 次插入4 次删除
  1. 2 2
      AK/String.h
  2. 2 2
      AK/StringBuilder.h

+ 2 - 2
AK/String.h

@@ -102,10 +102,10 @@ public:
     [[nodiscard]] static String roman_number_from(size_t value);
     [[nodiscard]] static String roman_number_from(size_t value);
 
 
     template<class SeparatorType, class CollectionType>
     template<class SeparatorType, class CollectionType>
-    [[nodiscard]] static String join(const SeparatorType& separator, const CollectionType& collection)
+    [[nodiscard]] static String join(const SeparatorType& separator, const CollectionType& collection, StringView fmtstr = "{}"sv)
     {
     {
         StringBuilder builder;
         StringBuilder builder;
-        builder.join(separator, collection);
+        builder.join(separator, collection, fmtstr);
         return builder.build();
         return builder.build();
     }
     }
 
 

+ 2 - 2
AK/StringBuilder.h

@@ -70,7 +70,7 @@ public:
     void trim(size_t count) { m_buffer.resize(m_buffer.size() - count); }
     void trim(size_t count) { m_buffer.resize(m_buffer.size() - count); }
 
 
     template<class SeparatorType, class CollectionType>
     template<class SeparatorType, class CollectionType>
-    void join(SeparatorType const& separator, CollectionType const& collection)
+    void join(SeparatorType const& separator, CollectionType const& collection, StringView fmtstr = "{}"sv)
     {
     {
         bool first = true;
         bool first = true;
         for (auto& item : collection) {
         for (auto& item : collection) {
@@ -78,7 +78,7 @@ public:
                 first = false;
                 first = false;
             else
             else
                 append(separator);
                 append(separator);
-            appendff("{}", item);
+            appendff(fmtstr, item);
         }
         }
     }
     }