diff options
Diffstat (limited to 'include/jsoncons/detail')
-rw-r--r-- | include/jsoncons/detail/endian.hpp | 44 | ||||
-rw-r--r-- | include/jsoncons/detail/grisu3.hpp | 312 | ||||
-rw-r--r-- | include/jsoncons/detail/optional.hpp | 483 | ||||
-rw-r--r-- | include/jsoncons/detail/parse_number.hpp | 1133 | ||||
-rw-r--r-- | include/jsoncons/detail/span.hpp | 188 | ||||
-rw-r--r-- | include/jsoncons/detail/string_view.hpp | 537 | ||||
-rw-r--r-- | include/jsoncons/detail/string_wrapper.hpp | 370 | ||||
-rw-r--r-- | include/jsoncons/detail/write_number.hpp | 567 |
8 files changed, 3634 insertions, 0 deletions
diff --git a/include/jsoncons/detail/endian.hpp b/include/jsoncons/detail/endian.hpp new file mode 100644 index 0000000..d47a0f7 --- /dev/null +++ b/include/jsoncons/detail/endian.hpp @@ -0,0 +1,44 @@ +// Copyright 2020 Daniel Parker +// Distributed under the Boost license, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +// See https://github.com/danielaparker/jsoncons for latest version + +#ifndef JSONCONS_DETAIL_ENDIAN_HPP +#define JSONCONS_DETAIL_ENDIAN_HPP + +#if defined(__sun) +# include <sys/byteorder.h> +#endif + +namespace jsoncons { +namespace detail { + + enum class endian + { + #if defined(_MSC_VER) + // MSVC, which implies Windows, which implies little-endian + little = 0, + big = 1, + native = little + #elif defined(__ORDER_LITTLE_ENDIAN__) && defined(__ORDER_BIG_ENDIAN__) && defined(__BYTE_ORDER__) + little = __ORDER_LITTLE_ENDIAN__, + big = __ORDER_BIG_ENDIAN__, + native = __BYTE_ORDER__ + #elif defined(_BIG_ENDIAN) && !defined(_LITTLE_ENDIAN) + little = 0, + big = 1, + native = big + #elif !defined(_BIG_ENDIAN) && defined(_LITTLE_ENDIAN) + little = 0, + big = 1, + native = little + #else + #error "Unable to determine byte order!" + #endif + }; + +} // namespace detail +} // namespace jsoncons + +#endif diff --git a/include/jsoncons/detail/grisu3.hpp b/include/jsoncons/detail/grisu3.hpp new file mode 100644 index 0000000..771cf1d --- /dev/null +++ b/include/jsoncons/detail/grisu3.hpp @@ -0,0 +1,312 @@ +/* +Implements the Grisu3 algorithm for printing floating-point numbers. + +Follows Florian Loitsch's grisu3_59_56 implementation, available at +http://florian.loitsch.com/publications, in bench.tar.gz, with +minor modifications. +*/ + +/* + Copyright (c) 2009 Florian Loitsch + + Permission is hereby granted, free of charge, to any person + obtaining a copy of this software and associated documentation + files (the "Software"), to deal in the Software without + restriction, including without limitation the rights to use, + copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the + Software is furnished to do so, subject to the following + conditions: + + The above copyright notice and this permission notice shall be + included in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + OTHER DEALINGS IN THE SOFTWARE. + */ + +#ifndef JSONCONS_GRISU3_HPP +#define JSONCONS_GRISU3_HPP + +#pragma once +#include <stdlib.h> +#include <cinttypes> +#include <cstdint> +#include <cassert> +#include <cmath> +#include <cstring> // std::memmove +#include <jsoncons/detail/write_number.hpp> + +namespace jsoncons { namespace detail { + +// diy_fp + +typedef struct diy_fp_t { + uint64_t f; + int e; +} diy_fp_t; + +inline +diy_fp_t minus(diy_fp_t x, diy_fp_t y) +{ + assert(x.e == y.e); + assert(x.f >= y.f); + diy_fp_t r = { x.f = x.f - y.f, x.e }; + return r; +} + +inline +diy_fp_t multiply(diy_fp_t x, diy_fp_t y) +{ + uint64_t a, b, c, d, ac, bc, ad, bd, tmp; + diy_fp_t r; uint64_t M32 = 0xFFFFFFFF; + a = x.f >> 32; b = x.f & M32; + c = y.f >> 32; d = y.f & M32; + ac = a * c; bc = b * c; ad = a * d; bd = b * d; + tmp = (bd >> 32) + (ad & M32) + (bc & M32); + tmp += 1U << 31; /// mult_round + r.f = ac + (ad >> 32) + (bc >> 32) + (tmp >> 32); + r.e = x.e + y.e + 64; + return r; +} + +// k_comp + +inline +int k_comp(int e, int alpha, int /*gamma*/) +{ + constexpr double d_1_log2_10 = 0.30102999566398114; // 1 / lg(10) + int x = alpha - e + 63; + return static_cast<int>(std::ceil(x * d_1_log2_10)); +} + +// powers_ten_round64 + +constexpr int diy_significand_size = 64; + +static constexpr uint64_t powers_ten[] = { 0xbf29dcaba82fdeae, 0xeef453d6923bd65a, 0x9558b4661b6565f8, 0xbaaee17fa23ebf76, 0xe95a99df8ace6f54, 0x91d8a02bb6c10594, 0xb64ec836a47146fa, 0xe3e27a444d8d98b8, 0x8e6d8c6ab0787f73, 0xb208ef855c969f50, 0xde8b2b66b3bc4724, 0x8b16fb203055ac76, 0xaddcb9e83c6b1794, 0xd953e8624b85dd79, 0x87d4713d6f33aa6c, 0xa9c98d8ccb009506, 0xd43bf0effdc0ba48, 0x84a57695fe98746d, 0xa5ced43b7e3e9188, 0xcf42894a5dce35ea, 0x818995ce7aa0e1b2, 0xa1ebfb4219491a1f, 0xca66fa129f9b60a7, 0xfd00b897478238d1, 0x9e20735e8cb16382, 0xc5a890362fddbc63, 0xf712b443bbd52b7c, 0x9a6bb0aa55653b2d, 0xc1069cd4eabe89f9, 0xf148440a256e2c77, 0x96cd2a865764dbca, 0xbc807527ed3e12bd, 0xeba09271e88d976c, 0x93445b8731587ea3, 0xb8157268fdae9e4c, 0xe61acf033d1a45df, 0x8fd0c16206306bac, 0xb3c4f1ba87bc8697, 0xe0b62e2929aba83c, 0x8c71dcd9ba0b4926, 0xaf8e5410288e1b6f, 0xdb71e91432b1a24b, 0x892731ac9faf056f, 0xab70fe17c79ac6ca, 0xd64d3d9db981787d, 0x85f0468293f0eb4e, 0xa76c582338ed2622, 0xd1476e2c07286faa, 0x82cca4db847945ca, 0xa37fce126597973d, 0xcc5fc196fefd7d0c, 0xff77b1fcbebcdc4f, 0x9faacf3df73609b1, 0xc795830d75038c1e, 0xf97ae3d0d2446f25, 0x9becce62836ac577, 0xc2e801fb244576d5, 0xf3a20279ed56d48a, 0x9845418c345644d7, 0xbe5691ef416bd60c, 0xedec366b11c6cb8f, 0x94b3a202eb1c3f39, 0xb9e08a83a5e34f08, 0xe858ad248f5c22ca, 0x91376c36d99995be, 0xb58547448ffffb2e, 0xe2e69915b3fff9f9, 0x8dd01fad907ffc3c, 0xb1442798f49ffb4b, 0xdd95317f31c7fa1d, 0x8a7d3eef7f1cfc52, 0xad1c8eab5ee43b67, 0xd863b256369d4a41, 0x873e4f75e2224e68, 0xa90de3535aaae202, 0xd3515c2831559a83, 0x8412d9991ed58092, 0xa5178fff668ae0b6, 0xce5d73ff402d98e4, 0x80fa687f881c7f8e, 0xa139029f6a239f72, 0xc987434744ac874f, 0xfbe9141915d7a922, 0x9d71ac8fada6c9b5, 0xc4ce17b399107c23, 0xf6019da07f549b2b, 0x99c102844f94e0fb, 0xc0314325637a193a, 0xf03d93eebc589f88, 0x96267c7535b763b5, 0xbbb01b9283253ca3, 0xea9c227723ee8bcb, 0x92a1958a7675175f, 0xb749faed14125d37, 0xe51c79a85916f485, 0x8f31cc0937ae58d3, 0xb2fe3f0b8599ef08, 0xdfbdcece67006ac9, 0x8bd6a141006042be, 0xaecc49914078536d, 0xda7f5bf590966849, 0x888f99797a5e012d, 0xaab37fd7d8f58179, 0xd5605fcdcf32e1d7, 0x855c3be0a17fcd26, 0xa6b34ad8c9dfc070, 0xd0601d8efc57b08c, 0x823c12795db6ce57, 0xa2cb1717b52481ed, 0xcb7ddcdda26da269, 0xfe5d54150b090b03, 0x9efa548d26e5a6e2, 0xc6b8e9b0709f109a, 0xf867241c8cc6d4c1, 0x9b407691d7fc44f8, 0xc21094364dfb5637, 0xf294b943e17a2bc4, 0x979cf3ca6cec5b5b, 0xbd8430bd08277231, 0xece53cec4a314ebe, 0x940f4613ae5ed137, 0xb913179899f68584, 0xe757dd7ec07426e5, 0x9096ea6f3848984f, 0xb4bca50b065abe63, 0xe1ebce4dc7f16dfc, 0x8d3360f09cf6e4bd, 0xb080392cc4349ded, 0xdca04777f541c568, 0x89e42caaf9491b61, 0xac5d37d5b79b6239, 0xd77485cb25823ac7, 0x86a8d39ef77164bd, 0xa8530886b54dbdec, 0xd267caa862a12d67, 0x8380dea93da4bc60, 0xa46116538d0deb78, 0xcd795be870516656, 0x806bd9714632dff6, 0xa086cfcd97bf97f4, 0xc8a883c0fdaf7df0, 0xfad2a4b13d1b5d6c, 0x9cc3a6eec6311a64, 0xc3f490aa77bd60fd, 0xf4f1b4d515acb93c, 0x991711052d8bf3c5, 0xbf5cd54678eef0b7, 0xef340a98172aace5, 0x9580869f0e7aac0f, 0xbae0a846d2195713, 0xe998d258869facd7, 0x91ff83775423cc06, 0xb67f6455292cbf08, 0xe41f3d6a7377eeca, 0x8e938662882af53e, 0xb23867fb2a35b28e, 0xdec681f9f4c31f31, 0x8b3c113c38f9f37f, 0xae0b158b4738705f, 0xd98ddaee19068c76, 0x87f8a8d4cfa417ca, 0xa9f6d30a038d1dbc, 0xd47487cc8470652b, 0x84c8d4dfd2c63f3b, 0xa5fb0a17c777cf0a, 0xcf79cc9db955c2cc, 0x81ac1fe293d599c0, 0xa21727db38cb0030, 0xca9cf1d206fdc03c, 0xfd442e4688bd304b, 0x9e4a9cec15763e2f, 0xc5dd44271ad3cdba, 0xf7549530e188c129, 0x9a94dd3e8cf578ba, 0xc13a148e3032d6e8, 0xf18899b1bc3f8ca2, 0x96f5600f15a7b7e5, 0xbcb2b812db11a5de, 0xebdf661791d60f56, 0x936b9fcebb25c996, 0xb84687c269ef3bfb, 0xe65829b3046b0afa, 0x8ff71a0fe2c2e6dc, 0xb3f4e093db73a093, 0xe0f218b8d25088b8, 0x8c974f7383725573, 0xafbd2350644eead0, 0xdbac6c247d62a584, 0x894bc396ce5da772, 0xab9eb47c81f5114f, 0xd686619ba27255a3, 0x8613fd0145877586, 0xa798fc4196e952e7, 0xd17f3b51fca3a7a1, 0x82ef85133de648c5, 0xa3ab66580d5fdaf6, 0xcc963fee10b7d1b3, 0xffbbcfe994e5c620, 0x9fd561f1fd0f9bd4, 0xc7caba6e7c5382c9, 0xf9bd690a1b68637b, 0x9c1661a651213e2d, 0xc31bfa0fe5698db8, 0xf3e2f893dec3f126, 0x986ddb5c6b3a76b8, 0xbe89523386091466, 0xee2ba6c0678b597f, 0x94db483840b717f0, 0xba121a4650e4ddec, 0xe896a0d7e51e1566, 0x915e2486ef32cd60, 0xb5b5ada8aaff80b8, 0xe3231912d5bf60e6, 0x8df5efabc5979c90, 0xb1736b96b6fd83b4, 0xddd0467c64bce4a1, 0x8aa22c0dbef60ee4, 0xad4ab7112eb3929e, 0xd89d64d57a607745, 0x87625f056c7c4a8b, 0xa93af6c6c79b5d2e, 0xd389b47879823479, 0x843610cb4bf160cc, 0xa54394fe1eedb8ff, 0xce947a3da6a9273e, 0x811ccc668829b887, 0xa163ff802a3426a9, 0xc9bcff6034c13053, 0xfc2c3f3841f17c68, 0x9d9ba7832936edc1, 0xc5029163f384a931, 0xf64335bcf065d37d, 0x99ea0196163fa42e, 0xc06481fb9bcf8d3a, 0xf07da27a82c37088, 0x964e858c91ba2655, 0xbbe226efb628afeb, 0xeadab0aba3b2dbe5, 0x92c8ae6b464fc96f, 0xb77ada0617e3bbcb, 0xe55990879ddcaabe, 0x8f57fa54c2a9eab7, 0xb32df8e9f3546564, 0xdff9772470297ebd, 0x8bfbea76c619ef36, 0xaefae51477a06b04, 0xdab99e59958885c5, 0x88b402f7fd75539b, 0xaae103b5fcd2a882, 0xd59944a37c0752a2, 0x857fcae62d8493a5, 0xa6dfbd9fb8e5b88f, 0xd097ad07a71f26b2, 0x825ecc24c8737830, 0xa2f67f2dfa90563b, 0xcbb41ef979346bca, 0xfea126b7d78186bd, 0x9f24b832e6b0f436, 0xc6ede63fa05d3144, 0xf8a95fcf88747d94, 0x9b69dbe1b548ce7d, 0xc24452da229b021c, 0xf2d56790ab41c2a3, 0x97c560ba6b0919a6, 0xbdb6b8e905cb600f, 0xed246723473e3813, 0x9436c0760c86e30c, 0xb94470938fa89bcf, 0xe7958cb87392c2c3, 0x90bd77f3483bb9ba, 0xb4ecd5f01a4aa828, 0xe2280b6c20dd5232, 0x8d590723948a535f, 0xb0af48ec79ace837, 0xdcdb1b2798182245, 0x8a08f0f8bf0f156b, 0xac8b2d36eed2dac6, 0xd7adf884aa879177, 0x86ccbb52ea94baeb, 0xa87fea27a539e9a5, 0xd29fe4b18e88640f, 0x83a3eeeef9153e89, 0xa48ceaaab75a8e2b, 0xcdb02555653131b6, 0x808e17555f3ebf12, 0xa0b19d2ab70e6ed6, 0xc8de047564d20a8c, 0xfb158592be068d2f, 0x9ced737bb6c4183d, 0xc428d05aa4751e4d, 0xf53304714d9265e0, 0x993fe2c6d07b7fac, 0xbf8fdb78849a5f97, 0xef73d256a5c0f77d, 0x95a8637627989aae, 0xbb127c53b17ec159, 0xe9d71b689dde71b0, 0x9226712162ab070e, 0xb6b00d69bb55c8d1, 0xe45c10c42a2b3b06, 0x8eb98a7a9a5b04e3, 0xb267ed1940f1c61c, 0xdf01e85f912e37a3, 0x8b61313bbabce2c6, 0xae397d8aa96c1b78, 0xd9c7dced53c72256, 0x881cea14545c7575, 0xaa242499697392d3, 0xd4ad2dbfc3d07788, 0x84ec3c97da624ab5, 0xa6274bbdd0fadd62, 0xcfb11ead453994ba, 0x81ceb32c4b43fcf5, 0xa2425ff75e14fc32, 0xcad2f7f5359a3b3e, 0xfd87b5f28300ca0e, 0x9e74d1b791e07e48, 0xc612062576589ddb, 0xf79687aed3eec551, 0x9abe14cd44753b53, 0xc16d9a0095928a27, 0xf1c90080baf72cb1, 0x971da05074da7bef, 0xbce5086492111aeb, 0xec1e4a7db69561a5, 0x9392ee8e921d5d07, 0xb877aa3236a4b449, 0xe69594bec44de15b, 0x901d7cf73ab0acd9, 0xb424dc35095cd80f, 0xe12e13424bb40e13, 0x8cbccc096f5088cc, 0xafebff0bcb24aaff, 0xdbe6fecebdedd5bf, 0x89705f4136b4a597, 0xabcc77118461cefd, 0xd6bf94d5e57a42bc, 0x8637bd05af6c69b6, 0xa7c5ac471b478423, 0xd1b71758e219652c, 0x83126e978d4fdf3b, 0xa3d70a3d70a3d70a, 0xcccccccccccccccd, 0x8000000000000000, 0xa000000000000000, 0xc800000000000000, 0xfa00000000000000, 0x9c40000000000000, 0xc350000000000000, 0xf424000000000000, 0x9896800000000000, 0xbebc200000000000, 0xee6b280000000000, 0x9502f90000000000, 0xba43b74000000000, 0xe8d4a51000000000, 0x9184e72a00000000, 0xb5e620f480000000, 0xe35fa931a0000000, 0x8e1bc9bf04000000, 0xb1a2bc2ec5000000, 0xde0b6b3a76400000, 0x8ac7230489e80000, 0xad78ebc5ac620000, 0xd8d726b7177a8000, 0x878678326eac9000, 0xa968163f0a57b400, 0xd3c21bcecceda100, 0x84595161401484a0, 0xa56fa5b99019a5c8, 0xcecb8f27f4200f3a, 0x813f3978f8940984, 0xa18f07d736b90be5, 0xc9f2c9cd04674edf, 0xfc6f7c4045812296, 0x9dc5ada82b70b59e, 0xc5371912364ce305, 0xf684df56c3e01bc7, 0x9a130b963a6c115c, 0xc097ce7bc90715b3, 0xf0bdc21abb48db20, 0x96769950b50d88f4, 0xbc143fa4e250eb31, 0xeb194f8e1ae525fd, 0x92efd1b8d0cf37be, 0xb7abc627050305ae, 0xe596b7b0c643c719, 0x8f7e32ce7bea5c70, 0xb35dbf821ae4f38c, 0xe0352f62a19e306f, 0x8c213d9da502de45, 0xaf298d050e4395d7, 0xdaf3f04651d47b4c, 0x88d8762bf324cd10, 0xab0e93b6efee0054, 0xd5d238a4abe98068, 0x85a36366eb71f041, 0xa70c3c40a64e6c52, 0xd0cf4b50cfe20766, 0x82818f1281ed44a0, 0xa321f2d7226895c8, 0xcbea6f8ceb02bb3a, 0xfee50b7025c36a08, 0x9f4f2726179a2245, 0xc722f0ef9d80aad6, 0xf8ebad2b84e0d58c, 0x9b934c3b330c8577, 0xc2781f49ffcfa6d5, 0xf316271c7fc3908b, 0x97edd871cfda3a57, 0xbde94e8e43d0c8ec, 0xed63a231d4c4fb27, 0x945e455f24fb1cf9, 0xb975d6b6ee39e437, 0xe7d34c64a9c85d44, 0x90e40fbeea1d3a4b, 0xb51d13aea4a488dd, 0xe264589a4dcdab15, 0x8d7eb76070a08aed, 0xb0de65388cc8ada8, 0xdd15fe86affad912, 0x8a2dbf142dfcc7ab, 0xacb92ed9397bf996, 0xd7e77a8f87daf7fc, 0x86f0ac99b4e8dafd, 0xa8acd7c0222311bd, 0xd2d80db02aabd62c, 0x83c7088e1aab65db, 0xa4b8cab1a1563f52, 0xcde6fd5e09abcf27, 0x80b05e5ac60b6178, 0xa0dc75f1778e39d6, 0xc913936dd571c84c, 0xfb5878494ace3a5f, 0x9d174b2dcec0e47b, 0xc45d1df942711d9a, 0xf5746577930d6501, 0x9968bf6abbe85f20, 0xbfc2ef456ae276e9, 0xefb3ab16c59b14a3, 0x95d04aee3b80ece6, 0xbb445da9ca61281f, 0xea1575143cf97227, 0x924d692ca61be758, 0xb6e0c377cfa2e12e, 0xe498f455c38b997a, 0x8edf98b59a373fec, 0xb2977ee300c50fe7, 0xdf3d5e9bc0f653e1, 0x8b865b215899f46d, 0xae67f1e9aec07188, 0xda01ee641a708dea, 0x884134fe908658b2, 0xaa51823e34a7eedf, 0xd4e5e2cdc1d1ea96, 0x850fadc09923329e, 0xa6539930bf6bff46, 0xcfe87f7cef46ff17, 0x81f14fae158c5f6e, 0xa26da3999aef774a, 0xcb090c8001ab551c, 0xfdcb4fa002162a63, 0x9e9f11c4014dda7e, 0xc646d63501a1511e, 0xf7d88bc24209a565, 0x9ae757596946075f, 0xc1a12d2fc3978937, 0xf209787bb47d6b85, 0x9745eb4d50ce6333, 0xbd176620a501fc00, 0xec5d3fa8ce427b00, 0x93ba47c980e98ce0, 0xb8a8d9bbe123f018, 0xe6d3102ad96cec1e, 0x9043ea1ac7e41393, 0xb454e4a179dd1877, 0xe16a1dc9d8545e95, 0x8ce2529e2734bb1d, 0xb01ae745b101e9e4, 0xdc21a1171d42645d, 0x899504ae72497eba, 0xabfa45da0edbde69, 0xd6f8d7509292d603, 0x865b86925b9bc5c2, 0xa7f26836f282b733, 0xd1ef0244af2364ff, 0x8335616aed761f1f, 0xa402b9c5a8d3a6e7, 0xcd036837130890a1, 0x802221226be55a65, 0xa02aa96b06deb0fe, 0xc83553c5c8965d3d, 0xfa42a8b73abbf48d, 0x9c69a97284b578d8, 0xc38413cf25e2d70e, 0xf46518c2ef5b8cd1, 0x98bf2f79d5993803, 0xbeeefb584aff8604, 0xeeaaba2e5dbf6785, 0x952ab45cfa97a0b3, 0xba756174393d88e0, 0xe912b9d1478ceb17, 0x91abb422ccb812ef, 0xb616a12b7fe617aa, 0xe39c49765fdf9d95, 0x8e41ade9fbebc27d, 0xb1d219647ae6b31c, 0xde469fbd99a05fe3, 0x8aec23d680043bee, 0xada72ccc20054aea, 0xd910f7ff28069da4, 0x87aa9aff79042287, 0xa99541bf57452b28, 0xd3fa922f2d1675f2, 0x847c9b5d7c2e09b7, 0xa59bc234db398c25, 0xcf02b2c21207ef2f, 0x8161afb94b44f57d, 0xa1ba1ba79e1632dc, 0xca28a291859bbf93, 0xfcb2cb35e702af78, 0x9defbf01b061adab, 0xc56baec21c7a1916, 0xf6c69a72a3989f5c, 0x9a3c2087a63f6399, 0xc0cb28a98fcf3c80, 0xf0fdf2d3f3c30b9f, 0x969eb7c47859e744, 0xbc4665b596706115, 0xeb57ff22fc0c795a, 0x9316ff75dd87cbd8, 0xb7dcbf5354e9bece, 0xe5d3ef282a242e82, 0x8fa475791a569d11, 0xb38d92d760ec4455, 0xe070f78d3927556b, 0x8c469ab843b89563, 0xaf58416654a6babb, 0xdb2e51bfe9d0696a, 0x88fcf317f22241e2, 0xab3c2fddeeaad25b, 0xd60b3bd56a5586f2, 0x85c7056562757457, 0xa738c6bebb12d16d, 0xd106f86e69d785c8, 0x82a45b450226b39d, 0xa34d721642b06084, 0xcc20ce9bd35c78a5, 0xff290242c83396ce, 0x9f79a169bd203e41, 0xc75809c42c684dd1, 0xf92e0c3537826146, 0x9bbcc7a142b17ccc, 0xc2abf989935ddbfe, 0xf356f7ebf83552fe, 0x98165af37b2153df, 0xbe1bf1b059e9a8d6, 0xeda2ee1c7064130c, 0x9485d4d1c63e8be8, 0xb9a74a0637ce2ee1, 0xe8111c87c5c1ba9a, 0x910ab1d4db9914a0, 0xb54d5e4a127f59c8, 0xe2a0b5dc971f303a, 0x8da471a9de737e24, 0xb10d8e1456105dad, 0xdd50f1996b947519, 0x8a5296ffe33cc930, 0xace73cbfdc0bfb7b, 0xd8210befd30efa5a, 0x8714a775e3e95c78, 0xa8d9d1535ce3b396, 0xd31045a8341ca07c, 0x83ea2b892091e44e, 0xa4e4b66b68b65d61, 0xce1de40642e3f4b9, 0x80d2ae83e9ce78f4, 0xa1075a24e4421731, 0xc94930ae1d529cfd, 0xfb9b7cd9a4a7443c, 0x9d412e0806e88aa6, 0xc491798a08a2ad4f, 0xf5b5d7ec8acb58a3, 0x9991a6f3d6bf1766, 0xbff610b0cc6edd3f, 0xeff394dcff8a948f, 0x95f83d0a1fb69cd9, 0xbb764c4ca7a44410, 0xea53df5fd18d5514, 0x92746b9be2f8552c, 0xb7118682dbb66a77, 0xe4d5e82392a40515, 0x8f05b1163ba6832d, 0xb2c71d5bca9023f8, 0xdf78e4b2bd342cf7, 0x8bab8eefb6409c1a, 0xae9672aba3d0c321, 0xda3c0f568cc4f3e9, 0x8865899617fb1871, 0xaa7eebfb9df9de8e, 0xd51ea6fa85785631, 0x8533285c936b35df, 0xa67ff273b8460357, 0xd01fef10a657842c, 0x8213f56a67f6b29c, 0xa298f2c501f45f43, 0xcb3f2f7642717713, 0xfe0efb53d30dd4d8, 0x9ec95d1463e8a507, 0xc67bb4597ce2ce49, 0xf81aa16fdc1b81db, 0x9b10a4e5e9913129, 0xc1d4ce1f63f57d73, 0xf24a01a73cf2dcd0, 0x976e41088617ca02, 0xbd49d14aa79dbc82, 0xec9c459d51852ba3, 0x93e1ab8252f33b46, 0xb8da1662e7b00a17, 0xe7109bfba19c0c9d, 0x906a617d450187e2, 0xb484f9dc9641e9db, 0xe1a63853bbd26451, 0x8d07e33455637eb3, 0xb049dc016abc5e60, 0xdc5c5301c56b75f7, 0x89b9b3e11b6329bb, 0xac2820d9623bf429, 0xd732290fbacaf134, 0x867f59a9d4bed6c0, 0xa81f301449ee8c70, 0xd226fc195c6a2f8c, 0x83585d8fd9c25db8, 0xa42e74f3d032f526, 0xcd3a1230c43fb26f, 0x80444b5e7aa7cf85, 0xa0555e361951c367, 0xc86ab5c39fa63441, 0xfa856334878fc151, 0x9c935e00d4b9d8d2, 0xc3b8358109e84f07, 0xf4a642e14c6262c9, 0x98e7e9cccfbd7dbe, 0xbf21e44003acdd2d, 0xeeea5d5004981478, 0x95527a5202df0ccb, 0xbaa718e68396cffe, 0xe950df20247c83fd, 0x91d28b7416cdd27e, 0xb6472e511c81471e, 0xe3d8f9e563a198e5, 0x8e679c2f5e44ff8f, 0xb201833b35d63f73, 0xde81e40a034bcf50, 0x8b112e86420f6192, 0xadd57a27d29339f6, 0xd94ad8b1c7380874, 0x87cec76f1c830549, 0xa9c2794ae3a3c69b, 0xd433179d9c8cb841, 0x849feec281d7f329, 0xa5c7ea73224deff3, 0xcf39e50feae16bf0, 0x81842f29f2cce376, 0xa1e53af46f801c53, 0xca5e89b18b602368, 0xfcf62c1dee382c42, 0x9e19db92b4e31ba9, 0xc5a05277621be294, 0xf70867153aa2db39, 0x9a65406d44a5c903, 0xc0fe908895cf3b44, 0xf13e34aabb430a15, 0x96c6e0eab509e64d, 0xbc789925624c5fe1, 0xeb96bf6ebadf77d9, 0x933e37a534cbaae8, 0xb80dc58e81fe95a1, 0xe61136f2227e3b0a, 0x8fcac257558ee4e6, 0xb3bd72ed2af29e20, 0xe0accfa875af45a8, 0x8c6c01c9498d8b89, 0xaf87023b9bf0ee6b, 0xdb68c2ca82ed2a06, 0x892179be91d43a44, 0xab69d82e364948d4 }; +static constexpr int powers_ten_e[] = { -1203, -1200, -1196, -1193, -1190, -1186, -1183, -1180, -1176, -1173, -1170, -1166, -1163, -1160, -1156, -1153, -1150, -1146, -1143, -1140, -1136, -1133, -1130, -1127, -1123, -1120, -1117, -1113, -1110, -1107, -1103, -1100, -1097, -1093, -1090, -1087, -1083, -1080, -1077, -1073, -1070, -1067, -1063, -1060, -1057, -1053, -1050, -1047, -1043, -1040, -1037, -1034, -1030, -1027, -1024, -1020, -1017, -1014, -1010, -1007, -1004, -1000, -997, -994, -990, -987, -984, -980, -977, -974, -970, -967, -964, -960, -957, -954, -950, -947, -944, -940, -937, -934, -931, -927, -924, -921, -917, -914, -911, -907, -904, -901, -897, -894, -891, -887, -884, -881, -877, -874, -871, -867, -864, -861, -857, -854, -851, -847, -844, -841, -838, -834, -831, -828, -824, -821, -818, -814, -811, -808, -804, -801, -798, -794, -791, -788, -784, -781, -778, -774, -771, -768, -764, -761, -758, -754, -751, -748, -744, -741, -738, -735, -731, -728, -725, -721, -718, -715, -711, -708, -705, -701, -698, -695, -691, -688, -685, -681, -678, -675, -671, -668, -665, -661, -658, -655, -651, -648, -645, -642, -638, -635, -632, -628, -625, -622, -618, -615, -612, -608, -605, -602, -598, -595, -592, -588, -585, -582, -578, -575, -572, -568, -565, -562, -558, -555, -552, -549, -545, -542, -539, -535, -532, -529, -525, -522, -519, -515, -512, -509, -505, -502, -499, -495, -492, -489, -485, -482, -479, -475, -472, -469, -465, -462, -459, -455, -452, -449, -446, -442, -439, -436, -432, -429, -426, -422, -419, -416, -412, -409, -406, -402, -399, -396, -392, -389, -386, -382, -379, -376, -372, -369, -366, -362, -359, -356, -353, -349, -346, -343, -339, -336, -333, -329, -326, -323, -319, -316, -313, -309, -306, -303, -299, -296, -293, -289, -286, -283, -279, -276, -273, -269, -266, -263, -259, -256, -253, -250, -246, -243, -240, -236, -233, -230, -226, -223, -220, -216, -213, -210, -206, -203, -200, -196, -193, -190, -186, -183, -180, -176, -173, -170, -166, -163, -160, -157, -153, -150, -147, -143, -140, -137, -133, -130, -127, -123, -120, -117, -113, -110, -107, -103, -100, -97, -93, -90, -87, -83, -80, -77, -73, -70, -67, -63, -60, -57, -54, -50, -47, -44, -40, -37, -34, -30, -27, -24, -20, -17, -14, -10, -7, -4, 0, 3, 6, 10, 13, 16, 20, 23, 26, 30, 33, 36, 39, 43, 46, 49, 53, 56, 59, 63, 66, 69, 73, 76, 79, 83, 86, 89, 93, 96, 99, 103, 106, 109, 113, 116, 119, 123, 126, 129, 132, 136, 139, 142, 146, 149, 152, 156, 159, 162, 166, 169, 172, 176, 179, 182, 186, 189, 192, 196, 199, 202, 206, 209, 212, 216, 219, 222, 226, 229, 232, 235, 239, 242, 245, 249, 252, 255, 259, 262, 265, 269, 272, 275, 279, 282, 285, 289, 292, 295, 299, 302, 305, 309, 312, 315, 319, 322, 325, 328, 332, 335, 338, 342, 345, 348, 352, 355, 358, 362, 365, 368, 372, 375, 378, 382, 385, 388, 392, 395, 398, 402, 405, 408, 412, 415, 418, 422, 425, 428, 431, 435, 438, 441, 445, 448, 451, 455, 458, 461, 465, 468, 471, 475, 478, 481, 485, 488, 491, 495, 498, 501, 505, 508, 511, 515, 518, 521, 524, 528, 531, 534, 538, 541, 544, 548, 551, 554, 558, 561, 564, 568, 571, 574, 578, 581, 584, 588, 591, 594, 598, 601, 604, 608, 611, 614, 617, 621, 624, 627, 631, 634, 637, 641, 644, 647, 651, 654, 657, 661, 664, 667, 671, 674, 677, 681, 684, 687, 691, 694, 697, 701, 704, 707, 711, 714, 717, 720, 724, 727, 730, 734, 737, 740, 744, 747, 750, 754, 757, 760, 764, 767, 770, 774, 777, 780, 784, 787, 790, 794, 797, 800, 804, 807, 810, 813, 817, 820, 823, 827, 830, 833, 837, 840, 843, 847, 850, 853, 857, 860, 863, 867, 870, 873, 877, 880, 883, 887, 890, 893, 897, 900, 903, 907, 910, 913, 916, 920, 923, 926, 930, 933, 936, 940, 943, 946, 950, 953, 956, 960, 963, 966, 970, 973, 976, 980, 983, 986, 990, 993, 996, 1000, 1003, 1006, 1009, 1013, 1016, 1019, 1023, 1026, 1029, 1033, 1036, 1039, 1043, 1046, 1049, 1053, 1056, 1059, 1063, 1066, 1069, 1073, 1076 }; + +inline +diy_fp_t cached_power(int k) +{ + diy_fp_t res; + int index = 343 + k; + res.f = powers_ten[index]; + res.e = powers_ten_e[index]; + return res; +} + +// double + +/* +typedef union { + double d; + uint64_t n; +} converter_t; + +inline +uint64_t double_to_uint64(double d) { converter_t tmp; tmp.d = d; return tmp.n; } + +inline +double uint64_to_double(uint64_t d64) { converter_t tmp; tmp.n = d64; return tmp.d; } +*/ +inline +uint64_t double_to_uint64(double d) {uint64_t d64; std::memcpy(&d64,&d,sizeof(double)); return d64; } + +inline +double uint64_to_double(uint64_t d64) {double d; std::memcpy(&d,&d64,sizeof(double)); return d; } + +constexpr int dp_significand_size = 52; +constexpr int dp_exponent_bias = (0x3FF + dp_significand_size); +constexpr int dp_min_exponent = (-dp_exponent_bias); +constexpr uint64_t dp_exponent_mask = 0x7FF0000000000000; +constexpr uint64_t dp_significand_mask = 0x000FFFFFFFFFFFFF; +constexpr uint64_t dp_hidden_bit = 0x0010000000000000; + +inline +diy_fp_t normalize_diy_fp(diy_fp_t in) +{ + diy_fp_t res = in; + /* Normalize now */ + /* the original number could have been a denormal. */ + while (!(res.f & dp_hidden_bit)) + { + res.f <<= 1; + res.e--; + } + /* do the final shifts in one go. Don't forget the hidden bit (the '-1') */ + res.f <<= (diy_significand_size - dp_significand_size - 1); + res.e = res.e - (diy_significand_size - dp_significand_size - 1); + return res; +} + +inline +diy_fp_t double2diy_fp(double d) +{ + uint64_t d64 = double_to_uint64(d); + int biased_e = (d64 & dp_exponent_mask) >> dp_significand_size; + uint64_t significand = (d64 & dp_significand_mask); + diy_fp_t res; + if (biased_e != 0) + { + res.f = significand + dp_hidden_bit; + res.e = biased_e - dp_exponent_bias; + } + else + { + res.f = significand; + res.e = dp_min_exponent + 1; + } + return res; +} + +inline +diy_fp_t normalize_boundary(diy_fp_t in) +{ + diy_fp_t res = in; + /* Normalize now */ + /* the original number could have been a denormal. */ + while (!(res.f & (dp_hidden_bit << 1))) + { + res.f <<= 1; + res.e--; + } + /* do the final shifts in one go. Don't forget the hidden bit (the '-1') */ + res.f <<= (diy_significand_size - dp_significand_size - 2); + res.e = res.e - (diy_significand_size - dp_significand_size - 2); + return res; +} + +inline +void normalized_boundaries(double d, diy_fp_t *out_m_minus, diy_fp_t *out_m_plus) +{ + diy_fp_t v = double2diy_fp(d); + diy_fp_t pl, mi; + bool significand_is_zero = v.f == dp_hidden_bit; + pl.f = (v.f << 1) + 1; pl.e = v.e - 1; + pl = normalize_boundary(pl); + if (significand_is_zero) + { + mi.f = (v.f << 2) - 1; + mi.e = v.e - 2; + } else + { + mi.f = (v.f << 1) - 1; + mi.e = v.e - 1; + } + int x = mi.e - pl.e; + mi.f <<= x; + mi.e = pl.e; + *out_m_plus = pl; + *out_m_minus = mi; +} + +inline +double random_double() +{ + uint64_t tmp = 0; + int i; + for (i = 0; i < 8; i++) + { + tmp <<= 8; + tmp += rand() % 256; + } + return uint64_to_double(tmp); +} + +// grisu3 + +inline +bool round_weed(char *buffer, int len, + uint64_t wp_W, uint64_t Delta, + uint64_t rest, uint64_t ten_kappa, + uint64_t ulp) +{ + uint64_t wp_Wup = wp_W - ulp; + uint64_t wp_Wdown = wp_W + ulp; + while (rest < wp_Wup && /// round1 + Delta - rest >= ten_kappa && + (rest + ten_kappa < wp_Wup || /// closer + wp_Wup - rest >= rest + ten_kappa - wp_Wup)) + { + buffer[len - 1]--; rest += ten_kappa; + } + if (rest < wp_Wdown && /// round2 + Delta - rest >= ten_kappa && + (rest + ten_kappa < wp_Wdown || + wp_Wdown - rest > rest + ten_kappa - wp_Wdown)) return 0; + return 2 * ulp <= rest && rest <= Delta - 4 * ulp; /// weed +} + +inline +bool digit_gen(diy_fp_t Wm, diy_fp_t W, diy_fp_t Wp, + char *buffer, int *len, int *K) +{ + const uint32_t TEN2 = 100; + + uint32_t div, p1; uint64_t p2, tmp, unit = 1; + int d, kappa; + diy_fp_t one, wp_W, Delta; + Delta = minus(Wp, Wm); Delta.f += 2 * unit; + wp_W = minus(Wp, W); wp_W.f += unit; + one.f = ((uint64_t)1) << -Wp.e; one.e = Wp.e; + p1 = static_cast<uint32_t>((Wp.f + 1) >> -one.e); + p2 = (Wp.f + 1) & (one.f - 1); + *len = 0; kappa = 3; div = TEN2; + while (kappa > 0) + { + d = p1 / div; + if (d || *len) buffer[(*len)++] = (char)('0' + d); + p1 %= div; kappa--; + tmp = (((uint64_t)p1) << -one.e) + p2; + if (tmp < Delta.f) + { + *K += kappa; + return round_weed(buffer, *len, wp_W.f, Delta.f, tmp, + ((uint64_t)div) << -one.e, unit); + } + div /= 10; + } + while (1) + { + p2 *= 10; Delta.f *= 10; unit *= 10; + d = static_cast<int>(p2 >> -one.e); + if (d || *len) buffer[(*len)++] = (char)('0' + d); + p2 &= one.f - 1; kappa--; + if (p2 < Delta.f) + { + *K += kappa; + return round_weed(buffer, *len, wp_W.f * unit, Delta.f, p2, + one.f, unit); + } + } +} + +inline +bool grisu3(double v, char *buffer, int *length, int *K) +{ + diy_fp_t w_m, w_p; + int q = 64, alpha = -59, gamma = -56; + normalized_boundaries(v, &w_m, &w_p); + diy_fp_t w = normalize_diy_fp(double2diy_fp(v)); + int mk = k_comp(w_p.e + q, alpha, gamma); + diy_fp_t c_mk = cached_power(mk); + diy_fp_t W = multiply(w, c_mk); + diy_fp_t Wp = multiply(w_p, c_mk); + diy_fp_t Wm = multiply(w_m, c_mk); + *K = -mk; + bool result = digit_gen(Wm, W, Wp, buffer, length, K); + buffer[*length] = 0; + return result; +} + +}} // namespace detail namespace jsoncons + +#endif diff --git a/include/jsoncons/detail/optional.hpp b/include/jsoncons/detail/optional.hpp new file mode 100644 index 0000000..492cd76 --- /dev/null +++ b/include/jsoncons/detail/optional.hpp @@ -0,0 +1,483 @@ +// Copyright 2019 Daniel Parker +// Distributed under the Boost license, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +// See https://github.com/danielaparker/jsoncons for latest version + +#ifndef JSONCONS_DETAIL_OPTIONAL_HPP +#define JSONCONS_DETAIL_OPTIONAL_HPP + +#include <new> // placement new +#include <memory> +#include <utility> // std::swap +#include <type_traits> +#include <jsoncons/config/compiler_support.hpp> + +namespace jsoncons +{ +namespace detail +{ + template <typename T> + class optional; + + template <typename T1, typename T2> + struct is_constructible_or_convertible_from_optional + : std::integral_constant< + bool, std::is_constructible<T1, optional<T2>&>::value || + std::is_constructible<T1, optional<T2>&&>::value || + std::is_constructible<T1, const optional<T2>&>::value || + std::is_constructible<T1, const optional<T2>&&>::value || + std::is_convertible<optional<T2>&, T1>::value || + std::is_convertible<optional<T2>&&, T1>::value || + std::is_convertible<const optional<T2>&, T1>::value || + std::is_convertible<const optional<T2>&&, T1>::value> {}; + + template <typename T1, typename T2> + struct is_constructible_convertible_or_assignable_from_optional + : std::integral_constant< + bool, is_constructible_or_convertible_from_optional<T1, T2>::value || + std::is_assignable<T1&, optional<T2>&>::value || + std::is_assignable<T1&, optional<T2>&&>::value || + std::is_assignable<T1&, const optional<T2>&>::value || + std::is_assignable<T1&, const optional<T2>&&>::value> {}; + + template <typename T> + class optional + { + public: + using value_type = T; + private: + bool has_value_; + union { + char dummy_; + T value_; + }; + public: + constexpr optional() noexcept + : has_value_(false), dummy_{} + { + } + + // copy constructors + optional(const optional<T>& other) + : has_value_(false), dummy_{} + { + if (other) + { + construct(*other); + } + } + + // converting + template <class U, + typename std::enable_if<!std::is_same<T,U>::value && + std::is_constructible<T, const U&>::value && + std::is_convertible<const U&,T>::value && + !is_constructible_or_convertible_from_optional<T,U>::value && + std::is_copy_constructible<typename std::decay<U>::type>::value,int>::type = 0> + optional(const optional<U>& other) + : has_value_(false), dummy_{} + { + if (other) + { + construct(*other); + } + } + + template <class U, + typename std::enable_if<!std::is_same<T,U>::value && + std::is_constructible<T, const U&>::value && + !std::is_convertible<const U&,T>::value && + !is_constructible_or_convertible_from_optional<T,U>::value && + std::is_copy_constructible<typename std::decay<U>::type>::value,int>::type = 0> + explicit optional(const optional<U>& other) + : has_value_(false), dummy_{} + { + if (other) + { + construct(*other); + } + } + + // move constructors + template <class T2 = T> + optional(optional<T>&& other, + typename std::enable_if<std::is_move_constructible<typename std::decay<T2>::type>::value>::type* = 0) + : has_value_(false), dummy_{} + { + if (other) + { + construct(std::move(other.value_)); + } + } + + // converting + template <class U> + optional(optional<U>&& value, + typename std::enable_if<!std::is_same<T,U>::value && + std::is_constructible<T, U&&>::value && + !is_constructible_or_convertible_from_optional<T,U>::value && + std::is_convertible<U&&,T>::value,int>::type = 0) // (8) + : has_value_(true), value_(std::forward<U>(value)) + { + } + + template <class U> + explicit optional(optional<U>&& value, + typename std::enable_if<!std::is_same<T,U>::value && + std::is_constructible<T, U&&>::value && + !is_constructible_or_convertible_from_optional<T,U>::value && + !std::is_convertible<U&&,T>::value,int>::type = 0) // (8) + : has_value_(true), value_(std::forward<U>(value)) + { + } + + + // value constructors + template <class T2> + optional(T2&& value, + typename std::enable_if<!std::is_same<optional<T>,typename std::decay<T2>::type>::value && + std::is_constructible<T, T2>::value && + std::is_convertible<T2,T>::value,int>::type = 0) // (8) + : has_value_(true), value_(std::forward<T2>(value)) + { + } + + template <class T2> + explicit optional(T2&& value, + typename std::enable_if<!std::is_same<optional<T>,typename std::decay<T2>::type>::value && + std::is_constructible<T, T2>::value && + !std::is_convertible<T2,T>::value,int>::type = 0) // (8) + : has_value_(true), value_(std::forward<T2>(value)) + { + } + + ~optional() noexcept + { + destroy(); + } + + optional& operator=(const optional& other) + { + if (other) + { + assign(*other); + } + else + { + reset(); + } + return *this; + } + + optional& operator=(optional&& other ) + { + if (other) + { + assign(std::move(*other)); + } + else + { + reset(); + } + return *this; + } + + template <typename U> + typename std::enable_if<!std::is_same<optional<T>, U>::value && + std::is_constructible<T, const U&>::value && + !is_constructible_convertible_or_assignable_from_optional<T,U>::value && + std::is_assignable<T&, const U&>::value, + optional&>::type + operator=(const optional<U>& other) + { + if (other) + { + assign(*other); + } + else + { + destroy(); + } + return *this; + } + + template <typename U> + typename std::enable_if<!std::is_same<optional<T>, U>::value && + std::is_constructible<T, U>::value && + !is_constructible_convertible_or_assignable_from_optional<T,U>::value && + std::is_assignable<T&, U>::value, + optional&>::type + operator=(optional<U>&& other) + { + if (other) + { + assign(std::move(*other)); + } + else + { + destroy(); + } + return *this; + } + + // value assignment + template <typename T2> + typename std::enable_if<!std::is_same<optional<T>, typename std::decay<T2>::type>::value && + std::is_constructible<T, T2>::value && + std::is_assignable<T&, T2>::value && + !(std::is_scalar<T>::value && std::is_same<T, typename std::decay<T2>::type>::value), + optional&>::type + operator=(T2&& v) + { + assign(std::forward<T2>(v)); + return *this; + } + + constexpr explicit operator bool() const noexcept + { + return has_value_; + } + constexpr bool has_value() const noexcept + { + return has_value_; + } + +#ifdef _MSC_VER +#pragma warning(push) +#pragma warning(disable : 4702) +#endif // _MSC_VER + + T& value() & + { + return static_cast<bool>(*this) + ? get() + : JSONCONS_THROW(std::runtime_error("Bad optional access")), get(); + } + + constexpr const T& value() const & + { + return static_cast<bool>(*this) + ? get() + : JSONCONS_THROW(std::runtime_error("Bad optional access")), get(); + } + + template <typename U> + constexpr T value_or(U&& default_value) const & + { + static_assert(std::is_copy_constructible<T>::value, + "get_value_or: T must be copy constructible"); + static_assert(std::is_convertible<U&&, T>::value, + "get_value_or: U must be convertible to T"); + return static_cast<bool>(*this) + ? **this + : static_cast<T>(std::forward<U>(default_value)); + } + + template <typename U> + T value_or(U&& default_value) && + { + static_assert(std::is_move_constructible<T>::value, + "get_value_or: T must be move constructible"); + static_assert(std::is_convertible<U&&, T>::value, + "get_value_or: U must be convertible to T"); + return static_cast<bool>(*this) ? std::move(**this) + : static_cast<T>(std::forward<U>(default_value)); + } +#ifdef _MSC_VER +#pragma warning(pop) +#endif // _MSC_VER + + const T* operator->() const + { + return std::addressof(this->value_); + } + + T* operator->() + { + return std::addressof(this->value_); + } + + constexpr const T& operator*() const& + { + return value(); + } + + T& operator*() & + { + return value(); + } + + void reset() noexcept + { + destroy(); + } + + void swap(optional& other) noexcept(std::is_nothrow_move_constructible<T>::value /*&& + std::is_nothrow_swappable<T>::value*/) + { + const bool contains_a_value = has_value(); + if (contains_a_value == other.has_value()) + { + if (contains_a_value) + { + using std::swap; + swap(**this, *other); + } + } + else + { + optional& source = contains_a_value ? *this : other; + optional& target = contains_a_value ? other : *this; + target = optional<T>(*source); + source.reset(); + } + } + private: + constexpr const T& get() const { return this->value_; } + T& get() { return this->value_; } + + template <typename... Args> + void construct(Args&&... args) + { + ::new (static_cast<void*>(&this->value_)) T(std::forward<Args>(args)...); + has_value_ = true; + } + + void destroy() noexcept + { + if (has_value_) + { + value_.~T(); + has_value_ = false; + } + } + + template <typename U> + void assign(U&& u) + { + if (has_value_) + { + value_ = std::forward<U>(u); + } + else + { + construct(std::forward<U>(u)); + } + } + }; + + template <typename T> + typename std::enable_if<std::is_nothrow_move_constructible<T>::value,void>::type + swap(optional<T>& lhs, optional<T>& rhs) noexcept + { + lhs.swap(rhs); + } + + template <class T1, class T2> + constexpr bool operator==(const optional<T1>& lhs, const optional<T2>& rhs) noexcept + { + return lhs.has_value() == rhs.has_value() && (!lhs.has_value() || *lhs == *rhs); + } + + template <class T1, class T2> + constexpr bool operator!=(const optional<T1>& lhs, const optional<T2>& rhs) noexcept + { + return lhs.has_value() != rhs.has_value() || (lhs.has_value() && *lhs != *rhs); + } + + template <class T1, class T2> + constexpr bool operator<(const optional<T1>& lhs, const optional<T2>& rhs) noexcept + { + return rhs.has_value() && (!lhs.has_value() || *lhs < *rhs); + } + + template <class T1, class T2> + constexpr bool operator>(const optional<T1>& lhs, const optional<T2>& rhs) noexcept + { + return lhs.has_value() && (!rhs.has_value() || *lhs > *rhs); + } + + template <class T1, class T2> + constexpr bool operator<=(const optional<T1>& lhs, const optional<T2>& rhs) noexcept + { + return !lhs.has_value() || (rhs.has_value() && *lhs <= *rhs); + } + + template <class T1, class T2> + constexpr bool operator>=(const optional<T1>& lhs, const optional<T2>& rhs) noexcept + { + return !rhs.has_value() || (lhs.has_value() && *lhs >= *rhs); + } + + template <class T1, class T2> + constexpr bool operator==(const optional<T1>& lhs, const T2& rhs) noexcept + { + return lhs ? *lhs == rhs : false; + } + template <class T1, class T2> + constexpr bool operator==(const T1& lhs, const optional<T2>& rhs) noexcept + { + return rhs ? lhs == *rhs : false; + } + + template <class T1, class T2> + constexpr bool operator!=(const optional<T1>& lhs, const T2& rhs) noexcept + { + return lhs ? *lhs != rhs : true; + } + template <class T1, class T2> + constexpr bool operator!=(const T1& lhs, const optional<T2>& rhs) noexcept + { + return rhs ? lhs != *rhs : true; + } + + template <class T1, class T2> + constexpr bool operator<(const optional<T1>& lhs, const T2& rhs) noexcept + { + return lhs ? *lhs < rhs : true; + } + template <class T1, class T2> + constexpr bool operator<(const T1& lhs, const optional<T2>& rhs) noexcept + { + return rhs ? lhs < *rhs : false; + } + + template <class T1, class T2> + constexpr bool operator<=(const optional<T1>& lhs, const T2& rhs) noexcept + { + return lhs ? *lhs <= rhs : true; + } + template <class T1, class T2> + constexpr bool operator<=(const T1& lhs, const optional<T2>& rhs) noexcept + { + return rhs ? lhs <= *rhs : false; + } + + template <class T1, class T2> + constexpr bool operator>(const optional<T1>& lhs, const T2& rhs) noexcept + { + return lhs ? *lhs > rhs : false; + } + + template <class T1, class T2> + constexpr bool operator>(const T1& lhs, const optional<T2>& rhs) noexcept + { + return rhs ? lhs > *rhs : true; + } + + template <class T1, class T2> + constexpr bool operator>=(const optional<T1>& lhs, const T2& rhs) noexcept + { + return lhs ? *lhs >= rhs : false; + } + template <class T1, class T2> + constexpr bool operator>=(const T1& lhs, const optional<T2>& rhs) noexcept + { + return rhs ? lhs >= *rhs : true; + } + +} // namespace detail +} // namespace jsoncons + +#endif diff --git a/include/jsoncons/detail/parse_number.hpp b/include/jsoncons/detail/parse_number.hpp new file mode 100644 index 0000000..7417ff2 --- /dev/null +++ b/include/jsoncons/detail/parse_number.hpp @@ -0,0 +1,1133 @@ +// Copyright 2013 Daniel Parker +// Distributed under the Boost license, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +// See https://github.com/danielaparker/jsoncons for latest version + +#ifndef JSONCONS_DETAIL_PARSE_NUMBER_HPP +#define JSONCONS_DETAIL_PARSE_NUMBER_HPP + +#include <system_error> +#include <stdexcept> +#include <string> +#include <vector> +#include <locale> +#include <string> +#include <limits> // std::numeric_limits +#include <type_traits> // std::enable_if +#include <exception> +#include <jsoncons/config/jsoncons_config.hpp> +#include <jsoncons/json_exception.hpp> +#include <cctype> + +namespace jsoncons { namespace detail { + + enum class to_integer_errc : uint8_t {success=0, overflow, invalid_digit, invalid_number}; + + class to_integer_error_category_impl + : public std::error_category + { + public: + const char* name() const noexcept override + { + return "jsoncons/to_integer_unchecked"; + } + std::string message(int ev) const override + { + switch (static_cast<to_integer_errc>(ev)) + { + case to_integer_errc::overflow: + return "Integer overflow"; + case to_integer_errc::invalid_digit: + return "Invalid digit"; + case to_integer_errc::invalid_number: + return "Invalid number"; + default: + return "Unknown to_integer_unchecked error"; + } + } + }; + + inline + const std::error_category& to_integer_error_category() + { + static to_integer_error_category_impl instance; + return instance; + } + + inline + std::error_code make_error_code(to_integer_errc e) + { + return std::error_code(static_cast<int>(e),to_integer_error_category()); + } + +} // namespace detail +} // namespace jsoncons + +namespace std { + template<> + struct is_error_code_enum<jsoncons::detail::to_integer_errc> : public true_type + { + }; +} + +namespace jsoncons { namespace detail { + +template <class T,class CharT> +struct to_integer_result +{ + const CharT* ptr; + to_integer_errc ec; + constexpr to_integer_result(const CharT* ptr_) + : ptr(ptr_), ec(to_integer_errc()) + { + } + constexpr to_integer_result(const CharT* ptr_, to_integer_errc ec_) + : ptr(ptr_), ec(ec_) + { + } + + to_integer_result(const to_integer_result&) = default; + + to_integer_result& operator=(const to_integer_result&) = default; + + constexpr explicit operator bool() const noexcept + { + return ec == to_integer_errc(); + } + std::error_code error_code() const + { + return make_error_code(ec); + } +}; + +enum class integer_chars_format : uint8_t {decimal=1,hex}; +enum class integer_chars_state {initial,minus,integer,binary,octal,decimal,base16}; + +template <class CharT> +bool is_base10(const CharT* s, std::size_t length) +{ + integer_chars_state state = integer_chars_state::initial; + + const CharT* end = s + length; + for (;s < end; ++s) + { + switch(state) + { + case integer_chars_state::initial: + { + switch(*s) + { + case '0':case '1':case '2':case '3':case '4':case '5':case '6':case '7':case '8': case '9': + state = integer_chars_state::decimal; + break; + case '-': + state = integer_chars_state::minus; + break; + default: + return false; + } + break; + } + case integer_chars_state::minus: + { + switch(*s) + { + case '0':case '1':case '2':case '3':case '4':case '5':case '6':case '7':case '8': case '9': + state = integer_chars_state::decimal; + break; + default: + return false; + } + break; + } + case integer_chars_state::decimal: + { + switch(*s) + { + case '0':case '1':case '2':case '3':case '4':case '5':case '6':case '7':case '8': case '9': + break; + default: + return false; + } + break; + } + default: + break; + } + } + return state == integer_chars_state::decimal ? true : false; +} + +template <class T, class CharT> +typename std::enable_if<type_traits::integer_limits<T>::is_specialized && !type_traits::integer_limits<T>::is_signed,to_integer_result<T,CharT>>::type +to_integer_decimal(const CharT* s, std::size_t length, T& n) +{ + n = 0; + + integer_chars_state state = integer_chars_state::initial; + + const CharT* end = s + length; + while (s < end) + { + switch(state) + { + case integer_chars_state::initial: + { + switch(*s) + { + case '0': + if (++s == end) + { + return (++s == end) ? to_integer_result<T,CharT>(s) : to_integer_result<T, CharT>(s, to_integer_errc()); + } + else + { + return to_integer_result<T,CharT>(s, to_integer_errc::invalid_digit); + } + case '1':case '2':case '3':case '4':case '5':case '6':case '7':case '8': case '9': // Must be decimal + state = integer_chars_state::decimal; + break; + default: + return to_integer_result<T,CharT>(s, to_integer_errc::invalid_digit); + } + break; + } + case integer_chars_state::decimal: + { + static constexpr T max_value = (type_traits::integer_limits<T>::max)(); + static constexpr T max_value_div_10 = max_value / 10; + for (; s < end; ++s) + { + T x = 0; + switch(*s) + { + case '0':case '1':case '2':case '3':case '4':case '5':case '6':case '7':case '8': case '9': + x = static_cast<T>(*s) - static_cast<T>('0'); + break; + default: + return to_integer_result<T,CharT>(s, to_integer_errc::invalid_digit); + } + if (n > max_value_div_10) + { + return to_integer_result<T,CharT>(s, to_integer_errc::overflow); + } + n = n * 10; + if (n > max_value - x) + { + return to_integer_result<T,CharT>(s, to_integer_errc::overflow); + } + n += x; + } + break; + } + default: + JSONCONS_UNREACHABLE(); + break; + } + } + return (state == integer_chars_state::initial) ? to_integer_result<T,CharT>(s, to_integer_errc::invalid_number) : to_integer_result<T,CharT>(s, to_integer_errc()); +} + +template <class T, class CharT> +typename std::enable_if<type_traits::integer_limits<T>::is_specialized && type_traits::integer_limits<T>::is_signed,to_integer_result<T,CharT>>::type +to_integer_decimal(const CharT* s, std::size_t length, T& n) +{ + n = 0; + + if (length == 0) + { + return to_integer_result<T,CharT>(s, to_integer_errc::invalid_number); + } + + bool is_negative = *s == '-' ? true : false; + if (is_negative) + { + ++s; + --length; + } + + using U = typename type_traits::make_unsigned<T>::type; + + U u; + auto ru = to_integer_decimal(s, length, u); + if (ru.ec != to_integer_errc()) + { + return to_integer_result<T,CharT>(ru.ptr, ru.ec); + } + if (is_negative) + { + if (u > static_cast<U>(-((type_traits::integer_limits<T>::lowest)()+T(1))) + U(1)) + { + return to_integer_result<T,CharT>(ru.ptr, to_integer_errc::overflow); + } + else + { + n = static_cast<T>(U(0) - u); + return to_integer_result<T,CharT>(ru.ptr, to_integer_errc()); + } + } + else + { + if (u > static_cast<U>((type_traits::integer_limits<T>::max)())) + { + return to_integer_result<T,CharT>(ru.ptr, to_integer_errc::overflow); + } + else + { + n = static_cast<T>(u); + return to_integer_result<T,CharT>(ru.ptr, to_integer_errc()); + } + } +} + +template <class T, class CharT> +typename std::enable_if<type_traits::integer_limits<T>::is_specialized && !type_traits::integer_limits<T>::is_signed,to_integer_result<T,CharT>>::type +to_integer_base16(const CharT* s, std::size_t length, T& n) +{ + n = 0; + + integer_chars_state state = integer_chars_state::initial; + + const CharT* end = s + length; + while (s < end) + { + switch(state) + { + case integer_chars_state::initial: + { + switch(*s) + { + case '0': + if (++s == end) + { + return (++s == end) ? to_integer_result<T,CharT>(s) : to_integer_result<T, CharT>(s, to_integer_errc()); + } + else + { + return to_integer_result<T,CharT>(s, to_integer_errc::invalid_digit); + } + case '1':case '2':case '3':case '4':case '5':case '6':case '7':case '8': case '9': // Must be base16 + case 'a':case 'b':case 'c':case 'd':case 'e':case 'f': + case 'A':case 'B':case 'C':case 'D':case 'E':case 'F': + state = integer_chars_state::base16; + break; + default: + return to_integer_result<T,CharT>(s, to_integer_errc::invalid_digit); + } + break; + } + case integer_chars_state::base16: + { + static constexpr T max_value = (type_traits::integer_limits<T>::max)(); + static constexpr T max_value_div_16 = max_value / 16; + for (; s < end; ++s) + { + CharT c = *s; + T x = 0; + switch(*s) + { + case '0':case '1':case '2':case '3':case '4':case '5':case '6':case '7':case '8': case '9': + x = c - '0'; + break; + case 'a':case 'b':case 'c':case 'd':case 'e':case 'f': + x = c - ('a' - 10); + break; + case 'A':case 'B':case 'C':case 'D':case 'E':case 'F': + x = c - ('A' - 10); + break; + default: + return to_integer_result<T,CharT>(s, to_integer_errc::invalid_digit); + } + if (n > max_value_div_16) + { + return to_integer_result<T,CharT>(s, to_integer_errc::overflow); + } + n = n * 16; + if (n > max_value - x) + { + return to_integer_result<T,CharT>(s, to_integer_errc::overflow); + } + n += x; + } + break; + } + default: + JSONCONS_UNREACHABLE(); + break; + } + } + return (state == integer_chars_state::initial) ? to_integer_result<T,CharT>(s, to_integer_errc::invalid_number) : to_integer_result<T,CharT>(s, to_integer_errc()); +} + +template <class T, class CharT> +typename std::enable_if<type_traits::integer_limits<T>::is_specialized && type_traits::integer_limits<T>::is_signed,to_integer_result<T,CharT>>::type +to_integer_base16(const CharT* s, std::size_t length, T& n) +{ + n = 0; + + if (length == 0) + { + return to_integer_result<T,CharT>(s, to_integer_errc::invalid_number); + } + + bool is_negative = *s == '-' ? true : false; + if (is_negative) + { + ++s; + --length; + } + + using U = typename type_traits::make_unsigned<T>::type; + + U u; + auto ru = to_integer_base16(s, length, u); + if (ru.ec != to_integer_errc()) + { + return to_integer_result<T,CharT>(ru.ptr, ru.ec); + } + if (is_negative) + { + if (u > static_cast<U>(-((type_traits::integer_limits<T>::lowest)()+T(1))) + U(1)) + { + return to_integer_result<T,CharT>(ru.ptr, to_integer_errc::overflow); + } + else + { + n = static_cast<T>(U(0) - u); + return to_integer_result<T,CharT>(ru.ptr, to_integer_errc()); + } + } + else + { + if (u > static_cast<U>((type_traits::integer_limits<T>::max)())) + { + return to_integer_result<T,CharT>(ru.ptr, to_integer_errc::overflow); + } + else + { + n = static_cast<T>(u); + return to_integer_result<T,CharT>(ru.ptr, to_integer_errc()); + } + } +} + +template <class T, class CharT> +typename std::enable_if<type_traits::integer_limits<T>::is_specialized && !type_traits::integer_limits<T>::is_signed,to_integer_result<T,CharT>>::type +to_integer(const CharT* s, std::size_t length, T& n) +{ + n = 0; + + integer_chars_state state = integer_chars_state::initial; + + const CharT* end = s + length; + while (s < end) + { + switch(state) + { + case integer_chars_state::initial: + { + switch(*s) + { + case '0': + state = integer_chars_state::integer; // Could be binary, octal, hex + ++s; + break; + case '1':case '2':case '3':case '4':case '5':case '6':case '7':case '8': case '9': // Must be decimal + state = integer_chars_state::decimal; + break; + default: + return to_integer_result<T,CharT>(s, to_integer_errc::invalid_digit); + } + break; + } + case integer_chars_state::integer: + { + switch(*s) + { + case 'b':case 'B': + { + state = integer_chars_state::binary; + ++s; + break; + } + case 'x':case 'X': + { + state = integer_chars_state::base16; + ++s; + break; + } + case '0':case '1':case '2':case '3':case '4':case '5':case '6':case '7':case '8': case '9': + { + state = integer_chars_state::octal; + break; + } + default: + return to_integer_result<T,CharT>(s, to_integer_errc::invalid_digit); + } + break; + } + case integer_chars_state::binary: + { + static constexpr T max_value = (type_traits::integer_limits<T>::max)(); + static constexpr T max_value_div_2 = max_value / 2; + for (; s < end; ++s) + { + T x = 0; + switch(*s) + { + case '0':case '1': + x = static_cast<T>(*s) - static_cast<T>('0'); + break; + default: + return to_integer_result<T,CharT>(s, to_integer_errc::invalid_digit); + } + if (n > max_value_div_2) + { + return to_integer_result<T,CharT>(s, to_integer_errc::overflow); + } + n = n * 2; + if (n > max_value - x) + { + return to_integer_result<T,CharT>(s, to_integer_errc::overflow); + } + n += x; + } + break; + } + case integer_chars_state::octal: + { + static constexpr T max_value = (type_traits::integer_limits<T>::max)(); + static constexpr T max_value_div_8 = max_value / 8; + for (; s < end; ++s) + { + T x = 0; + switch(*s) + { + case '0':case '1':case '2':case '3':case '4':case '5':case '6':case '7': + x = static_cast<T>(*s) - static_cast<T>('0'); + break; + default: + return to_integer_result<T,CharT>(s, to_integer_errc::invalid_digit); + } + if (n > max_value_div_8) + { + return to_integer_result<T,CharT>(s, to_integer_errc::overflow); + } + n = n * 8; + if (n > max_value - x) + { + return to_integer_result<T,CharT>(s, to_integer_errc::overflow); + } + n += x; + } + break; + } + case integer_chars_state::decimal: + { + static constexpr T max_value = (type_traits::integer_limits<T>::max)(); + static constexpr T max_value_div_10 = max_value / 10; + for (; s < end; ++s) + { + T x = 0; + switch(*s) + { + case '0':case '1':case '2':case '3':case '4':case '5':case '6':case '7':case '8': case '9': + x = static_cast<T>(*s) - static_cast<T>('0'); + break; + default: + return to_integer_result<T,CharT>(s, to_integer_errc::invalid_digit); + } + if (n > max_value_div_10) + { + return to_integer_result<T,CharT>(s, to_integer_errc::overflow); + } + n = n * 10; + if (n > max_value - x) + { + return to_integer_result<T,CharT>(s, to_integer_errc::overflow); + } + n += x; + } + break; + } + case integer_chars_state::base16: + { + static constexpr T max_value = (type_traits::integer_limits<T>::max)(); + static constexpr T max_value_div_16 = max_value / 16; + for (; s < end; ++s) + { + CharT c = *s; + T x = 0; + switch (c) + { + case '0':case '1':case '2':case '3':case '4':case '5':case '6':case '7':case '8': case '9': + x = c - '0'; + break; + case 'a':case 'b':case 'c':case 'd':case 'e':case 'f': + x = c - ('a' - 10); + break; + case 'A':case 'B':case 'C':case 'D':case 'E':case 'F': + x = c - ('A' - 10); + break; + default: + return to_integer_result<T,CharT>(s, to_integer_errc::invalid_digit); + } + if (n > max_value_div_16) + { + return to_integer_result<T,CharT>(s, to_integer_errc::overflow); + } + n = n * 16; + if (n > max_value - x) + { + return to_integer_result<T,CharT>(s, to_integer_errc::overflow); + } + + n += x; + } + break; + } + default: + JSONCONS_UNREACHABLE(); + break; + } + } + return (state == integer_chars_state::initial) ? to_integer_result<T,CharT>(s, to_integer_errc::invalid_number) : to_integer_result<T,CharT>(s, to_integer_errc()); +} + +template <class T, class CharT> +typename std::enable_if<type_traits::integer_limits<T>::is_specialized && type_traits::integer_limits<T>::is_signed,to_integer_result<T,CharT>>::type +to_integer(const CharT* s, std::size_t length, T& n) +{ + n = 0; + + if (length == 0) + { + return to_integer_result<T,CharT>(s, to_integer_errc::invalid_number); + } + + bool is_negative = *s == '-' ? true : false; + if (is_negative) + { + ++s; + --length; + } + + using U = typename type_traits::make_unsigned<T>::type; + + U u; + auto ru = to_integer(s, length, u); + if (ru.ec != to_integer_errc()) + { + return to_integer_result<T,CharT>(ru.ptr, ru.ec); + } + if (is_negative) + { + if (u > static_cast<U>(-((type_traits::integer_limits<T>::lowest)()+T(1))) + U(1)) + { + return to_integer_result<T,CharT>(ru.ptr, to_integer_errc::overflow); + } + else + { + n = static_cast<T>(U(0) - u); + return to_integer_result<T,CharT>(ru.ptr, to_integer_errc()); + } + } + else + { + if (u > static_cast<U>((type_traits::integer_limits<T>::max)())) + { + return to_integer_result<T,CharT>(ru.ptr, to_integer_errc::overflow); + } + else + { + n = static_cast<T>(u); + return to_integer_result<T,CharT>(ru.ptr, to_integer_errc()); + } + } +} + +template <class T, class CharT> +typename std::enable_if<type_traits::integer_limits<T>::is_specialized,to_integer_result<T,CharT>>::type +to_integer(const CharT* s, T& n) +{ + return to_integer<T,CharT>(s, std::char_traits<CharT>::length(s), n); +} + +// Precondition: s satisfies + +// digit +// digit1-digits +// - digit +// - digit1-digits + +template <class T, class CharT> +typename std::enable_if<type_traits::integer_limits<T>::is_specialized && !type_traits::integer_limits<T>::is_signed,to_integer_result<T,CharT>>::type +to_integer_unchecked(const CharT* s, std::size_t length, T& n) +{ + static_assert(type_traits::integer_limits<T>::is_specialized, "Integer type not specialized"); + JSONCONS_ASSERT(length > 0); + + n = 0; + const CharT* end = s + length; + if (*s == '-') + { + static constexpr T min_value = (type_traits::integer_limits<T>::lowest)(); + static constexpr T min_value_div_10 = min_value / 10; + ++s; + for (; s < end; ++s) + { + T x = (T)*s - (T)('0'); + if (n < min_value_div_10) + { + return to_integer_result<T,CharT>(s, to_integer_errc::overflow); + } + n = n * 10; + if (n < min_value + x) + { + return to_integer_result<T,CharT>(s, to_integer_errc::overflow); + } + + n -= x; + } + } + else + { + static constexpr T max_value = (type_traits::integer_limits<T>::max)(); + static constexpr T max_value_div_10 = max_value / 10; + for (; s < end; ++s) + { + T x = static_cast<T>(*s) - static_cast<T>('0'); + if (n > max_value_div_10) + { + return to_integer_result<T,CharT>(s, to_integer_errc::overflow); + } + n = n * 10; + if (n > max_value - x) + { + return to_integer_result<T,CharT>(s, to_integer_errc::overflow); + } + + n += x; + } + } + + return to_integer_result<T,CharT>(s, to_integer_errc()); +} + +// Precondition: s satisfies + +// digit +// digit1-digits +// - digit +// - digit1-digits + +template <class T, class CharT> +typename std::enable_if<type_traits::integer_limits<T>::is_specialized && type_traits::integer_limits<T>::is_signed,to_integer_result<T,CharT>>::type +to_integer_unchecked(const CharT* s, std::size_t length, T& n) +{ + static_assert(type_traits::integer_limits<T>::is_specialized, "Integer type not specialized"); + JSONCONS_ASSERT(length > 0); + + n = 0; + + const CharT* end = s + length; + if (*s == '-') + { + static constexpr T min_value = (type_traits::integer_limits<T>::lowest)(); + static constexpr T min_value_div_10 = min_value / 10; + ++s; + for (; s < end; ++s) + { + T x = (T)*s - (T)('0'); + if (n < min_value_div_10) + { + return to_integer_result<T,CharT>(s, to_integer_errc::overflow); + } + n = n * 10; + if (n < min_value + x) + { + return to_integer_result<T,CharT>(s, to_integer_errc::overflow); + } + + n -= x; + } + } + else + { + static constexpr T max_value = (type_traits::integer_limits<T>::max)(); + static constexpr T max_value_div_10 = max_value / 10; + for (; s < end; ++s) + { + T x = static_cast<T>(*s) - static_cast<T>('0'); + if (n > max_value_div_10) + { + return to_integer_result<T,CharT>(s, to_integer_errc::overflow); + } + n = n * 10; + if (n > max_value - x) + { + return to_integer_result<T,CharT>(s, to_integer_errc::overflow); + } + + n += x; + } + } + + return to_integer_result<T,CharT>(s, to_integer_errc()); +} + +// base16_to_integer + +template <class T, class CharT> +typename std::enable_if<type_traits::integer_limits<T>::is_specialized && type_traits::integer_limits<T>::is_signed,to_integer_result<T,CharT>>::type +base16_to_integer(const CharT* s, std::size_t length, T& n) +{ + static_assert(type_traits::integer_limits<T>::is_specialized, "Integer type not specialized"); + JSONCONS_ASSERT(length > 0); + + n = 0; + + const CharT* end = s + length; + if (*s == '-') + { + static constexpr T min_value = (type_traits::integer_limits<T>::lowest)(); + static constexpr T min_value_div_16 = min_value / 16; + ++s; + for (; s < end; ++s) + { + CharT c = *s; + T x = 0; + switch (c) + { + case '0':case '1':case '2':case '3':case '4':case '5':case '6':case '7':case '8': case '9': + x = c - '0'; + break; + case 'a':case 'b':case 'c':case 'd':case 'e':case 'f': + x = c - ('a' - 10); + break; + case 'A':case 'B':case 'C':case 'D':case 'E':case 'F': + x = c - ('A' - 10); + break; + default: + return to_integer_result<T,CharT>(s, to_integer_errc::invalid_digit); + } + if (n < min_value_div_16) + { + return to_integer_result<T,CharT>(s, to_integer_errc::overflow); + } + n = n * 16; + if (n < min_value + x) + { + return to_integer_result<T,CharT>(s, to_integer_errc::overflow); + } + n -= x; + } + } + else + { + static constexpr T max_value = (type_traits::integer_limits<T>::max)(); + static constexpr T max_value_div_16 = max_value / 16; + for (; s < end; ++s) + { + CharT c = *s; + T x = 0; + switch (c) + { + case '0':case '1':case '2':case '3':case '4':case '5':case '6':case '7':case '8': case '9': + x = c - '0'; + break; + case 'a':case 'b':case 'c':case 'd':case 'e':case 'f': + x = c - ('a' - 10); + break; + case 'A':case 'B':case 'C':case 'D':case 'E':case 'F': + x = c - ('A' - 10); + break; + default: + return to_integer_result<T,CharT>(s, to_integer_errc::invalid_digit); + } + if (n > max_value_div_16) + { + return to_integer_result<T,CharT>(s, to_integer_errc::overflow); + } + n = n * 16; + if (n > max_value - x) + { + return to_integer_result<T,CharT>(s, to_integer_errc::overflow); + } + + n += x; + } + } + + return to_integer_result<T,CharT>(s, to_integer_errc()); +} + +template <class T, class CharT> +typename std::enable_if<type_traits::integer_limits<T>::is_specialized && !type_traits::integer_limits<T>::is_signed,to_integer_result<T,CharT>>::type +base16_to_integer(const CharT* s, std::size_t length, T& n) +{ + static_assert(type_traits::integer_limits<T>::is_specialized, "Integer type not specialized"); + JSONCONS_ASSERT(length > 0); + + n = 0; + const CharT* end = s + length; + + static constexpr T max_value = (type_traits::integer_limits<T>::max)(); + static constexpr T max_value_div_16 = max_value / 16; + for (; s < end; ++s) + { + CharT c = *s; + T x = *s; + switch (c) + { + case '0':case '1':case '2':case '3':case '4':case '5':case '6':case '7':case '8': case '9': + x = c - '0'; + break; + case 'a':case 'b':case 'c':case 'd':case 'e':case 'f': + x = c - ('a' - 10); + break; + case 'A':case 'B':case 'C':case 'D':case 'E':case 'F': + x = c - ('A' - 10); + break; + default: + return to_integer_result<T,CharT>(s, to_integer_errc::invalid_digit); + } + if (n > max_value_div_16) + { + return to_integer_result<T,CharT>(s, to_integer_errc::overflow); + } + n = n * 16; + if (n > max_value - x) + { + return to_integer_result<T,CharT>(s, to_integer_errc::overflow); + } + + n += x; + } + + return to_integer_result<T,CharT>(s, to_integer_errc()); +} + + +#if defined(JSONCONS_HAS_STD_FROM_CHARS) + +class chars_to +{ +public: + + char get_decimal_point() const + { + return '.'; + } + + template <class CharT> + typename std::enable_if<std::is_same<CharT,char>::value,double>::type + operator()(const CharT* s, std::size_t len) const + { + double val = 0; + const auto res = std::from_chars(s, s+len, val); + if (res.ec != std::errc()) + { + JSONCONS_THROW(json_runtime_error<std::invalid_argument>("Convert chars to double failed")); + } + return val; + } + + template <class CharT> + typename std::enable_if<std::is_same<CharT,wchar_t>::value,double>::type + operator()(const CharT* s, std::size_t len) const + { + std::string input(len,'0'); + for (size_t i = 0; i < len; ++i) + { + input[i] = static_cast<char>(s[i]); + } + + double val = 0; + const auto res = std::from_chars(input.data(), input.data() + len, val); + if (res.ec != std::errc()) + { + JSONCONS_THROW(json_runtime_error<std::invalid_argument>("Convert chars to double failed")); + } + return val; + } +}; +#elif defined(JSONCONS_HAS_MSC_STRTOD_L) + +class chars_to +{ +private: + _locale_t locale_; +public: + chars_to() + { + locale_ = _create_locale(LC_NUMERIC, "C"); + } + ~chars_to() noexcept + { + _free_locale(locale_); + } + + chars_to(const chars_to&) + { + locale_ = _create_locale(LC_NUMERIC, "C"); + } + + chars_to& operator=(const chars_to&) + { + // Don't assign locale + return *this; + } + + char get_decimal_point() const + { + return '.'; + } + + template <class CharT> + typename std::enable_if<std::is_same<CharT,char>::value,double>::type + operator()(const CharT* s, std::size_t) const + { + CharT *end = nullptr; + double val = _strtod_l(s, &end, locale_); + if (s == end) + { + JSONCONS_THROW(json_runtime_error<std::invalid_argument>("Convert string to double failed")); + } + return val; + } + + template <class CharT> + typename std::enable_if<std::is_same<CharT,wchar_t>::value,double>::type + operator()(const CharT* s, std::size_t) const + { + CharT *end = nullptr; + double val = _wcstod_l(s, &end, locale_); + if (s == end) + { + JSONCONS_THROW(json_runtime_error<std::invalid_argument>("Convert string to double failed")); + } + return val; + } +}; + +#elif defined(JSONCONS_HAS_STRTOLD_L) + +class chars_to +{ +private: + locale_t locale_; +public: + chars_to() + { + locale_ = newlocale(LC_ALL_MASK, "C", (locale_t) 0); + } + ~chars_to() noexcept + { + freelocale(locale_); + } + + chars_to(const chars_to&) + { + locale_ = newlocale(LC_ALL_MASK, "C", (locale_t) 0); + } + + chars_to& operator=(const chars_to&) + { + return *this; + } + + char get_decimal_point() const + { + return '.'; + } + + template <class CharT> + typename std::enable_if<std::is_same<CharT,char>::value,double>::type + operator()(const CharT* s, std::size_t) const + { + char *end = nullptr; + double val = strtold_l(s, &end, locale_); + if (s == end) + { + JSONCONS_THROW(json_runtime_error<std::invalid_argument>("Convert string to double failed")); + } + return val; + } + + template <class CharT> + typename std::enable_if<std::is_same<CharT,wchar_t>::value,double>::type + operator()(const CharT* s, std::size_t) const + { + CharT *end = nullptr; + double val = wcstold_l(s, &end, locale_); + if (s == end) + { + JSONCONS_THROW(json_runtime_error<std::invalid_argument>("Convert string to double failed")); + } + return val; + } +}; + +#else +class chars_to +{ +private: + std::vector<char> buffer_; + char decimal_point_; +public: + chars_to() + : buffer_() + { + struct lconv * lc = localeconv(); + if (lc != nullptr && lc->decimal_point[0] != 0) + { + decimal_point_ = lc->decimal_point[0]; + } + else + { + decimal_point_ = '.'; + } + buffer_.reserve(100); + } + + chars_to(const chars_to&) = default; + chars_to& operator=(const chars_to&) = default; + + char get_decimal_point() const + { + return decimal_point_; + } + + template <class CharT> + typename std::enable_if<std::is_same<CharT,char>::value,double>::type + operator()(const CharT* s, std::size_t /*length*/) const + { + CharT *end = nullptr; + double val = strtod(s, &end); + if (s == end) + { + JSONCONS_THROW(json_runtime_error<std::invalid_argument>("Convert string to double failed")); + } + return val; + } + + template <class CharT> + typename std::enable_if<std::is_same<CharT,wchar_t>::value,double>::type + operator()(const CharT* s, std::size_t /*length*/) const + { + CharT *end = nullptr; + double val = wcstod(s, &end); + if (s == end) + { + JSONCONS_THROW(json_runtime_error<std::invalid_argument>("Convert string to double failed")); + } + return val; + } +}; +#endif + +}} + +#endif diff --git a/include/jsoncons/detail/span.hpp b/include/jsoncons/detail/span.hpp new file mode 100644 index 0000000..02c24ee --- /dev/null +++ b/include/jsoncons/detail/span.hpp @@ -0,0 +1,188 @@ +#ifndef JSONCONS_DETAIL_SPAN_HPP +#define JSONCONS_DETAIL_SPAN_HPP + +#include <utility> // std::swap +#include <memory> // std::addressof +#include <type_traits> // std::enable_if, std::true_type, std::false_type +#include <jsoncons/config/compiler_support.hpp> +#include <jsoncons/more_type_traits.hpp> +#include <iterator> +#include <limits> + +namespace jsoncons +{ +namespace detail +{ + constexpr std::size_t dynamic_extent = (std::numeric_limits<std::size_t>::max)(); + + template< class T, std::size_t Extent = dynamic_extent> + class span; + + template<class T> + struct is_span : std::false_type{}; + + template< class T> + struct is_span<span<T>> : std::true_type{}; + + template< + class T, + std::size_t Extent + > class span + { + public: + using element_type = T; + using value_type = typename std::remove_volatile<typename std::remove_const<T>::type>::type; + using size_type = std::size_t; + using difference_type = std::ptrdiff_t; + using pointer = T*; + using const_pointer = const T*; + using reference = T&; + using const_reference = const T&; + using iterator = pointer; + using const_iterator = const_pointer; + using reverse_iterator = std::reverse_iterator<iterator>; + using const_reverse_iterator = std::reverse_iterator<const_iterator>; + private: + pointer data_; + size_type size_; + public: + static constexpr std::size_t extent = Extent; + + constexpr span() noexcept + : data_(nullptr), size_(0) + { + } + constexpr span(pointer data, size_type size) + : data_(data), size_(size) + { + } + + template <typename C> + constexpr span(C& c, + typename std::enable_if<!is_span<C>::value && !type_traits::is_std_array<C>::value && type_traits::is_compatible_element<C,element_type>::value && type_traits::has_data_and_size<C>::value>::type* = 0) + : data_(c.data()), size_(c.size()) + { + } + + template <std::size_t N> + span(element_type (&arr)[N]) noexcept + : data_(std::addressof(arr[0])), size_(N) + { + } + + template <std::size_t N> + span(std::array<value_type, N>& arr, + typename std::enable_if<(extent == dynamic_extent || extent == N)>::type* = 0) noexcept + : data_(arr.data()), size_(arr.size()) + { + } + + template <std::size_t N> + span(const std::array<value_type, N>& arr, + typename std::enable_if<(extent == dynamic_extent || extent == N)>::type* = 0) noexcept + : data_(arr.data()), size_(arr.size()) + { + } + + template <typename C> + constexpr span(const C& c, + typename std::enable_if<!is_span<C>::value && !type_traits::is_std_array<C>::value && type_traits::is_compatible_element<C,element_type>::value && type_traits::has_data_and_size<C>::value>::type* = 0) + : data_(c.data()), size_(c.size()) + { + } + + template <class U, std::size_t N> + constexpr span(const span<U, N>& s, + typename std::enable_if<(N == dynamic_extent || N == extent) && std::is_convertible<U(*)[], element_type(*)[]>::value>::type* = 0) noexcept + : data_(s.data()), size_(s.size()) + { + } + + constexpr span(const span& other) noexcept = default; + + span& operator=( const span& other ) noexcept = default; + + constexpr pointer data() const noexcept + { + return data_; + } + + constexpr size_type size() const noexcept + { + return size_; + } + + constexpr bool empty() const noexcept + { + return size_ == 0; + } + + constexpr reference operator[](size_type index) const + { + return data_[index]; + } + + // iterator support + const_iterator begin() const noexcept + { + return data_; + } + const_iterator end() const noexcept + { + return data_ + size_; + } + const_iterator cbegin() const noexcept + { + return data_; + } + const_iterator cend() const noexcept + { + return data_ + size_; + } + const_reverse_iterator rbegin() const noexcept + { + return const_reverse_iterator(end()); + } + const_reverse_iterator rend() const noexcept + { + return const_reverse_iterator(begin()); + } + const_reverse_iterator crbegin() const noexcept + { + return const_reverse_iterator(end()); + } + const_reverse_iterator crend() const noexcept + { + return const_reverse_iterator(begin()); + } + + span<element_type, dynamic_extent> + first(std::size_t count) const + { + JSONCONS_ASSERT(count <= size()); + + return span< element_type, dynamic_extent >( data(), count ); + } + + span<element_type, dynamic_extent> + last(std::size_t count) const + { + JSONCONS_ASSERT(count <= size()); + + return span<element_type, dynamic_extent>(data() + ( size() - count ), count); + } + + span<element_type, dynamic_extent> + subspan(std::size_t offset, std::size_t count = dynamic_extent) const + { + //JSONCONS_ASSERT((offset <= size() && (count == dynamic_extent || (offset + count <= size())))); + + return span<element_type, dynamic_extent>( + data() + offset, count == dynamic_extent ? size() - offset : count ); + } + }; + +} // namespace detail +} // namespace jsoncons + +#endif diff --git a/include/jsoncons/detail/string_view.hpp b/include/jsoncons/detail/string_view.hpp new file mode 100644 index 0000000..fc568a1 --- /dev/null +++ b/include/jsoncons/detail/string_view.hpp @@ -0,0 +1,537 @@ +// Copyright 2019 Daniel Parker +// Distributed under the Boost license, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +// See https://github.com/danielaparker/jsoncons for latest version + +#ifndef JSONCONS_STRING_VIEW_HPP +#define JSONCONS_STRING_VIEW_HPP + +#include <stdexcept> +#include <string> +#include <vector> +#include <ostream> +#include <cmath> +#include <algorithm> // std::find, std::min, std::reverse +#include <memory> +#include <iterator> +#include <exception> +#include <stdexcept> +#include <istream> // std::basic_istream +#include <jsoncons/config/compiler_support.hpp> + +namespace jsoncons { +namespace detail { + + template <class CharT, class Traits = std::char_traits<CharT>> + class basic_string_view + { + private: + const CharT* data_; + std::size_t length_; + public: + using value_type = CharT; + using const_reference = const CharT&; + using traits_type = Traits; + using size_type = std::size_t; + static constexpr size_type npos = size_type(-1); + using const_iterator = const CharT*; + using iterator = const CharT*; + using const_reverse_iterator = std::reverse_iterator<const_iterator>; + + constexpr basic_string_view() noexcept + : data_(nullptr), length_(0) + { + } + constexpr basic_string_view(const CharT* data, std::size_t length) + : data_(data), length_(length) + { + } + + basic_string_view(const CharT* data) + : data_(data), length_(Traits::length(data)) + { + } + constexpr basic_string_view(const basic_string_view& other) noexcept = default; + + template <class Tr, class Allocator> + JSONCONS_CPP14_CONSTEXPR basic_string_view(const std::basic_string<CharT,Tr,Allocator>& s) noexcept + : data_(s.data()), length_(s.length()) + { + } + + JSONCONS_CPP14_CONSTEXPR basic_string_view& operator=( const basic_string_view& view ) noexcept + { + data_ = view.data(); + length_ = view.length(); + + return *this; + } + + template <class Allocator> + explicit operator std::basic_string<CharT,Traits,Allocator>() const + { + return std::basic_string<CharT,Traits,Allocator>(data_,length_); + } + + // iterator support + const_iterator begin() const noexcept + { + return data_; + } + const_iterator end() const noexcept + { + return data_ + length_; + } + const_iterator cbegin() const noexcept + { + return data_; + } + const_iterator cend() const noexcept + { + return data_ + length_; + } + const_reverse_iterator rbegin() const noexcept + { + return const_reverse_iterator(end()); + } + const_reverse_iterator rend() const noexcept + { + return const_reverse_iterator(begin()); + } + const_reverse_iterator crbegin() const noexcept + { + return const_reverse_iterator(end()); + } + const_reverse_iterator crend() const noexcept + { + return const_reverse_iterator(begin()); + } + + // capacity + + std::size_t size() const + { + return length_; + } + + std::size_t length() const + { + return length_; + } + size_type max_size() const noexcept + { + return length_; + } + bool empty() const noexcept + { + return length_ == 0; + } + + // element access + + const_reference operator[](size_type pos) const + { + return data_[pos]; + } + + const_reference at(std::size_t pos) const + { + if (pos >= length_) + { + JSONCONS_THROW(std::out_of_range("pos exceeds length")); + } + return data_[pos]; + } + + const_reference front() const + { + return data_[0]; + } + const_reference back() const + { + return data_[length_-1]; + } + + const CharT* data() const + { + return data_; + } + + // string operations + + basic_string_view substr(size_type pos, size_type n=npos) const + { + if (pos > length_) + { + JSONCONS_THROW(std::out_of_range("pos exceeds size")); + } + if (n == npos || pos + n > length_) + { + n = length_ - pos; + } + return basic_string_view(data_ + pos, n); + } + + int compare(const basic_string_view& s) const noexcept + { + const int rc = Traits::compare(data_, s.data_, (std::min)(length_, s.length_)); + return rc != 0 ? rc : (length_ == s.length_ ? 0 : length_ < s.length_ ? -1 : 1); + } + + int compare(const CharT* data) const noexcept + { + const size_t length = Traits::length(data); + const int rc = Traits::compare(data_, data, (std::min)(length_, length)); + return rc != 0 ? rc : (length_ == length? 0 : length_ < length? -1 : 1); + } + + template <class Allocator> + int compare(const std::basic_string<CharT,Traits,Allocator>& s) const noexcept + { + const int rc = Traits::compare(data_, s.data(), (std::min)(length_, s.length())); + return rc != 0 ? rc : (length_ == s.length() ? 0 : length_ < s.length() ? -1 : 1); + } + + size_type find(basic_string_view s, size_type pos = 0) const noexcept + { + if (pos > length_) + { + return npos; + } + if (s.length_ == 0) + { + return pos; + } + const_iterator it = std::search(cbegin() + pos, cend(), + s.cbegin(), s.cend(), Traits::eq); + return it == cend() ? npos : std::distance(cbegin(), it); + } + size_type find(CharT ch, size_type pos = 0) const noexcept + { + return find(basic_string_view(&ch, 1), pos); + } + size_type find(const CharT* s, size_type pos, size_type n) const noexcept + { + return find(basic_string_view(s, n), pos); + } + size_type find(const CharT* s, size_type pos = 0) const noexcept + { + return find(basic_string_view(s), pos); + } + + size_type rfind(basic_string_view s, size_type pos = npos) const noexcept + { + if (length_ < s.length_) + { + return npos; + } + if (pos > length_ - s.length_) + { + pos = length_ - s.length_; + } + if (s.length_ == 0) + { + return pos; + } + for (const CharT* p = data_ + pos; true; --p) + { + if (Traits::compare(p, s.data_, s.length_) == 0) + { + return p - data_; + } + if (p == data_) + { + return npos; + } + }; + } + size_type rfind(CharT ch, size_type pos = npos) const noexcept + { + return rfind(basic_string_view(&ch, 1), pos); + } + size_type rfind(const CharT* s, size_type pos, size_type n) const noexcept + { + return rfind(basic_string_view(s, n), pos); + } + size_type rfind(const CharT* s, size_type pos = npos) const noexcept + { + return rfind(basic_string_view(s), pos); + } + + size_type find_first_of(basic_string_view s, size_type pos = 0) const noexcept + { + if (pos >= length_ || s.length_ == 0) + { + return npos; + } + const_iterator it = std::find_first_of + (cbegin() + pos, cend(), s.cbegin(), s.cend(), Traits::eq); + return it == cend() ? npos : std::distance (cbegin(), it); + } + size_type find_first_of(CharT ch, size_type pos = 0) const noexcept + { + return find_first_of(basic_string_view(&ch, 1), pos); + } + size_type find_first_of(const CharT* s, size_type pos, size_type n) const noexcept + { + return find_first_of(basic_string_view(s, n), pos); + } + size_type find_first_of(const CharT* s, size_type pos = 0) const noexcept + { + return find_first_of(basic_string_view(s), pos); + } + + size_type find_last_of(basic_string_view s, size_type pos = npos) const noexcept + { + if (s.length_ == 0) + { + return npos; + } + if (pos >= length_) + { + pos = 0; + } + else + { + pos = length_ - (pos+1); + } + const_reverse_iterator it = std::find_first_of + (crbegin() + pos, crend(), s.cbegin(), s.cend(), Traits::eq); + return it == crend() ? npos : (length_ - 1 - std::distance(crbegin(), it)); + } + size_type find_last_of(CharT ch, size_type pos = npos) const noexcept + { + return find_last_of(basic_string_view(&ch, 1), pos); + } + size_type find_last_of(const CharT* s, size_type pos, size_type n) const noexcept + { + return find_last_of(basic_string_view(s, n), pos); + } + size_type find_last_of(const CharT* s, size_type pos = npos) const noexcept + { + return find_last_of(basic_string_view(s), pos); + } + + size_type find_first_not_of(basic_string_view s, size_type pos = 0) const noexcept + { + if (pos >= length_) + return npos; + if (s.length_ == 0) + return pos; + + const_iterator it = cend(); + for (auto p = cbegin()+pos; p != cend(); ++p) + { + if (Traits::find(s.data_, s.length_, *p) == 0) + { + it = p; + break; + } + } + return it == cend() ? npos : std::distance (cbegin(), it); + } + size_type find_first_not_of(CharT ch, size_type pos = 0) const noexcept + { + return find_first_not_of(basic_string_view(&ch, 1), pos); + } + size_type find_first_not_of(const CharT* s, size_type pos, size_type n) const noexcept + { + return find_first_not_of(basic_string_view(s, n), pos); + } + size_type find_first_not_of(const CharT* s, size_type pos = 0) const noexcept + { + return find_first_not_of(basic_string_view(s), pos); + } + + size_type find_last_not_of(basic_string_view s, size_type pos = npos) const noexcept + { + if (pos >= length_) + { + pos = length_ - 1; + } + if (s.length_ == 0) + { + return pos; + } + pos = length_ - (pos+1); + + const_iterator it = crend(); + for (auto p = crbegin()+pos; p != crend(); ++p) + { + if (Traits::find(s.data_, s.length_, *p) == 0) + { + it = p; + break; + } + } + return it == crend() ? npos : (length_ - 1 - std::distance(crbegin(), it)); + } + size_type find_last_not_of(CharT ch, size_type pos = npos) const noexcept + { + return find_last_not_of(basic_string_view(&ch, 1), pos); + } + size_type find_last_not_of(const CharT* s, size_type pos, size_type n) const noexcept + { + return find_last_not_of(basic_string_view(s, n), pos); + } + size_type find_last_not_of(const CharT* s, size_type pos = npos) const noexcept + { + return find_last_not_of(basic_string_view(s), pos); + } + + friend std::basic_ostream<CharT>& operator<<(std::basic_ostream<CharT>& os, const basic_string_view& sv) + { + os.write(sv.data_,sv.length_); + return os; + } + }; + + // == + template<class CharT,class Traits> + bool operator==(const basic_string_view<CharT,Traits>& lhs, + const basic_string_view<CharT,Traits>& rhs) noexcept + { + return lhs.compare(rhs) == 0; + } + template<class CharT,class Traits,class Allocator> + bool operator==(const basic_string_view<CharT,Traits>& lhs, + const std::basic_string<CharT,Traits,Allocator>& rhs) noexcept + { + return lhs.compare(rhs) == 0; + } + template<class CharT,class Traits,class Allocator> + bool operator==(const std::basic_string<CharT,Traits,Allocator>& lhs, + const basic_string_view<CharT,Traits>& rhs) noexcept + { + return rhs.compare(lhs) == 0; + } + template<class CharT,class Traits> + bool operator==(const basic_string_view<CharT,Traits>& lhs, + const CharT* rhs) noexcept + { + return lhs.compare(rhs) == 0; + } + template<class CharT,class Traits> + bool operator==(const CharT* lhs, + const basic_string_view<CharT,Traits>& rhs) noexcept + { + return rhs.compare(lhs) == 0; + } + + // != + template<class CharT,class Traits> + bool operator!=(const basic_string_view<CharT,Traits>& lhs, + const basic_string_view<CharT,Traits>& rhs) noexcept + { + return lhs.compare(rhs) != 0; + } + template<class CharT,class Traits,class Allocator> + bool operator!=(const basic_string_view<CharT,Traits>& lhs, + const std::basic_string<CharT,Traits,Allocator>& rhs) noexcept + { + return lhs.compare(rhs) != 0; + } + template<class CharT,class Traits,class Allocator> + bool operator!=(const std::basic_string<CharT,Traits,Allocator>& lhs, + const basic_string_view<CharT,Traits>& rhs) noexcept + { + return rhs.compare(lhs) != 0; + } + template<class CharT,class Traits> + bool operator!=(const basic_string_view<CharT,Traits>& lhs, + const CharT* rhs) noexcept + { + return lhs.compare(rhs) != 0; + } + template<class CharT,class Traits> + bool operator!=(const CharT* lhs, + const basic_string_view<CharT,Traits>& rhs) noexcept + { + return rhs.compare(lhs) != 0; + } + + // <= + template<class CharT,class Traits> + bool operator<=(const basic_string_view<CharT,Traits>& lhs, + const basic_string_view<CharT,Traits>& rhs) noexcept + { + return lhs.compare(rhs) <= 0; + } + template<class CharT,class Traits,class Allocator> + bool operator<=(const basic_string_view<CharT,Traits>& lhs, + const std::basic_string<CharT,Traits,Allocator>& rhs) noexcept + { + return lhs.compare(rhs) <= 0; + } + template<class CharT,class Traits,class Allocator> + bool operator<=(const std::basic_string<CharT,Traits,Allocator>& lhs, + const basic_string_view<CharT,Traits>& rhs) noexcept + { + return rhs.compare(lhs) >= 0; + } + + // < + template<class CharT,class Traits> + bool operator<(const basic_string_view<CharT,Traits>& lhs, + const basic_string_view<CharT,Traits>& rhs) noexcept + { + return lhs.compare(rhs) < 0; + } + template<class CharT,class Traits,class Allocator> + bool operator<(const basic_string_view<CharT,Traits>& lhs, + const std::basic_string<CharT,Traits,Allocator>& rhs) noexcept + { + return lhs.compare(rhs) < 0; + } + template<class CharT,class Traits,class Allocator> + bool operator<(const std::basic_string<CharT,Traits,Allocator>& lhs, + const basic_string_view<CharT,Traits>& rhs) noexcept + { + return rhs.compare(lhs) > 0; + } + + // >= + template<class CharT,class Traits> + bool operator>=(const basic_string_view<CharT,Traits>& lhs, + const basic_string_view<CharT,Traits>& rhs) noexcept + { + return lhs.compare(rhs) >= 0; + } + template<class CharT,class Traits,class Allocator> + bool operator>=(const basic_string_view<CharT,Traits>& lhs, + const std::basic_string<CharT,Traits,Allocator>& rhs) noexcept + { + return lhs.compare(rhs) >= 0; + } + template<class CharT,class Traits,class Allocator> + bool operator>=(const std::basic_string<CharT,Traits,Allocator>& lhs, + const basic_string_view<CharT,Traits>& rhs) noexcept + { + return rhs.compare(lhs) <= 0; + } + + // > + template<class CharT,class Traits> + bool operator>(const basic_string_view<CharT,Traits>& lhs, + const basic_string_view<CharT,Traits>& rhs) noexcept + { + return lhs.compare(rhs) > 0; + } + template<class CharT,class Traits,class Allocator> + bool operator>(const basic_string_view<CharT,Traits>& lhs, + const std::basic_string<CharT,Traits,Allocator>& rhs) noexcept + { + return lhs.compare(rhs) > 0; + } + template<class CharT,class Traits,class Allocator> + bool operator>(const std::basic_string<CharT,Traits,Allocator>& lhs, + const basic_string_view<CharT,Traits>& rhs) noexcept + { + return rhs.compare(lhs) < 0; + } + +} // namespace detail +} // namespace jsoncons + +#endif diff --git a/include/jsoncons/detail/string_wrapper.hpp b/include/jsoncons/detail/string_wrapper.hpp new file mode 100644 index 0000000..88634b0 --- /dev/null +++ b/include/jsoncons/detail/string_wrapper.hpp @@ -0,0 +1,370 @@ +// Copyright 2013 Daniel Parker +// Distributed under the Boost license, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +// See https://github.com/danielaparker/jsoncons for latest version + +#ifndef JSONCONS_DETAIL_STRING_WRAPPER_HPP +#define JSONCONS_DETAIL_STRING_WRAPPER_HPP + +#include <stdexcept> +#include <string> +#include <exception> +#include <ostream> +#include <cstring> // std::memcpy +#include <memory> // std::allocator +#include <jsoncons/config/compiler_support.hpp> + +namespace jsoncons { +namespace detail { + + // From boost 1_71 + template <class T, class U> + T launder_cast(U* u) + { + #if defined(__cpp_lib_launder) && __cpp_lib_launder >= 201606 + return std::launder(reinterpret_cast<T>(u)); + #elif defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__) > 800 + return __builtin_launder(reinterpret_cast<T>(u)); + #else + return reinterpret_cast<T>(u); + #endif + } + + // string_wrapper + + template <class CharT,class Allocator> + class string_wrapper + { + public: + using char_type = CharT; + private: + struct str_base_t + { + Allocator alloc_; + + Allocator& get_allocator() + { + return alloc_; + } + + const Allocator& get_allocator() const + { + return alloc_; + } + + str_base_t(const Allocator& alloc) + : alloc_(alloc) + { + } + + ~str_base_t() noexcept = default; + }; + + struct str_t : public str_base_t + { + typedef typename std::allocator_traits<Allocator>::template rebind_alloc<CharT> allocator_type; + using allocator_traits_type = std::allocator_traits<allocator_type>; + using pointer = typename allocator_traits_type::pointer; + + pointer p_; + std::size_t length_; + + ~str_t() noexcept = default; + + const char_type* c_str() const { return type_traits::to_plain_pointer(p_); } + const char_type* data() const { return type_traits::to_plain_pointer(p_); } + std::size_t length() const { return length_; } + + str_t(const Allocator& alloc) + : str_base_t(alloc), p_(nullptr), length_(0) + { + + } + + str_t(const str_t&) = delete; + str_t& operator=(const str_t&) = delete; + + }; + + typedef typename std::allocator_traits<Allocator>::template rebind_alloc<char> byte_allocator_type; + using byte_pointer = typename std::allocator_traits<byte_allocator_type>::pointer; + + typedef typename std::allocator_traits<Allocator>::template rebind_alloc<str_t> string_allocator_type; + using string_pointer = typename std::allocator_traits<string_allocator_type>::pointer; + + struct storage_t + { + str_t data; + char_type c[1]; + }; + typedef typename std::aligned_storage<sizeof(storage_t), alignof(storage_t)>::type json_storage_kind; + + string_pointer ptr_; + public: + string_wrapper() = default; + + string_wrapper(string_pointer ptr) + : ptr_(ptr) + { + } + + string_wrapper(const char_type* data, std::size_t length, const Allocator& a) + { + ptr_ = create(data,length,a); + } + + string_wrapper(const string_wrapper& val) + { + ptr_ = create(val.data(),val.length(),val.get_allocator()); + } + + string_wrapper(const string_wrapper& val, const Allocator& a) + { + ptr_ = create(val.data(),val.length(),a); + } + + ~string_wrapper() noexcept + { + if (ptr_ != nullptr) + { + destroy(ptr_); + } + } + + void swap(string_wrapper& other) noexcept + { + std::swap(ptr_,other.ptr_); + } + + const char_type* data() const + { + return ptr_->data(); + } + + const char_type* c_str() const + { + return ptr_->c_str(); + } + + std::size_t length() const + { + return ptr_->length(); + } + + Allocator get_allocator() const + { + return ptr_->get_allocator(); + } + private: + static size_t aligned_size(std::size_t n) + { + return sizeof(json_storage_kind) + n; + } + + static string_pointer create(const char_type* s, std::size_t length, const Allocator& alloc) + { + std::size_t mem_size = aligned_size(length*sizeof(char_type)); + + byte_allocator_type byte_alloc(alloc); + byte_pointer ptr = byte_alloc.allocate(mem_size); + + char* storage = type_traits::to_plain_pointer(ptr); + str_t* ps = new(storage)str_t(byte_alloc); + + auto psa = launder_cast<storage_t*>(storage); + + CharT* p = new(&psa->c)char_type[length + 1]; + std::memcpy(p, s, length*sizeof(char_type)); + p[length] = 0; + ps->p_ = std::pointer_traits<typename str_t::pointer>::pointer_to(*p); + ps->length_ = length; + return std::pointer_traits<string_pointer>::pointer_to(*ps); + } + + static void destroy(string_pointer ptr) + { + str_t* rawp = type_traits::to_plain_pointer(ptr); + + char* p = launder_cast<char*>(rawp); + + std::size_t mem_size = aligned_size(ptr->length_*sizeof(char_type)); + byte_allocator_type byte_alloc(ptr->get_allocator()); + byte_alloc.deallocate(p,mem_size); + } + }; + + // tagged_string_wrapper + + template <class CharT,class Allocator> + class tagged_string_wrapper + { + public: + using char_type = CharT; + private: + struct str_base_t + { + Allocator alloc_; + + Allocator& get_allocator() + { + return alloc_; + } + + const Allocator& get_allocator() const + { + return alloc_; + } + + str_base_t(const Allocator& alloc) + : alloc_(alloc) + { + } + + ~str_base_t() noexcept = default; + }; + + struct str_t : public str_base_t + { + typedef typename std::allocator_traits<Allocator>::template rebind_alloc<CharT> allocator_type; + using allocator_traits_type = std::allocator_traits<allocator_type>; + using pointer = typename allocator_traits_type::pointer; + + pointer p_; + std::size_t length_; + uint64_t tag_; + + ~str_t() noexcept = default; + + const char_type* c_str() const { return type_traits::to_plain_pointer(p_); } + const char_type* data() const { return type_traits::to_plain_pointer(p_); } + std::size_t length() const { return length_; } + uint64_t tag() const { return tag_; } + + str_t(uint64_t tag, const Allocator& alloc) + : str_base_t(alloc), p_(nullptr), length_(0), tag_(tag) + { + + } + + str_t(const str_t&) = delete; + str_t& operator=(const str_t&) = delete; + + }; + + typedef typename std::allocator_traits<Allocator>::template rebind_alloc<char> byte_allocator_type; + using byte_pointer = typename std::allocator_traits<byte_allocator_type>::pointer; + + typedef typename std::allocator_traits<Allocator>::template rebind_alloc<str_t> string_allocator_type; + using string_pointer = typename std::allocator_traits<string_allocator_type>::pointer; + + struct storage_t + { + str_t data; + char_type c[1]; + }; + typedef typename std::aligned_storage<sizeof(storage_t), alignof(storage_t)>::type json_storage_kind; + + string_pointer ptr_; + public: + tagged_string_wrapper() = default; + + tagged_string_wrapper(string_pointer ptr) + : ptr_(ptr) + { + } + + tagged_string_wrapper(const char_type* data, std::size_t length, uint64_t tag, const Allocator& alloc) + { + ptr_ = create(data, length, tag, alloc); + } + + tagged_string_wrapper(const tagged_string_wrapper& val) + { + ptr_ = create(val.data(), val.length(), val.tag(), val.get_allocator()); + } + + tagged_string_wrapper(const tagged_string_wrapper& val, const Allocator& alloc) + { + ptr_ = create(val.data(), val.length(), val.tag(), alloc); + } + + ~tagged_string_wrapper() noexcept + { + if (ptr_ != nullptr) + { + destroy(ptr_); + } + } + + void swap(tagged_string_wrapper& other) noexcept + { + std::swap(ptr_,other.ptr_); + } + + const char_type* data() const + { + return ptr_->data(); + } + + const char_type* c_str() const + { + return ptr_->c_str(); + } + + std::size_t length() const + { + return ptr_->length(); + } + + uint64_t tag() const + { + return ptr_->tag(); + } + + Allocator get_allocator() const + { + return ptr_->get_allocator(); + } + private: + static size_t aligned_size(std::size_t n) + { + return sizeof(json_storage_kind) + n; + } + + static string_pointer create(const char_type* s, std::size_t length, uint64_t tag, const Allocator& alloc) + { + std::size_t mem_size = aligned_size(length*sizeof(char_type)); + + byte_allocator_type byte_alloc(alloc); + byte_pointer ptr = byte_alloc.allocate(mem_size); + + char* storage = type_traits::to_plain_pointer(ptr); + str_t* ps = new(storage)str_t(tag, byte_alloc); + + auto psa = launder_cast<storage_t*>(storage); + + CharT* p = new(&psa->c)char_type[length + 1]; + std::memcpy(p, s, length*sizeof(char_type)); + p[length] = 0; + ps->p_ = std::pointer_traits<typename str_t::pointer>::pointer_to(*p); + ps->length_ = length; + return std::pointer_traits<string_pointer>::pointer_to(*ps); + } + + static void destroy(string_pointer ptr) + { + str_t* rawp = type_traits::to_plain_pointer(ptr); + + char* p = launder_cast<char*>(rawp); + + std::size_t mem_size = aligned_size(ptr->length_*sizeof(char_type)); + byte_allocator_type byte_alloc(ptr->get_allocator()); + byte_alloc.deallocate(p,mem_size); + } + }; + +} // namespace detail +} // namespace jsoncons + +#endif diff --git a/include/jsoncons/detail/write_number.hpp b/include/jsoncons/detail/write_number.hpp new file mode 100644 index 0000000..2613467 --- /dev/null +++ b/include/jsoncons/detail/write_number.hpp @@ -0,0 +1,567 @@ +// Copyright 2013 Daniel Parker +// Distributed under the Boost license, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +// See https://github.com/danielaparker/jsoncons for latest version + +#ifndef JSONCONS_DETAIL_WRITE_NUMBER_HPP +#define JSONCONS_DETAIL_WRITE_NUMBER_HPP + +#include <stdexcept> +#include <string> +#include <cmath> +#include <locale> +#include <limits> // std::numeric_limits +#include <exception> +#include <stdio.h> // snprintf +#include <jsoncons/config/jsoncons_config.hpp> +#include <jsoncons/json_options.hpp> +#include <jsoncons/detail/grisu3.hpp> +#include <jsoncons/detail/parse_number.hpp> +#include <jsoncons/more_type_traits.hpp> + +namespace jsoncons { +namespace detail { + + inline + char to_hex_character(uint8_t c) + { + return (char)((c < 10) ? ('0' + c) : ('A' - 10 + c)); + } + + // from_integer + + template<class Integer,class Result> + typename std::enable_if<type_traits::is_integer<Integer>::value,std::size_t>::type + from_integer(Integer value, Result& result) + { + using char_type = typename Result::value_type; + + char_type buf[255]; + char_type *p = buf; + const char_type* last = buf+255; + + bool is_negative = value < 0; + + if (value < 0) + { + do + { + *p++ = static_cast<char_type>(48 - (value % 10)); + } + while ((value /= 10) && (p < last)); + } + else + { + + do + { + *p++ = static_cast<char_type>(48 + value % 10); + } + while ((value /= 10) && (p < last)); + } + JSONCONS_ASSERT(p != last); + + std::size_t count = (p - buf); + if (is_negative) + { + result.push_back('-'); + ++count; + } + while (--p >= buf) + { + result.push_back(*p); + } + + return count; + } + + // integer_to_string_hex + + template<class Integer,class Result> + typename std::enable_if<type_traits::is_integer<Integer>::value,std::size_t>::type + integer_to_string_hex(Integer value, Result& result) + { + using char_type = typename Result::value_type; + + char_type buf[255]; + char_type *p = buf; + const char_type* last = buf+255; + + bool is_negative = value < 0; + + if (value < 0) + { + do + { + *p++ = to_hex_character(0-(value % 16)); + } + while ((value /= 16) && (p < last)); + } + else + { + + do + { + *p++ = to_hex_character(value % 16); + } + while ((value /= 16) && (p < last)); + } + JSONCONS_ASSERT(p != last); + + std::size_t count = (p - buf); + if (is_negative) + { + result.push_back('-'); + ++count; + } + while (--p >= buf) + { + result.push_back(*p); + } + + return count; + } + + // write_double + + // fast exponent + template <class Result> + void fill_exponent(int K, Result& result) + { + if (K < 0) + { + result.push_back('-'); + K = -K; + } + else + { + result.push_back('+'); // compatibility with sprintf + } + + if (K < 10) + { + result.push_back('0'); // compatibility with sprintf + result.push_back((char)('0' + K)); + } + else if (K < 100) + { + result.push_back((char)('0' + K / 10)); K %= 10; + result.push_back((char)('0' + K)); + } + else if (K < 1000) + { + result.push_back((char)('0' + K / 100)); K %= 100; + result.push_back((char)('0' + K / 10)); K %= 10; + result.push_back((char)('0' + K)); + } + else + { + jsoncons::detail::from_integer(K, result); + } + } + + template <class Result> + void prettify_string(const char *buffer, std::size_t length, int k, int min_exp, int max_exp, Result& result) + { + int nb_digits = (int)length; + int offset; + /* v = buffer * 10^k + kk is such that 10^(kk-1) <= v < 10^kk + this way kk gives the position of the decimal point. + */ + int kk = nb_digits + k; + + if (nb_digits <= kk && kk <= max_exp) + { + /* the first digits are already in. Add some 0s and call it a day. */ + /* the max_exp is a personal choice. Only 16 digits could possibly be relevant. + * Basically we want to print 12340000000 rather than 1234.0e7 or 1.234e10 */ + for (int i = 0; i < nb_digits; ++i) + { + result.push_back(buffer[i]); + } + for (int i = nb_digits; i < kk; ++i) + { + result.push_back('0'); + } + result.push_back('.'); + result.push_back('0'); + } + else if (0 < kk && kk <= max_exp) + { + /* comma number. Just insert a '.' at the correct location. */ + for (int i = 0; i < kk; ++i) + { + result.push_back(buffer[i]); + } + result.push_back('.'); + for (int i = kk; i < nb_digits; ++i) + { + result.push_back(buffer[i]); + } + } + else if (min_exp < kk && kk <= 0) + { + offset = 2 - kk; + + result.push_back('0'); + result.push_back('.'); + for (int i = 2; i < offset; ++i) + result.push_back('0'); + for (int i = 0; i < nb_digits; ++i) + { + result.push_back(buffer[i]); + } + } + else if (nb_digits == 1) + { + result.push_back(buffer[0]); + result.push_back('e'); + fill_exponent(kk - 1, result); + } + else + { + result.push_back(buffer[0]); + result.push_back('.'); + for (int i = 1; i < nb_digits; ++i) + { + result.push_back(buffer[i]); + } + result.push_back('e'); + fill_exponent(kk - 1, result); + } + } + + template<class Result> + void dump_buffer(const char *buffer, std::size_t length, char decimal_point, Result& result) + { + const char *sbeg = buffer; + const char *send = sbeg + length; + + if (sbeg != send) + { + bool needs_dot = true; + for (const char* q = sbeg; q < send; ++q) + { + switch (*q) + { + case '-': + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + case '+': + result.push_back(*q); + break; + case 'e': + case 'E': + result.push_back('e'); + needs_dot = false; + break; + default: + if (*q == decimal_point) + { + needs_dot = false; + result.push_back('.'); + } + break; + } + } + if (needs_dot) + { + result.push_back('.'); + result.push_back('0'); + needs_dot = true; + } + } + } + + template<class Result> + bool dtoa_scientific(double val, char decimal_point, Result& result) + { + if (val == 0) + { + result.push_back('0'); + result.push_back('.'); + result.push_back('0'); + return true; + } + + jsoncons::detail::chars_to to_double_; + + char buffer[100]; + int precision = std::numeric_limits<double>::digits10; + int length = snprintf(buffer, sizeof(buffer), "%1.*e", precision, val); + if (length < 0) + { + return false; + } + if (to_double_(buffer, sizeof(buffer)) != val) + { + const int precision2 = std::numeric_limits<double>::max_digits10; + length = snprintf(buffer, sizeof(buffer), "%1.*e", precision2, val); + if (length < 0) + { + return false; + } + } + dump_buffer(buffer, static_cast<std::size_t>(length), decimal_point, result); + return true; + } + + template<class Result> + bool dtoa_general(double val, char decimal_point, Result& result, std::false_type) + { + if (val == 0) + { + result.push_back('0'); + result.push_back('.'); + result.push_back('0'); + return true; + } + + jsoncons::detail::chars_to to_double_; + + char buffer[100]; + int precision = std::numeric_limits<double>::digits10; + int length = snprintf(buffer, sizeof(buffer), "%1.*g", precision, val); + if (length < 0) + { + return false; + } + if (to_double_(buffer, sizeof(buffer)) != val) + { + const int precision2 = std::numeric_limits<double>::max_digits10; + length = snprintf(buffer, sizeof(buffer), "%1.*g", precision2, val); + if (length < 0) + { + return false; + } + } + dump_buffer(buffer, length, decimal_point, result); + return true; + } + + template<class Result> + bool dtoa_general(double v, char decimal_point, Result& result, std::true_type) + { + if (v == 0) + { + result.push_back('0'); + result.push_back('.'); + result.push_back('0'); + return true; + } + + int length = 0; + int k; + + char buffer[100]; + + double u = std::signbit(v) ? -v : v; + if (jsoncons::detail::grisu3(u, buffer, &length, &k)) + { + if (std::signbit(v)) + { + result.push_back('-'); + } + // min exp: -4 is consistent with sprintf + // max exp: std::numeric_limits<double>::max_digits10 + jsoncons::detail::prettify_string(buffer, length, k, -4, std::numeric_limits<double>::max_digits10, result); + return true; + } + else + { + return dtoa_general(v, decimal_point, result, std::false_type()); + } + } + + template<class Result> + bool dtoa_fixed(double val, char decimal_point, Result& result, std::false_type) + { + if (val == 0) + { + result.push_back('0'); + result.push_back('.'); + result.push_back('0'); + return true; + } + + jsoncons::detail::chars_to to_double_; + + char buffer[100]; + int precision = std::numeric_limits<double>::digits10; + int length = snprintf(buffer, sizeof(buffer), "%1.*f", precision, val); + if (length < 0) + { + return false; + } + if (to_double_(buffer, sizeof(buffer)) != val) + { + const int precision2 = std::numeric_limits<double>::max_digits10; + length = snprintf(buffer, sizeof(buffer), "%1.*f", precision2, val); + if (length < 0) + { + return false; + } + } + dump_buffer(buffer, length, decimal_point, result); + return true; + } + + template<class Result> + bool dtoa_fixed(double v, char decimal_point, Result& result, std::true_type) + { + if (v == 0) + { + result.push_back('0'); + result.push_back('.'); + result.push_back('0'); + return true; + } + + int length = 0; + int k; + + char buffer[100]; + + double u = std::signbit(v) ? -v : v; + if (jsoncons::detail::grisu3(u, buffer, &length, &k)) + { + if (std::signbit(v)) + { + result.push_back('-'); + } + jsoncons::detail::prettify_string(buffer, length, k, std::numeric_limits<int>::lowest(), (std::numeric_limits<int>::max)(), result); + return true; + } + else + { + return dtoa_fixed(v, decimal_point, result, std::false_type()); + } + } + + template<class Result> + bool dtoa_fixed(double v, char decimal_point, Result& result) + { + return dtoa_fixed(v, decimal_point, result, std::integral_constant<bool, std::numeric_limits<double>::is_iec559>()); + } + + template<class Result> + bool dtoa_general(double v, char decimal_point, Result& result) + { + return dtoa_general(v, decimal_point, result, std::integral_constant<bool, std::numeric_limits<double>::is_iec559>()); + } + + class write_double + { + private: + chars_to to_double_; + float_chars_format float_format_; + int precision_; + char decimal_point_; + public: + write_double(float_chars_format float_format, int precision) + : float_format_(float_format), precision_(precision), decimal_point_('.') + { + #if !defined(JSONCONS_NO_LOCALECONV) + struct lconv *lc = localeconv(); + if (lc != nullptr && lc->decimal_point[0] != 0) + { + decimal_point_ = lc->decimal_point[0]; + } + #endif + } + write_double(const write_double&) = default; + + write_double& operator=(const write_double&) = default; + + template<class Result> + std::size_t operator()(double val, Result& result) + { + std::size_t count = 0; + + char number_buffer[200]; + int length = 0; + + switch (float_format_) + { + case float_chars_format::fixed: + { + if (precision_ > 0) + { + length = snprintf(number_buffer, sizeof(number_buffer), "%1.*f", precision_, val); + if (length < 0) + { + JSONCONS_THROW(json_runtime_error<std::invalid_argument>("write_double failed.")); + } + dump_buffer(number_buffer, length, decimal_point_, result); + } + else + { + if (!dtoa_fixed(val, decimal_point_, result)) + { + JSONCONS_THROW(json_runtime_error<std::invalid_argument>("write_double failed.")); + } + } + } + break; + case float_chars_format::scientific: + { + if (precision_ > 0) + { + length = snprintf(number_buffer, sizeof(number_buffer), "%1.*e", precision_, val); + if (length < 0) + { + JSONCONS_THROW(json_runtime_error<std::invalid_argument>("write_double failed.")); + } + dump_buffer(number_buffer, length, decimal_point_, result); + } + else + { + if (!dtoa_scientific(val, decimal_point_, result)) + { + JSONCONS_THROW(json_runtime_error<std::invalid_argument>("write_double failed.")); + } + } + } + break; + case float_chars_format::general: + { + if (precision_ > 0) + { + length = snprintf(number_buffer, sizeof(number_buffer), "%1.*g", precision_, val); + if (length < 0) + { + JSONCONS_THROW(json_runtime_error<std::invalid_argument>("write_double failed.")); + } + dump_buffer(number_buffer, length, decimal_point_, result); + } + else + { + if (!dtoa_general(val, decimal_point_, result)) + { + JSONCONS_THROW(json_runtime_error<std::invalid_argument>("write_double failed.")); + } + } + break; + } + default: + JSONCONS_THROW(json_runtime_error<std::invalid_argument>("write_double failed.")); + break; + } + return count; + } + }; + +} // namespace detail +} // namespace jsoncons + +#endif |