33#include < string_view>
44#include < variant>
55#include < format>
6+ #include < strnatcmp.h>
67
78#if (defined(__GNUC__) && __GNUC__ >= 13) || (defined(__clang__) && __clang_major__ >= 20) || (defined(_MSC_VER) && _MSC_VER >= 1910)
89#include < charconv>
@@ -54,7 +55,7 @@ preprocessor::compare_result preprocessor::compare_symbols(const symbol& a, cons
5455 if (method.type ==order_t ::type_t ::DEFAULT){
5556 if (std::holds_alternative<int >(a))method.type =order_t ::type_t ::INTEGER;
5657 else if (std::holds_alternative<float >(a))method.type =order_t ::type_t ::FLOAT;
57- else if (std::holds_alternative<std::string>(a))method.type =order_t ::type_t ::STRING; // TODO: move to `order_t::type_t:: NATURAL_STRING`
58+ else if (std::holds_alternative<std::string>(a))method.type =order_t ::type_t ::NATURAL_STRING;
5859 else if (std::holds_alternative<const pugi::xml_node>(a))method.type =order_t ::type_t ::NODE;
5960 }
6061
@@ -79,12 +80,11 @@ preprocessor::compare_result preprocessor::compare_symbols(const symbol& a, cons
7980 else if (std::holds_alternative<const pugi::xml_node>(a) && std::holds_alternative<const pugi::xml_node>(b) && method.type ==order_t ::type_t ::NODE){
8081
8182 }
82- else if (std::holds_alternative<std::string>(a) && std::holds_alternative<std::string>(b) && method.type ==order_t ::type_t ::STRING){
83+ else if (std::holds_alternative<std::string>(a) && std::holds_alternative<std::string>(b) && ( method.type ==order_t ::type_t ::STRING || method. type == order_t :: type_t ::NATURAL_STRING ) ){
8384 const auto & _a = std::get<std::string>(a), _b = std::get<std::string>(b);
8485 if (method.modifiers .dot ){
85- auto i = cmp_dot_str (_a.c_str (), _b.c_str ());
86- if (method.method ==order_t ::method_t ::ASC) return (i<0 )?compare_result::LESS: ( (i>0 )? compare_result::BIGGER : compare_result::EQUAL);
87- else if (method.method ==order_t ::method_t ::DESC) return (i<0 )?compare_result::BIGGER: ( (i>0 )? compare_result::LESS : compare_result::EQUAL);
86+ if (method.method ==order_t ::method_t ::ASC){auto i = (method.type ==order_t ::type_t ::NATURAL_STRING)?cmp_dot_natstr (_a.c_str (), _b.c_str ()):cmp_dot_str (_a.c_str (), _b.c_str ());return (i<0 )?compare_result::LESS: ( (i>0 )? compare_result::BIGGER : compare_result::EQUAL);}
87+ else if (method.method ==order_t ::method_t ::DESC){auto i = -((method.type ==order_t ::type_t ::NATURAL_STRING)?cmp_dot_natstr (_a.c_str (), _b.c_str ()):cmp_dot_str (_a.c_str (), _b.c_str ())); return (i<0 )?compare_result::LESS: ( (i>0 )? compare_result::BIGGER : compare_result::EQUAL);}
8888 else if (method.method ==order_t ::method_t ::RANDOM){
8989 auto va = split_string (std::get<std::string>(a).c_str (), ' .' );
9090 auto vb = split_string (std::get<std::string>(b).c_str (), ' .' );
@@ -106,8 +106,8 @@ preprocessor::compare_result preprocessor::compare_symbols(const symbol& a, cons
106106 }
107107 }
108108 else {
109- if (method.method ==order_t ::method_t ::ASC) return (_a<_b) ?compare_result::LESS: ( (_a>_b )? compare_result::BIGGER : compare_result::EQUAL);
110- else if (method.method ==order_t ::method_t ::DESC) return (_a<_b) ?compare_result::BIGGER : ( (_a>_b )? compare_result::LESS : compare_result::EQUAL);
109+ if (method.method ==order_t ::method_t ::ASC){ auto cmp = (method. type == order_t :: type_t ::NATURAL_STRING)? strnatcmp (_a. c_str (),_b. c_str ()): strcmp (_a. c_str (),_b. c_str ()); return (cmp< 0 ) ?compare_result::LESS: ( (cmp> 0 )? compare_result::BIGGER : compare_result::EQUAL);}
110+ else if (method.method ==order_t ::method_t ::DESC){ auto cmp = -((method. type == order_t :: type_t ::NATURAL_STRING)? strnatcmp (_a. c_str (),_b. c_str ()): strcmp (_a. c_str (),_b. c_str ())); return (cmp< 0 ) ?compare_result::LESS : ( (cmp> 0 )? compare_result::BIGGER : compare_result::EQUAL);}
111111 else if (method.method ==order_t ::method_t ::RANDOM){
112112 auto hash_a = preprocessor::hash (a);
113113 auto hash_b = preprocessor::hash (b);
@@ -116,10 +116,7 @@ preprocessor::compare_result preprocessor::compare_symbols(const symbol& a, cons
116116 }
117117 }
118118 else if (std::holds_alternative<std::string>(a) && std::holds_alternative<std::string>(b) && method.type ==order_t ::type_t ::LEXI_STRING){
119-
120- }
121- else if (std::holds_alternative<std::string>(a) && std::holds_alternative<std::string>(b) && method.type ==order_t ::type_t ::NATURAL_STRING){
122- // https://github.com/sourcefrog/natsort
119+ // TODO: not implemented
123120 }
124121 // Cannot have pugi::xml_attribute since it has been resolved by the time it gets here.
125122
0 commit comments