From 76b49f9ee6d9c51cebc8abbb37ef326e08b2a9ea Mon Sep 17 00:00:00 2001
From: Francois Chabot <francois@ai-r.com>
Date: Thu, 28 May 2020 02:29:53 -0400
Subject: [PATCH] misc formatting fixes

---
 .clang-format                                 | 137 ++++++++++++++++++
 .../nlohmann/detail/input/input_adapters.hpp  |  47 +++---
 include/nlohmann/json.hpp                     |  10 +-
 single_include/nlohmann/json.hpp              |  57 ++++----
 4 files changed, 199 insertions(+), 52 deletions(-)
 create mode 100644 .clang-format

diff --git a/.clang-format b/.clang-format
new file mode 100644
index 00000000..ef5f80c1
--- /dev/null
+++ b/.clang-format
@@ -0,0 +1,137 @@
+---
+Language:        Cpp
+# BasedOnStyle:  Mozilla
+AccessModifierOffset: -2
+AlignAfterOpenBracket: Align
+AlignConsecutiveMacros: false
+AlignConsecutiveAssignments: false
+AlignConsecutiveDeclarations: false
+AlignEscapedNewlines: Right
+AlignOperands:   true
+AlignTrailingComments: true
+AllowAllArgumentsOnNextLine: true
+AllowAllConstructorInitializersOnNextLine: true
+AllowAllParametersOfDeclarationOnNextLine: false
+AllowShortBlocksOnASingleLine: Never
+AllowShortCaseLabelsOnASingleLine: false
+AllowShortFunctionsOnASingleLine: None
+AllowShortLambdasOnASingleLine: All
+AllowShortIfStatementsOnASingleLine: Never
+AllowShortLoopsOnASingleLine: false
+AlwaysBreakAfterDefinitionReturnType: TopLevel
+AlwaysBreakAfterReturnType: TopLevel
+AlwaysBreakBeforeMultilineStrings: false
+AlwaysBreakTemplateDeclarations: Yes
+BinPackArguments: false
+BinPackParameters: false
+BraceWrapping:
+  AfterCaseLabel:  true
+  AfterClass:      true
+  AfterControlStatement: true
+  AfterEnum:       true
+  AfterFunction:   true
+  AfterNamespace:  true
+  AfterObjCDeclaration: false
+  AfterStruct:     true
+  AfterUnion:      true
+  AfterExternBlock: true
+  BeforeCatch:     false
+  BeforeElse:      true
+  IndentBraces:    false
+  SplitEmptyFunction: true
+  SplitEmptyRecord: false
+  SplitEmptyNamespace: true
+BreakBeforeBinaryOperators: None
+BreakBeforeBraces: Custom
+BreakBeforeInheritanceComma: false
+BreakInheritanceList: BeforeComma
+BreakBeforeTernaryOperators: true
+BreakConstructorInitializersBeforeComma: false
+BreakConstructorInitializers: BeforeComma
+BreakAfterJavaFieldAnnotations: false
+BreakStringLiterals: false
+ColumnLimit:     180
+CommentPragmas:  '^ IWYU pragma:'
+CompactNamespaces: false
+ConstructorInitializerAllOnOneLineOrOnePerLine: true
+ConstructorInitializerIndentWidth: 4
+ContinuationIndentWidth: 4
+Cpp11BracedListStyle: true
+DeriveLineEnding: true
+DerivePointerAlignment: false
+DisableFormat:   false
+ExperimentalAutoDetectBinPacking: false
+FixNamespaceComments: false
+ForEachMacros:
+  - foreach
+  - Q_FOREACH
+  - BOOST_FOREACH
+IncludeBlocks:   Preserve
+IncludeCategories:
+  - Regex:           '^"(llvm|llvm-c|clang|clang-c)/'
+    Priority:        2
+    SortPriority:    0
+  - Regex:           '^(<|"(gtest|gmock|isl|json)/)'
+    Priority:        3
+    SortPriority:    0
+  - Regex:           '.*'
+    Priority:        1
+    SortPriority:    0
+IncludeIsMainRegex: '(Test)?$'
+IncludeIsMainSourceRegex: ''
+IndentCaseLabels: true
+IndentGotoLabels: false
+IndentPPDirectives: None
+IndentWidth:     4
+IndentWrappedFunctionNames: false
+JavaScriptQuotes: Leave
+JavaScriptWrapImports: true
+KeepEmptyLinesAtTheStartOfBlocks: true
+MacroBlockBegin: ''
+MacroBlockEnd:   ''
+MaxEmptyLinesToKeep: 1
+NamespaceIndentation: None
+ObjCBinPackProtocolList: Auto
+ObjCBlockIndentWidth: 2
+ObjCSpaceAfterProperty: true
+ObjCSpaceBeforeProtocolList: false
+PenaltyBreakAssignment: 2
+PenaltyBreakBeforeFirstCallParameter: 19
+PenaltyBreakComment: 300
+PenaltyBreakFirstLessLess: 120
+PenaltyBreakString: 1000
+PenaltyBreakTemplateDeclaration: 10
+PenaltyExcessCharacter: 1000000
+PenaltyReturnTypeOnItsOwnLine: 200
+PointerAlignment: Left
+ReflowComments:  true
+SortIncludes:    false
+SortUsingDeclarations: true
+SpaceAfterCStyleCast: false
+SpaceAfterLogicalNot: false
+SpaceAfterTemplateKeyword: false
+SpaceBeforeAssignmentOperators: true
+SpaceBeforeCpp11BracedList: true
+SpaceBeforeCtorInitializerColon: true
+SpaceBeforeInheritanceColon: true
+SpaceBeforeParens: ControlStatements
+SpaceBeforeRangeBasedForLoopColon: true
+SpaceInEmptyBlock: false
+SpaceInEmptyParentheses: false
+SpacesBeforeTrailingComments: 1
+SpacesInAngles:  false
+SpacesInConditionalStatement: false
+SpacesInContainerLiterals: true
+SpacesInCStyleCastParentheses: false
+SpacesInParentheses: false
+SpacesInSquareBrackets: false
+SpaceBeforeSquareBrackets: false
+Standard:        Latest
+StatementMacros:
+  - Q_UNUSED
+  - QT_REQUIRE_VERSION
+TabWidth:        8
+UseCRLF:         false
+UseTab:          Never
+...
+
diff --git a/include/nlohmann/detail/input/input_adapters.hpp b/include/nlohmann/detail/input/input_adapters.hpp
index 0a52abaa..08954a2f 100644
--- a/include/nlohmann/detail/input/input_adapters.hpp
+++ b/include/nlohmann/detail/input/input_adapters.hpp
@@ -104,7 +104,7 @@ class input_stream_adapter
     {
         auto res = sb->sbumpc();
         // set eof manually, as we don't use the istream interface.
-        if (res == EOF)
+        if (JSON_HEDLEY_UNLIKELY(res == EOF))
         {
             is->clear(is->rdstate() | std::ios::eofbit);
         }
@@ -130,7 +130,7 @@ class iterator_input_adapter
 
     typename std::char_traits<char_type>::int_type get_character()
     {
-        if (current != end)
+        if (JSON_HEDLEY_LIKELY(current != end))
         {
             auto result = std::char_traits<char_type>::to_int_type(*current);
             std::advance(current, 1);
@@ -171,7 +171,7 @@ struct wide_string_input_helper<BaseInputAdapter, 4>
     {
         utf8_bytes_index = 0;
 
-        if (input.empty())
+        if (JSON_HEDLEY_UNLIKELY(input.empty()))
         {
             utf8_bytes[0] = std::char_traits<char>::eof();
             utf8_bytes_filled = 1;
@@ -229,7 +229,7 @@ struct wide_string_input_helper<BaseInputAdapter, 2>
     {
         utf8_bytes_index = 0;
 
-        if (input.empty())
+        if (JSON_HEDLEY_UNLIKELY(input.empty()))
         {
             utf8_bytes[0] = std::char_traits<char>::eof();
             utf8_bytes_filled = 1;
@@ -331,34 +331,39 @@ struct iterator_input_adapter_factory
     using char_type = typename std::iterator_traits<iterator_type>::value_type;
     using adapter_type = iterator_input_adapter<iterator_type>;
 
-    static adapter_type create(IteratorType begin, IteratorType end)
+    static adapter_type create(IteratorType first, IteratorType last)
     {
-        return adapter_type(std::move(begin), std::move(end));
+        return adapter_type(std::move(first), std::move(last));
     }
 };
 
-template<typename IteratorType>
-struct iterator_input_adapter_factory<IteratorType,
-       typename std::enable_if<(sizeof(typename std::iterator_traits<IteratorType>::value_type)>1)>::type >
-       {
-
-           using iterator_type = IteratorType;
-           using char_type = typename std::iterator_traits<iterator_type>::value_type;
-           using base_adapter_type = iterator_input_adapter<iterator_type>;
-           using adapter_type = wide_string_input_adapter<base_adapter_type, char_type>;
-
-           static adapter_type create(IteratorType begin, IteratorType end)
+// This test breaks astyle formatting when inlined in a template specialization.
+template<typename T>
+inline constexpr bool is_iterator_of_multibyte()
 {
-    return adapter_type(base_adapter_type(std::move(begin), std::move(end)));
+    return sizeof(typename std::iterator_traits<T>::value_type) > 1;
 }
-       };
+
+template<typename IteratorType>
+struct iterator_input_adapter_factory<IteratorType, enable_if_t<is_iterator_of_multibyte<IteratorType>()>>
+{
+    using iterator_type = IteratorType;
+    using char_type = typename std::iterator_traits<iterator_type>::value_type;
+    using base_adapter_type = iterator_input_adapter<iterator_type>;
+    using adapter_type = wide_string_input_adapter<base_adapter_type, char_type>;
+
+    static adapter_type create(IteratorType first, IteratorType last)
+    {
+        return adapter_type(base_adapter_type(std::move(first), std::move(last)));
+    }
+};
 
 // General purpose iterator-based input
 template<typename IteratorType>
-typename iterator_input_adapter_factory<IteratorType>::adapter_type input_adapter(IteratorType begin, IteratorType end)
+typename iterator_input_adapter_factory<IteratorType>::adapter_type input_adapter(IteratorType first, IteratorType last)
 {
     using factory_type = iterator_input_adapter_factory<IteratorType>;
-    return factory_type::create(begin, end);
+    return factory_type::create(first, last);
 }
 
 // Convenience shorthand from container to iterator
diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp
index 145c4f43..1248c956 100644
--- a/include/nlohmann/json.hpp
+++ b/include/nlohmann/json.hpp
@@ -6623,13 +6623,13 @@ class basic_json
 
     template<typename IteratorType>
     JSON_HEDLEY_WARN_UNUSED_RESULT
-    static basic_json parse(IteratorType begin,
-                            IteratorType end,
+    static basic_json parse(IteratorType first,
+                            IteratorType last,
                             const parser_callback_t cb = nullptr,
                             const bool allow_exceptions = true)
     {
         basic_json result;
-        parser(detail::input_adapter(std::move(begin), std::move(end)), cb, allow_exceptions).parse(true, result);
+        parser(detail::input_adapter(std::move(first), std::move(last)), cb, allow_exceptions).parse(true, result);
         return result;
     }
 
@@ -6650,9 +6650,9 @@ class basic_json
     }
 
     template<typename IteratorType>
-    static bool accept(IteratorType begin, IteratorType end)
+    static bool accept(IteratorType first, IteratorType last)
     {
-        return parser(detail::input_adapter(std::move(begin), std::move(end))).accept(true);
+        return parser(detail::input_adapter(std::move(first), std::move(last))).accept(true);
     }
 
     static bool accept(detail::span_input_adapter&& i)
diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp
index 5e8e23fe..cb482bc2 100644
--- a/single_include/nlohmann/json.hpp
+++ b/single_include/nlohmann/json.hpp
@@ -4526,7 +4526,7 @@ class input_stream_adapter
     {
         auto res = sb->sbumpc();
         // set eof manually, as we don't use the istream interface.
-        if (res == EOF)
+        if (JSON_HEDLEY_UNLIKELY(res == EOF))
         {
             is->clear(is->rdstate() | std::ios::eofbit);
         }
@@ -4552,7 +4552,7 @@ class iterator_input_adapter
 
     typename std::char_traits<char_type>::int_type get_character()
     {
-        if (current != end)
+        if (JSON_HEDLEY_LIKELY(current != end))
         {
             auto result = std::char_traits<char_type>::to_int_type(*current);
             std::advance(current, 1);
@@ -4593,7 +4593,7 @@ struct wide_string_input_helper<BaseInputAdapter, 4>
     {
         utf8_bytes_index = 0;
 
-        if (input.empty())
+        if (JSON_HEDLEY_UNLIKELY(input.empty()))
         {
             utf8_bytes[0] = std::char_traits<char>::eof();
             utf8_bytes_filled = 1;
@@ -4651,7 +4651,7 @@ struct wide_string_input_helper<BaseInputAdapter, 2>
     {
         utf8_bytes_index = 0;
 
-        if (input.empty())
+        if (JSON_HEDLEY_UNLIKELY(input.empty()))
         {
             utf8_bytes[0] = std::char_traits<char>::eof();
             utf8_bytes_filled = 1;
@@ -4753,34 +4753,39 @@ struct iterator_input_adapter_factory
     using char_type = typename std::iterator_traits<iterator_type>::value_type;
     using adapter_type = iterator_input_adapter<iterator_type>;
 
-    static adapter_type create(IteratorType begin, IteratorType end)
+    static adapter_type create(IteratorType first, IteratorType last)
     {
-        return adapter_type(std::move(begin), std::move(end));
+        return adapter_type(std::move(first), std::move(last));
     }
 };
 
-template<typename IteratorType>
-struct iterator_input_adapter_factory<IteratorType,
-       typename std::enable_if<(sizeof(typename std::iterator_traits<IteratorType>::value_type)>1)>::type >
-       {
-
-           using iterator_type = IteratorType;
-           using char_type = typename std::iterator_traits<iterator_type>::value_type;
-           using base_adapter_type = iterator_input_adapter<iterator_type>;
-           using adapter_type = wide_string_input_adapter<base_adapter_type, char_type>;
-
-           static adapter_type create(IteratorType begin, IteratorType end)
+// This test breaks astyle formatting when inlined in a template specialization.
+template<typename T>
+inline constexpr bool is_iterator_of_multibyte()
 {
-    return adapter_type(base_adapter_type(std::move(begin), std::move(end)));
+    return sizeof(typename std::iterator_traits<T>::value_type) > 1;
 }
-       };
+
+template<typename IteratorType>
+struct iterator_input_adapter_factory<IteratorType, enable_if_t<is_iterator_of_multibyte<IteratorType>()>>
+{
+    using iterator_type = IteratorType;
+    using char_type = typename std::iterator_traits<iterator_type>::value_type;
+    using base_adapter_type = iterator_input_adapter<iterator_type>;
+    using adapter_type = wide_string_input_adapter<base_adapter_type, char_type>;
+
+    static adapter_type create(IteratorType first, IteratorType last)
+    {
+        return adapter_type(base_adapter_type(std::move(first), std::move(last)));
+    }
+};
 
 // General purpose iterator-based input
 template<typename IteratorType>
-typename iterator_input_adapter_factory<IteratorType>::adapter_type input_adapter(IteratorType begin, IteratorType end)
+typename iterator_input_adapter_factory<IteratorType>::adapter_type input_adapter(IteratorType first, IteratorType last)
 {
     using factory_type = iterator_input_adapter_factory<IteratorType>;
-    return factory_type::create(begin, end);
+    return factory_type::create(first, last);
 }
 
 // Convenience shorthand from container to iterator
@@ -22381,13 +22386,13 @@ class basic_json
 
     template<typename IteratorType>
     JSON_HEDLEY_WARN_UNUSED_RESULT
-    static basic_json parse(IteratorType begin,
-                            IteratorType end,
+    static basic_json parse(IteratorType first,
+                            IteratorType last,
                             const parser_callback_t cb = nullptr,
                             const bool allow_exceptions = true)
     {
         basic_json result;
-        parser(detail::input_adapter(std::move(begin), std::move(end)), cb, allow_exceptions).parse(true, result);
+        parser(detail::input_adapter(std::move(first), std::move(last)), cb, allow_exceptions).parse(true, result);
         return result;
     }
 
@@ -22408,9 +22413,9 @@ class basic_json
     }
 
     template<typename IteratorType>
-    static bool accept(IteratorType begin, IteratorType end)
+    static bool accept(IteratorType first, IteratorType last)
     {
-        return parser(detail::input_adapter(std::move(begin), std::move(end))).accept(true);
+        return parser(detail::input_adapter(std::move(first), std::move(last))).accept(true);
     }
 
     static bool accept(detail::span_input_adapter&& i)