| 1 |
#ifndef __PLINK2_STRING_H__ |
| 2 |
#define __PLINK2_STRING_H__ |
| 3 |
|
| 4 |
// This library is part of PLINK 2.0, copyright (C) 2005-2025 Shaun Purcell, |
| 5 |
// Christopher Chang. |
| 6 |
// |
| 7 |
// This program is free software: you can redistribute it and/or modify it |
| 8 |
// under the terms of the GNU Lesser General Public License as published by the |
| 9 |
// Free Software Foundation, either version 3 of the License, or (at your |
| 10 |
// option) any later version. |
| 11 |
// |
| 12 |
// This library is distributed in the hope that it will be useful, but WITHOUT |
| 13 |
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 14 |
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
| 15 |
// for more details. |
| 16 |
// |
| 17 |
// You should have received a copy of the GNU Lesser General Public License |
| 18 |
// along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
|
| 21 |
// Standalone string-printing and parsing functions which neither make |
| 22 |
// permanent memory allocations nor use g_bigstack for temporary allocations. |
| 23 |
|
| 24 |
#include "plink2_base.h" |
| 25 |
|
| 26 |
#include <math.h> // fabs(), isfinite() |
| 27 |
#include <stddef.h> // offsetof() |
| 28 |
|
| 29 |
#ifdef __cplusplus |
| 30 |
# include <algorithm> |
| 31 |
# if __cplusplus >= 201902L && defined(__GNUC__) && !defined(__clang__) |
| 32 |
# include <execution> |
| 33 |
# endif |
| 34 |
# ifdef _WIN32 |
| 35 |
// Windows C++11 <algorithm> resets these values :( |
| 36 |
# undef PRIu64 |
| 37 |
# undef PRId64 |
| 38 |
# define PRIu64 "I64u" |
| 39 |
# define PRId64 "I64d" |
| 40 |
# undef PRIuPTR |
| 41 |
# undef PRIdPTR |
| 42 |
# ifdef __LP64__ |
| 43 |
# define PRIuPTR PRIu64 |
| 44 |
# define PRIdPTR PRId64 |
| 45 |
# else |
| 46 |
# if __cplusplus < 201103L |
| 47 |
# define PRIuPTR "lu" |
| 48 |
# define PRIdPTR "ld" |
| 49 |
# else |
| 50 |
# define PRIuPTR "u" |
| 51 |
# define PRIdPTR "d" |
| 52 |
# endif |
| 53 |
# endif |
| 54 |
# endif |
| 55 |
#endif |
| 56 |
|
| 57 |
#ifdef _WIN32 |
| 58 |
# define EOLN_STR "\r\n" |
| 59 |
#else |
| 60 |
# define EOLN_STR "\n" |
| 61 |
#endif |
| 62 |
|
| 63 |
// generic maximum line byte length, currently also used as a default I/O |
| 64 |
// buffer size. .ped/.vcf/etc. lines can of course be longer. |
| 65 |
CONSTI32(kMaxMediumLine, 131072); |
| 66 |
|
| 67 |
// apparently these aren't always defined in limits.h |
| 68 |
#ifndef DBL_MAX |
| 69 |
# define DBL_MAX 1.7976931348623157e308 |
| 70 |
#endif |
| 71 |
#ifndef FLT_MAX |
| 72 |
# define FLT_MAX S_CAST(float, 3.40282347e38) |
| 73 |
#endif |
| 74 |
|
| 75 |
// These are needed by plink2_stats. May want to define these elsewhere, but |
| 76 |
// they can't live in plink2_cmdline any more. |
| 77 |
static const double kE = 2.7182818284590452; |
| 78 |
static const double kPi = 3.1415926535897932; |
| 79 |
static const double kSqrt2 = 1.4142135623730951; |
| 80 |
static const double kRecipE = 0.36787944117144233; |
| 81 |
static const double kRecip2m53 = 0.00000000000000011102230246251565404236316680908203125; |
| 82 |
|
| 83 |
// floating point comparison-to-nonzero tolerance, currently 2^{-30} |
| 84 |
static const double kEpsilon = 0.000000000931322574615478515625; |
| 85 |
// less tolerant versions (2^{-35}, 2^{-44}) for some exact calculations |
| 86 |
static const double kSmallEpsilon = 0.00000000000005684341886080801486968994140625; |
| 87 |
|
| 88 |
// 2^{-21}, must be >= sqrt(kSmallEpsilon) |
| 89 |
static const double kBigEpsilon = 0.000000476837158203125; |
| 90 |
|
| 91 |
// 2^{-83} bias to give exact tests maximum ability to determine tiny p-values. |
| 92 |
// (~2^{-53} is necessary to take advantage of denormalized small numbers, then |
| 93 |
// allow tail sum to be up to 2^30.) |
| 94 |
static const double kExactTestBias = 0.00000000000000000000000010339757656912845935892608650874535669572651386260986328125; |
| 95 |
|
| 96 |
#ifdef __cplusplus |
| 97 |
# define STD_SORT(ct, fallback_cmp, arr) std::sort(&((arr)[0]), (&((arr)[ct]))) |
| 98 |
# if __cplusplus >= 201902L && defined(__GNUC__) && !defined(__clang__) |
| 99 |
// this should only be used for arrays of length >= variant_ct or sample_ct |
| 100 |
// (sample_ct is cutting it close). |
| 101 |
// macro should still be used in e.g. non-__cplusplus blocks, so that we have |
| 102 |
// the option of falling back on a hand-coded parallel sort. |
| 103 |
# define STD_SORT_PAR_UNSEQ(ct, fallback_cmp, arr) std::sort(std::execution::par_unseq, &((arr)[0]), (&((arr)[ct]))) |
| 104 |
# else |
| 105 |
# define STD_SORT_PAR_UNSEQ(ct, fallback_cmp, arr) std::sort(&((arr)[0]), (&((arr)[ct]))) |
| 106 |
# endif |
| 107 |
#else |
| 108 |
# define STD_SORT(ct, fallback_cmp, arr) qsort((arr), (ct), sizeof(*(arr)), (fallback_cmp)) |
| 109 |
# define STD_SORT_PAR_UNSEQ(ct, fallback_cmp, arr) qsort((arr), (ct), sizeof(*(arr)), (fallback_cmp)) |
| 110 |
#endif |
| 111 |
|
| 112 |
#ifdef __cplusplus |
| 113 |
namespace plink2 { |
| 114 |
#endif |
| 115 |
|
| 116 |
// A bunch of library functions operating on char*s don't modify the buffer |
| 117 |
// themselves, but return a char* which should inherit the constness of the |
| 118 |
// input parameter. We want them to |
| 119 |
// 1. check const-correctness when this is compiled as C++ |
| 120 |
// 2. still be valid C99 |
| 121 |
// and the method of achieving this should be minimally bug-prone. |
| 122 |
// |
| 123 |
// Current hack: |
| 124 |
// 1. First declare the const char*-accepting version, with return type of |
| 125 |
// CXXCONST_CP. Make all return statements include a C-style cast, even |
| 126 |
// when not strictly necessary (e.g. relaying the return value of another |
| 127 |
// function of this sort), unless nullptr is being returned. |
| 128 |
// 2. Then declare the char*-accepting version in an immediately following |
| 129 |
// #ifdef __cplusplus block, which is simply a wrapper that uses |
| 130 |
// const_cast twice. |
| 131 |
// This is kind of ugly, so I may not use this approach in non-library source |
| 132 |
// code. |
| 133 |
// |
| 134 |
// There are related issues with const char** and const char* const*. |
| 135 |
// Unfortunately, while C99 implicitly converts char* to const char*, it does |
| 136 |
// not do char** -> const char* const*, so caller-side casts are required. |
| 137 |
// (And char** -> const char** implicit conversion doesn't happen in C++ either |
| 138 |
// for good reason, see http://c-faq.com/ansi/constmismatch.html .) The 'good' |
| 139 |
// news is that this removes the need for duplicate C++ function prototypes, |
| 140 |
// but it's generally an even worse situation than the single-indirection case; |
| 141 |
// these macro names are intentionally verbose to encourage assignment to the |
| 142 |
// appropriate-qualified type as soon as possible. |
| 143 |
#ifdef __cplusplus |
| 144 |
# define CXXCONST_CP const char* |
| 145 |
# define CXXCONST_VOIDP const void* |
| 146 |
# define TO_CONSTCPCONSTP(char_pp) (char_pp) |
| 147 |
|
| 148 |
HEADER_INLINE const char* DowncastToXC(const void* pp) { |
| 149 |
return S_CAST(const char*, pp); |
| 150 |
} |
| 151 |
#else |
| 152 |
# define CXXCONST_CP char* |
| 153 |
# define CXXCONST_VOIDP void* |
| 154 |
# define TO_CONSTCPCONSTP(char_pp) ((const char* const*)(char_pp)) |
| 155 |
|
| 156 |
HEADER_INLINE char* DowncastToXC(const void* pp) { |
| 157 |
return R_CAST(char*, pp); |
| 158 |
} |
| 159 |
#endif |
| 160 |
|
| 161 |
#ifdef _GNU_SOURCE |
| 162 |
// There was some recent (2016) discussion on the gcc mailing list on strlen() |
| 163 |
// vs. rawmemchr(., 0), where it was claimed that rawmemchr(., 0) could be |
| 164 |
// compiled to something slower than &(.[strlen(.)]), rather than being at |
| 165 |
// least as good. However, this didn't happen when I tried to benchmark this, |
| 166 |
// so I'll stick to the function that returns the right type (except when |
| 167 |
// rawmemchr itself has to be emulated). |
| 168 |
HEADER_INLINE CXXCONST_CP strnul(const char* str) { |
| 169 |
return S_CAST(CXXCONST_CP, rawmemchr(str, 0)); |
| 170 |
} |
| 171 |
|
| 172 |
# ifdef __cplusplus |
| 173 |
HEADER_INLINE char* strnul(char* str) { |
| 174 |
return const_cast<char*>(strnul(const_cast<const char*>(str))); |
| 175 |
} |
| 176 |
# endif |
| 177 |
|
| 178 |
#else // !_GNU_SOURCE |
| 179 |
|
| 180 |
# ifdef __LP64__ |
| 181 |
CXXCONST_VOIDP rawmemchr(const void* ss, int cc); |
| 182 |
|
| 183 |
HEADER_INLINE CXXCONST_CP strnul(const char* str) { |
| 184 |
return S_CAST(CXXCONST_CP, rawmemchr(str, 0)); |
| 185 |
} |
| 186 |
# else // !_GNU_SOURCE, !__LP64__ |
| 187 |
HEADER_INLINE CXXCONST_VOIDP rawmemchr(const void* ss, int cc) { |
| 188 |
return S_CAST(CXXCONST_VOIDP, memchr(ss, cc, 0x80000000U - kBytesPerVec)); |
| 189 |
} |
| 190 |
|
| 191 |
HEADER_INLINE CXXCONST_CP strnul(const char* str) { |
| 192 |
return S_CAST(CXXCONST_CP, &(str[strlen(str)])); |
| 193 |
} |
| 194 |
# endif |
| 195 |
|
| 196 |
# ifdef __cplusplus |
| 197 |
HEADER_INLINE void* rawmemchr(void* ss, int cc) { |
| 198 |
return const_cast<void*>(rawmemchr(const_cast<const void*>(ss), cc)); |
| 199 |
} |
| 200 |
|
| 201 |
HEADER_INLINE char* strnul(char* str) { |
| 202 |
return const_cast<char*>(strnul(const_cast<const char*>(str))); |
| 203 |
} |
| 204 |
# endif |
| 205 |
|
| 206 |
#endif // !_GNU_SOURCE |
| 207 |
|
| 208 |
// See also AdvToDelimOrEnd later below, which is an obvious alternative memchr |
| 209 |
// interface. |
| 210 |
|
| 211 |
// ReadLineStream emits lines which are *not* null-terminated, but are |
| 212 |
// guaranteed to have trailing '\n's. |
| 213 |
CXXCONST_VOIDP rawmemchr2(const void* ss, unsigned char ucc1, unsigned char ucc2); |
| 214 |
|
| 215 |
HEADER_INLINE CXXCONST_CP strchrnul_n(const char* ss, unsigned char ucc1) { |
| 216 |
return S_CAST(CXXCONST_CP, rawmemchr2(ss, ucc1, '\n')); |
| 217 |
} |
| 218 |
|
| 219 |
CXXCONST_VOIDP rawmemchr3(const void* ss, unsigned char ucc1, unsigned char ucc2, unsigned char ucc3); |
| 220 |
|
| 221 |
HEADER_INLINE CXXCONST_CP strchrnul2(const char* ss, unsigned char ucc1, unsigned char ucc2) { |
| 222 |
return S_CAST(CXXCONST_CP, rawmemchr3(ss, ucc1, ucc2, '\0')); |
| 223 |
} |
| 224 |
|
| 225 |
HEADER_INLINE CXXCONST_CP strchrnul2_n(const char* ss, unsigned char ucc1, unsigned char ucc2) { |
| 226 |
return S_CAST(CXXCONST_CP, rawmemchr3(ss, ucc1, ucc2, '\n')); |
| 227 |
} |
| 228 |
|
| 229 |
CXXCONST_CP strchrnul3(const char* ss, unsigned char ucc1, unsigned char ucc2, unsigned char ucc3); |
| 230 |
|
| 231 |
#ifdef __cplusplus |
| 232 |
HEADER_INLINE void* rawmemchr2(void* ss, unsigned char ucc1, unsigned char ucc2) { |
| 233 |
return const_cast<void*>(rawmemchr2(const_cast<const void*>(ss), ucc1, ucc2)); |
| 234 |
} |
| 235 |
|
| 236 |
HEADER_INLINE char* strchrnul_n(char* ss, unsigned char ucc1) { |
| 237 |
return const_cast<char*>(strchrnul_n(const_cast<const char*>(ss), ucc1)); |
| 238 |
} |
| 239 |
|
| 240 |
HEADER_INLINE void* rawmemchr3(void* ss, unsigned char ucc1, unsigned char ucc2, unsigned char ucc3) { |
| 241 |
return const_cast<void*>(rawmemchr3(const_cast<const void*>(ss), ucc1, ucc2, ucc3)); |
| 242 |
} |
| 243 |
|
| 244 |
HEADER_INLINE char* strchrnul2(char* ss, unsigned char ucc1, unsigned char ucc2) { |
| 245 |
return const_cast<char*>(strchrnul2(const_cast<const char*>(ss), ucc1, ucc2)); |
| 246 |
} |
| 247 |
|
| 248 |
HEADER_INLINE char* strchrnul2_n(char* ss, unsigned char ucc1, unsigned char ucc2) { |
| 249 |
return const_cast<char*>(strchrnul2_n(const_cast<const char*>(ss), ucc1, ucc2)); |
| 250 |
} |
| 251 |
|
| 252 |
HEADER_INLINE char* strchrnul3(char* ss, unsigned char ucc1, unsigned char ucc2, unsigned char ucc3) { |
| 253 |
return const_cast<char*>(strchrnul3(const_cast<const char*>(ss), ucc1, ucc2, ucc3)); |
| 254 |
} |
| 255 |
#endif |
| 256 |
|
| 257 |
#ifndef _GNU_SOURCE |
| 258 |
# ifdef __LP64__ |
| 259 |
HEADER_INLINE CXXCONST_CP strchrnul(const char* str, int needle) { |
| 260 |
return S_CAST(CXXCONST_CP, rawmemchr2(str, 0, needle)); |
| 261 |
} |
| 262 |
# else |
| 263 |
HEADER_INLINE CXXCONST_CP strchrnul(const char* str, int cc) { |
| 264 |
const char* strchr_result = strchr(str, cc); |
| 265 |
if (strchr_result) { |
| 266 |
return S_CAST(CXXCONST_CP, strchr_result); |
| 267 |
} |
| 268 |
return S_CAST(CXXCONST_CP, strnul(str)); |
| 269 |
} |
| 270 |
# endif |
| 271 |
|
| 272 |
# ifdef __cplusplus |
| 273 |
HEADER_INLINE char* strchrnul(char* ss, int needle) { |
| 274 |
return const_cast<char*>(strchrnul(const_cast<const char*>(ss), needle)); |
| 275 |
} |
| 276 |
# endif |
| 277 |
#endif |
| 278 |
|
| 279 |
// These return 1 at eoln. |
| 280 |
HEADER_INLINE uint32_t strchrnul_n_mov(unsigned char ucc1, const char** ss_ptr) { |
| 281 |
const char* ss_next = strchrnul_n(*ss_ptr, ucc1); |
| 282 |
*ss_ptr = ss_next; |
| 283 |
return (*ss_next != ucc1); |
| 284 |
} |
| 285 |
|
| 286 |
HEADER_INLINE uint32_t incr_strchrnul_n_mov(unsigned char ucc1, const char** ss_ptr) { |
| 287 |
const char* ss_next = strchrnul_n(&((*ss_ptr)[1]), ucc1); |
| 288 |
*ss_ptr = ss_next; |
| 289 |
return (*ss_next != ucc1); |
| 290 |
} |
| 291 |
|
| 292 |
// input for WordWrap should have no intermediate '\n's. If suffix_len is 0, |
| 293 |
// there should be a terminating \n. |
| 294 |
void WordWrap(uint32_t suffix_len, char* strbuf); |
| 295 |
|
| 296 |
// input: null-terminated string, could have multiple lines, must have at least |
| 297 |
// ~80 bytes available past the end |
| 298 |
// output: WordWrap() called on each line, \n appended to last line if not |
| 299 |
// there |
| 300 |
void WordWrapMultiline(char* strbuf); |
| 301 |
|
| 302 |
uint32_t UintSlen(uint32_t num); |
| 303 |
|
| 304 |
HEADER_INLINE uint32_t IntSlen(int32_t num) { |
| 305 |
// see abs_i32() |
| 306 |
const uint32_t neg_sign_bit = -(S_CAST(uint32_t, num) >> 31); |
| 307 |
return UintSlen((S_CAST(uint32_t, num) ^ neg_sign_bit) - neg_sign_bit) - neg_sign_bit; |
| 308 |
} |
| 309 |
|
| 310 |
// memcpya(), memseta() defined in plink2_base.h |
| 311 |
|
| 312 |
HEADER_INLINE char* memcpyax(void* __restrict dst, const void* __restrict src, uint32_t ct, char extra_char) { |
| 313 |
memcpy(dst, src, ct); |
| 314 |
S_CAST(char*, dst)[ct] = extra_char; |
| 315 |
return &(S_CAST(char*, dst)[ct + 1]); |
| 316 |
} |
| 317 |
|
| 318 |
HEADER_INLINE void memcpyx(void* __restrict dst, const void* __restrict src, uint32_t ct, char extra_char) { |
| 319 |
memcpy(dst, src, ct); |
| 320 |
S_CAST(char*, dst)[ct] = extra_char; |
| 321 |
} |
| 322 |
|
| 323 |
HEADER_INLINE char* strcpyax(char* __restrict dst, const void* __restrict src, char extra_char) { |
| 324 |
const uintptr_t slen = strlen(S_CAST(const char*, src)); |
| 325 |
memcpy(dst, src, slen); |
| 326 |
dst[slen] = extra_char; |
| 327 |
return &(dst[slen + 1]); |
| 328 |
} |
| 329 |
|
| 330 |
// MinGW support for stpcpy is a mess, so I'll use a capitalized name to route |
| 331 |
// around the problem. |
| 332 |
#if defined(_GNU_SOURCE) || defined(__APPLE__) || (_POSIX_C_SOURCE >= 200809L) |
| 333 |
HEADER_INLINE char* Stpcpy(char* __restrict dst, const char* __restrict src) { |
| 334 |
return stpcpy(dst, src); |
| 335 |
} |
| 336 |
#else |
| 337 |
HEADER_INLINE char* Stpcpy(char* __restrict dst, const char* __restrict src) { |
| 338 |
uintptr_t slen = strlen(src); |
| 339 |
memcpy(dst, src, slen + 1); |
| 340 |
return &(dst[slen]); |
| 341 |
} |
| 342 |
#endif |
| 343 |
|
| 344 |
#if defined(__LP64__) && (__cplusplus >= 201103L) |
| 345 |
template <uint32_t N> char* MemcpyaxK(void* __restrict dst, const void* __restrict src, char extra_char) { |
| 346 |
memcpyo_k(dst, src, N); |
| 347 |
S_CAST(char*, dst)[N] = extra_char; |
| 348 |
return &(S_CAST(char*, dst)[N + 1]); |
| 349 |
} |
| 350 |
|
| 351 |
# define memcpyax_k(dst, src, ct, extra_char) plink2::MemcpyaxK<ct>(dst, src, extra_char) |
| 352 |
|
| 353 |
template <uint32_t N> int32_t StrequalK(const char* s1, const char* k_s2, uint32_t s1_slen) { |
| 354 |
return (s1_slen == N) && memequal_k(s1, k_s2, N); |
| 355 |
}; |
| 356 |
|
| 357 |
// strcpya, CompileTimeSlen, strcpy_k, strcpya_k, u32toa, i64toa, wtoa moved to |
| 358 |
// plink2_base |
| 359 |
|
| 360 |
// can also use sizeof(k_s2) - 1, but that's less safe |
| 361 |
# define strequal_k(s1, k_s2, s1_slen) plink2::StrequalK<plink2::CompileTimeSlen(k_s2)>(s1, k_s2, s1_slen) |
| 362 |
|
| 363 |
# define strequal_k_unsafe(s1, k_s2) memequal_k(s1, k_s2, 1 + plink2::CompileTimeSlen(k_s2)) |
| 364 |
|
| 365 |
# define memequal_sk(s1, k_s2) memequal_k(s1, k_s2, plink2::CompileTimeSlen(k_s2)) |
| 366 |
|
| 367 |
#else // !(defined(__LP64__) && (__cplusplus >= 201103L)) |
| 368 |
HEADER_INLINE char* memcpyax_k(void* __restrict dst, const void* __restrict src, uint32_t ct, char extra_char) { |
| 369 |
return memcpyax(dst, src, ct, extra_char); |
| 370 |
} |
| 371 |
|
| 372 |
HEADER_INLINE int32_t strequal_k(const char* s1, const char* k_s2, uint32_t s1_slen) { |
| 373 |
// any sane compiler should compute s2_slen at compile-time if k_s2 is a |
| 374 |
// constant string |
| 375 |
const uint32_t s2_slen = strlen(k_s2); |
| 376 |
return (s1_slen == s2_slen) && memequal(s1, k_s2, s2_slen); |
| 377 |
} |
| 378 |
|
| 379 |
// Can use this when it's always safe to read first (1 + strlen(k_s2)) bytes of |
| 380 |
// s1. |
| 381 |
HEADER_INLINE int32_t strequal_k_unsafe(const char* s1, const char* k_s2) { |
| 382 |
const uint32_t s2_blen = 1 + strlen(k_s2); |
| 383 |
return memequal(s1, k_s2, s2_blen); |
| 384 |
} |
| 385 |
|
| 386 |
HEADER_INLINE int32_t memequal_sk(const void* s1, const char* k_s2) { |
| 387 |
const uint32_t s2_slen = strlen(k_s2); |
| 388 |
return memequal(s1, k_s2, s2_slen); |
| 389 |
} |
| 390 |
#endif |
| 391 |
|
| 392 |
// Must be safe to read either first slen bytes of unknown_len_str, or at least |
| 393 |
// a cacheline past the first mismatch. |
| 394 |
// Note that it's better to call memequal(unknown_len_str, known_len_tok, slen |
| 395 |
// + 1) when known_len_tok is null-terminated. |
| 396 |
HEADER_INLINE int32_t strequal_unsafe(const char* unknown_len_str, const char* known_len_tok, uint32_t slen) { |
| 397 |
return memequal(unknown_len_str, known_len_tok, slen) && (!unknown_len_str[slen]); |
| 398 |
} |
| 399 |
|
| 400 |
#if defined(__cplusplus) |
| 401 |
# if __cplusplus >= 201103L |
| 402 |
HEADER_INLINE bool isfinite_f(float fxx) { |
| 403 |
using namespace std; |
| 404 |
return isfinite(fxx); |
| 405 |
} |
| 406 |
|
| 407 |
HEADER_INLINE bool isfinite_d(double dxx) { |
| 408 |
using namespace std; |
| 409 |
return isfinite(dxx); |
| 410 |
} |
| 411 |
# else |
| 412 |
# ifdef isfinite |
| 413 |
# define isfinite_f isfinite |
| 414 |
# define isfinite_d isfinite |
| 415 |
# else |
| 416 |
HEADER_INLINE bool isfinite_f(float fxx) { |
| 417 |
return (fxx == fxx) && (fxx != INFINITY) && (fxx != -INFINITY); |
| 418 |
} |
| 419 |
|
| 420 |
HEADER_INLINE bool isfinite_d(double dxx) { |
| 421 |
return (dxx == dxx) && (dxx != S_CAST(double, INFINITY)) && (dxx != S_CAST(double, -INFINITY)); |
| 422 |
} |
| 423 |
# endif |
| 424 |
# endif |
| 425 |
#else |
| 426 |
# define isfinite_f isfinite |
| 427 |
# define isfinite_d isfinite |
| 428 |
#endif |
| 429 |
|
| 430 |
HEADER_INLINE int32_t IsSpaceOrEoln(unsigned char ucc) { |
| 431 |
return (ucc <= 32); |
| 432 |
} |
| 433 |
|
| 434 |
HEADER_INLINE uint32_t IsCommaOrTspaceTokenEnd(unsigned char ucc, uint32_t comma_delim) { |
| 435 |
if (comma_delim) { |
| 436 |
return (ucc < 32) || (ucc == ','); |
| 437 |
} else { |
| 438 |
return (ucc <= 32); |
| 439 |
} |
| 440 |
} |
| 441 |
|
| 442 |
// Assumes it's safe to read first 1 + strlen(s_const) bytes of s_read, i.e. |
| 443 |
// this is ALWAYS 'unsafe'. |
| 444 |
// Differs from strequal_k_unsafe() since strings are not considered equal when |
| 445 |
// s_read[strlen(s_const)] isn't a token-ender. |
| 446 |
HEADER_INLINE int32_t tokequal_k(const char* s_read, const char* s_const) { |
| 447 |
const uint32_t s_const_slen = strlen(s_const); |
| 448 |
return memequal(s_read, s_const, s_const_slen) && IsSpaceOrEoln(s_read[s_const_slen]); |
| 449 |
} |
| 450 |
|
| 451 |
// s_prefix must be strictly contained. |
| 452 |
HEADER_INLINE int32_t StrStartsWith(const char* s_read, const char* s_prefix_const, uint32_t s_read_slen) { |
| 453 |
const uint32_t s_const_slen = strlen(s_prefix_const); |
| 454 |
return (s_read_slen > s_const_slen) && memequal(s_read, s_prefix_const, s_const_slen); |
| 455 |
} |
| 456 |
|
| 457 |
// permits s_read and s_prefix to be equal. |
| 458 |
HEADER_INLINE int32_t StrStartsWith0(const char* s_read, const char* s_prefix_const, uint32_t s_read_slen) { |
| 459 |
const uint32_t s_const_slen = strlen(s_prefix_const); |
| 460 |
return (s_read_slen >= s_const_slen) && memequal(s_read, s_prefix_const, s_const_slen); |
| 461 |
} |
| 462 |
|
| 463 |
// Can use this when it's always safe to read first strlen(s_prefix_const) |
| 464 |
// bytes of s_read. |
| 465 |
HEADER_INLINE int32_t StrStartsWithUnsafe(const char* s_read, const char* s_prefix_const) { |
| 466 |
const uint32_t s_const_slen = strlen(s_prefix_const); |
| 467 |
return memequal(s_read, s_prefix_const, s_const_slen); |
| 468 |
} |
| 469 |
|
| 470 |
// s_suffix must be strictly contained. |
| 471 |
HEADER_INLINE int32_t StrEndsWith(const char* s_read, const char* s_suffix_const, uint32_t s_read_slen) { |
| 472 |
const uint32_t s_const_slen = strlen(s_suffix_const); |
| 473 |
return (s_read_slen > s_const_slen) && memequal(&(s_read[s_read_slen - s_const_slen]), s_suffix_const, s_const_slen); |
| 474 |
} |
| 475 |
|
| 476 |
// May read (kBytesPerWord - 1) bytes past the end of each string. |
| 477 |
HEADER_INLINE int32_t strequal_overread(const char* s1, const char* s2) { |
| 478 |
#ifndef NO_UNALIGNED |
| 479 |
const uintptr_t* s1_alias = R_CAST(const uintptr_t*, s1); |
| 480 |
const uintptr_t* s2_alias = R_CAST(const uintptr_t*, s2); |
| 481 |
for (uintptr_t widx = 0; ; ++widx) { |
| 482 |
const uintptr_t w1 = s1_alias[widx]; |
| 483 |
const uintptr_t zcheck = DetectFirstZeroByte(w1); |
| 484 |
const uintptr_t w2 = s2_alias[widx]; |
| 485 |
const uintptr_t xor_word = w1 ^ w2; |
| 486 |
if (zcheck) { |
| 487 |
// Mask out bytes past the known null. |
| 488 |
const uintptr_t mask = zcheck ^ (zcheck - 1); |
| 489 |
return (xor_word & mask)? 0 : 1; |
| 490 |
} |
| 491 |
if (xor_word) { |
| 492 |
return 0; |
| 493 |
} |
| 494 |
} |
| 495 |
#else |
| 496 |
return (strcmp(s1, s2) == 0); |
| 497 |
#endif |
| 498 |
} |
| 499 |
|
| 500 |
int32_t strcmp_overread(const char* s1, const char* s2); |
| 501 |
|
| 502 |
// Support for sorting arrays of strings, represented either as an array of |
| 503 |
// const char*s, or a single [# of strings] x [max byte width] array of chars |
| 504 |
// (suitable for old-school qsort(), referred to as a 'strbox' here). |
| 505 |
// The SortStrboxIndexed functions automatically construct an array of const |
| 506 |
// char*s and sort that when the max byte width is large. |
| 507 |
int32_t strcmp_casted(const void* s1, const void* s2); |
| 508 |
|
| 509 |
int32_t strcmp_overread_casted(const void* s1, const void* s2); |
| 510 |
|
| 511 |
|
| 512 |
int32_t strcmp_natural(const void* s1, const void* s2); |
| 513 |
|
| 514 |
|
| 515 |
int32_t strcmp_deref(const void* s1, const void* s2); |
| 516 |
|
| 517 |
int32_t strcmp_overread_deref(const void* s1, const void* s2); |
| 518 |
|
| 519 |
int32_t strcmp_natural_deref(const void* s1, const void* s2); |
| 520 |
|
| 521 |
|
| 522 |
int32_t strcmp_natural_uncasted(const char* s1, const char* s2); |
| 523 |
|
| 524 |
#ifdef __cplusplus |
| 525 |
typedef struct Strbuf28UiStruct { |
| 526 |
char strbuf[28]; |
| 527 |
uint32_t orig_idx; |
| 528 |
bool operator<(const struct Strbuf28UiStruct& rhs) const { |
| 529 |
return (strcmp_natural_uncasted(strbuf, rhs.strbuf) < 0); |
| 530 |
} |
| 531 |
} Strbuf28Ui; |
| 532 |
|
| 533 |
typedef struct Strbuf60UiStruct { |
| 534 |
char strbuf[60]; |
| 535 |
uint32_t orig_idx; |
| 536 |
bool operator<(const struct Strbuf60UiStruct& rhs) const { |
| 537 |
return (strcmp_natural_uncasted(strbuf, rhs.strbuf) < 0); |
| 538 |
} |
| 539 |
} Strbuf60Ui; |
| 540 |
#endif |
| 541 |
|
| 542 |
uintptr_t GetStrboxsortWentryBlen(uintptr_t max_str_blen); |
| 543 |
|
| 544 |
#ifdef __cplusplus |
| 545 |
typedef struct StrSortDerefStruct { |
| 546 |
const char* strptr; |
| 547 |
|
| 548 |
bool operator<(const struct StrSortDerefStruct& rhs) const { |
| 549 |
return (strcmp(strptr, rhs.strptr) < 0); |
| 550 |
} |
| 551 |
} StrSortDeref; |
| 552 |
|
| 553 |
HEADER_INLINE bool strcmp_overread_lt(const char* s1, const char* s2) { |
| 554 |
# ifndef NO_UNALIGNED |
| 555 |
const uintptr_t* s1_alias = R_CAST(const uintptr_t*, s1); |
| 556 |
const uintptr_t* s2_alias = R_CAST(const uintptr_t*, s2); |
| 557 |
for (uintptr_t widx = 0; ; ++widx) { |
| 558 |
uintptr_t w1 = s1_alias[widx]; |
| 559 |
const uintptr_t zcheck = DetectFirstZeroByte(w1); |
| 560 |
uintptr_t w2 = s2_alias[widx]; |
| 561 |
if (zcheck) { |
| 562 |
// Mask out bytes past the known null. |
| 563 |
// Note that we can't safely include the garbage bytes past the null in |
| 564 |
// the comparison even if they aren't being changed, because they may be |
| 565 |
// uninitialized, and if they're also past a page boundary the OS may |
| 566 |
// return different values for consecutive queries on the same address! |
| 567 |
// See e.g. "CppCon 2016: Nicholas Ormrod 'The strange details of |
| 568 |
// std::string at Facebook'" |
| 569 |
// (https://www.youtube.com/watch?v=kPR8h4-qZdk ) starting at ~22:15. |
| 570 |
const uintptr_t mask = zcheck ^ (zcheck - 1); |
| 571 |
w1 &= mask; |
| 572 |
w2 &= mask; |
| 573 |
if (w1 == w2) { |
| 574 |
return false; |
| 575 |
} |
| 576 |
goto strcmp_overread_lt_finish; |
| 577 |
} |
| 578 |
if (w1 != w2) { |
| 579 |
strcmp_overread_lt_finish: |
| 580 |
# ifdef __LP64__ |
| 581 |
return __builtin_bswap64(w1) < __builtin_bswap64(w2); |
| 582 |
# else |
| 583 |
return __builtin_bswap32(w1) < __builtin_bswap32(w2); |
| 584 |
# endif |
| 585 |
} |
| 586 |
} |
| 587 |
# else // NO_UNALIGNED |
| 588 |
return strcmp(s1, s2) < 0; |
| 589 |
# endif |
| 590 |
} |
| 591 |
|
| 592 |
typedef struct StrSortDerefOverreadStruct { |
| 593 |
// Must be safe to read up to (kBytesPerWord - 1) bytes past the end of these |
| 594 |
// strings. Enough of a speed advantage to be worth using whenever possible, |
| 595 |
// though. |
| 596 |
const char* strptr; |
| 597 |
|
| 598 |
bool operator<(const struct StrSortDerefOverreadStruct& rhs) const { |
| 599 |
return strcmp_overread_lt(strptr, rhs.strptr); |
| 600 |
} |
| 601 |
} StrSortDerefOverread; |
| 602 |
|
| 603 |
typedef struct StrNsortDerefStruct { |
| 604 |
const char* strptr; |
| 605 |
bool operator<(const struct StrNsortDerefStruct& rhs) const { |
| 606 |
return (strcmp_natural(strptr, rhs.strptr) < 0); |
| 607 |
} |
| 608 |
} StrNsortDeref; |
| 609 |
|
| 610 |
typedef struct StrNsortIndexedDerefStruct { |
| 611 |
const char* strptr; |
| 612 |
uint32_t orig_idx; |
| 613 |
bool operator<(const struct StrNsortIndexedDerefStruct& rhs) const { |
| 614 |
return (strcmp_natural_uncasted(strptr, rhs.strptr) < 0); |
| 615 |
} |
| 616 |
} StrNsortIndexedDeref; |
| 617 |
|
| 618 |
HEADER_INLINE void StrptrArrSort(uintptr_t ct, const char** strptr_arr) { |
| 619 |
std::sort(R_CAST(StrSortDeref*, strptr_arr), &(R_CAST(StrSortDeref*, strptr_arr)[ct])); |
| 620 |
} |
| 621 |
|
| 622 |
HEADER_INLINE void StrptrArrSortOverread(uintptr_t ct, const char** strptr_arr) { |
| 623 |
std::sort(R_CAST(StrSortDerefOverread*, strptr_arr), &(R_CAST(StrSortDerefOverread*, strptr_arr)[ct])); |
| 624 |
} |
| 625 |
|
| 626 |
HEADER_INLINE void StrptrArrNsort(uintptr_t ct, const char** strptr_arr) { |
| 627 |
std::sort(R_CAST(StrNsortDeref*, strptr_arr), &(R_CAST(StrNsortDeref*, strptr_arr)[ct])); |
| 628 |
} |
| 629 |
|
| 630 |
// Considered adding stable-sort variants of StrptrArrSort, but looks like it's |
| 631 |
// usually better to define a more sophisticated comparator that removes the |
| 632 |
// need for the explicit stable-sort. (Note that std::stable_sort can throw |
| 633 |
// std::bad_alloc, etc.) |
| 634 |
|
| 635 |
// need to expose these for plink2_cmdline bigstack-allocating |
| 636 |
// SortStrboxIndexed()'s use |
| 637 |
void SortStrbox32bFinish(uintptr_t str_ct, uintptr_t max_str_blen, uint32_t use_nsort, Strbuf28Ui* filled_wkspace, char* sorted_strbox, uint32_t* id_map); |
| 638 |
|
| 639 |
void SortStrbox64bFinish(uintptr_t str_ct, uintptr_t max_str_blen, uint32_t use_nsort, Strbuf60Ui* filled_wkspace, char* sorted_strbox, uint32_t* id_map); |
| 640 |
|
| 641 |
// Must be ok to overread. |
| 642 |
void SortStrboxIndexed2(uintptr_t str_ct, uintptr_t max_str_blen, uint32_t use_nsort, char* strbox, uint32_t* id_map, void* sort_wkspace); |
| 643 |
#else // !__cplusplus |
| 644 |
HEADER_INLINE uint32_t strcmp_overread_lt(const char* s1, const char* s2) { |
| 645 |
return strcmp_overread(s1, s2) < 0; |
| 646 |
} |
| 647 |
|
| 648 |
HEADER_INLINE void StrptrArrSort(uintptr_t ct, const char** strptr_arr) { |
| 649 |
qsort(strptr_arr, ct, sizeof(intptr_t), strcmp_deref); |
| 650 |
} |
| 651 |
|
| 652 |
HEADER_INLINE void StrptrArrSortOverread(uintptr_t ct, const char** strptr_arr) { |
| 653 |
qsort(strptr_arr, ct, sizeof(intptr_t), strcmp_overread_deref); |
| 654 |
} |
| 655 |
|
| 656 |
HEADER_INLINE void StrptrArrNsort(uintptr_t ct, const char** strptr_arr) { |
| 657 |
qsort(strptr_arr, ct, sizeof(intptr_t), strcmp_natural_deref); |
| 658 |
} |
| 659 |
|
| 660 |
// Must be ok to overread. |
| 661 |
void SortStrboxIndexed2Fallback(uintptr_t str_ct, uintptr_t max_str_blen, uint32_t use_nsort, char* strbox, uint32_t* id_map, void* sort_wkspace); |
| 662 |
|
| 663 |
HEADER_INLINE void SortStrboxIndexed2(uintptr_t str_ct, uintptr_t max_str_blen, uint32_t use_nsort, char* strbox, uint32_t* id_map, void* sort_wkspace) { |
| 664 |
SortStrboxIndexed2Fallback(str_ct, max_str_blen, use_nsort, strbox, id_map, sort_wkspace); |
| 665 |
} |
| 666 |
#endif |
| 667 |
|
| 668 |
// Uses malloc instead of bigstack. |
| 669 |
// Must be ok to overread strbox. |
| 670 |
BoolErr SortStrboxIndexedMalloc(uintptr_t str_ct, uintptr_t max_str_blen, char* strbox, uint32_t* id_map); |
| 671 |
|
| 672 |
// Returns dedup'd strbox entry count. |
| 673 |
uint32_t CopyAndDedupSortedStrptrsToStrbox(const char* const* sorted_strptrs, uintptr_t str_ct, uintptr_t max_str_blen, char* strbox); |
| 674 |
|
| 675 |
// note that this can be expected to have size 16 bytes, not 12, on 64-bit |
| 676 |
// systems |
| 677 |
typedef struct StrSortIndexedDerefStruct { |
| 678 |
const char* strptr; |
| 679 |
uint32_t orig_idx; |
| 680 |
#ifdef __cplusplus |
| 681 |
bool operator<(const struct StrSortIndexedDerefStruct& rhs) const { |
| 682 |
return (strcmp(strptr, rhs.strptr) < 0); |
| 683 |
} |
| 684 |
#endif |
| 685 |
} StrSortIndexedDeref; |
| 686 |
|
| 687 |
typedef struct StrSortIndexedDerefOverreadStruct { |
| 688 |
// must be safe to read up to (kBytesPerWord - 1) bytes past the end of these |
| 689 |
// strings. |
| 690 |
const char* strptr; |
| 691 |
|
| 692 |
uint32_t orig_idx; |
| 693 |
#ifdef __cplusplus |
| 694 |
bool operator<(const struct StrSortIndexedDerefOverreadStruct& rhs) const { |
| 695 |
return strcmp_overread_lt(strptr, rhs.strptr); |
| 696 |
} |
| 697 |
#endif |
| 698 |
} StrSortIndexedDerefOverread; |
| 699 |
|
| 700 |
void StrptrArrSortMain(uintptr_t str_ct, uint32_t overread_ok, uint32_t use_nsort, StrSortIndexedDeref* wkspace_alias); |
| 701 |
|
| 702 |
void SortStrptrArrIndexed2(uint32_t str_ct, uint32_t leave_first_alone, uint32_t overread_ok, uint32_t use_nsort, const char** strptrs, uint32_t* new_to_old_idx, uint32_t* old_to_new_idx, void* wkspace); |
| 703 |
|
| 704 |
HEADER_INLINE int32_t IsLetter(unsigned char ucc) { |
| 705 |
return (((ucc & 192) == 64) && (((ucc - 1) & 31) < 26)); |
| 706 |
} |
| 707 |
|
| 708 |
// if we need the digit value, better to use (unsigned char)cc - '0'... |
| 709 |
HEADER_INLINE int32_t IsDigit(unsigned char ucc) { |
| 710 |
return (ucc <= '9') && (ucc >= '0'); |
| 711 |
} |
| 712 |
|
| 713 |
HEADER_INLINE int32_t IsNotDigit(unsigned char ucc) { |
| 714 |
return (ucc > '9') || (ucc < '0'); |
| 715 |
} |
| 716 |
|
| 717 |
HEADER_INLINE int32_t IsNotNzdigit(unsigned char ucc) { |
| 718 |
return (ucc > '9') || (ucc <= '0'); |
| 719 |
} |
| 720 |
|
| 721 |
// May as well treat all chars < 32, except tab, as eoln... |
| 722 |
HEADER_INLINE int32_t IsEoln(unsigned char ucc) { |
| 723 |
return (ucc < 32) && (ucc != 9); |
| 724 |
} |
| 725 |
|
| 726 |
// kns = "known non-space" (where tab counts as a space) |
| 727 |
// This is of course identical to IsSpaceOrEoln(), but intent should be |
| 728 |
// clearer and we can insert a debug-assert that we aren't at a space/tab. |
| 729 |
HEADER_INLINE int32_t IsEolnKns(unsigned char ucc) { |
| 730 |
// could assert ucc is not a space/tab? |
| 731 |
return (ucc <= 32); |
| 732 |
} |
| 733 |
|
| 734 |
HEADER_INLINE int32_t IsEolnOrCommentKns(unsigned char ucc) { |
| 735 |
return (ucc < 32) || (ucc == '#'); |
| 736 |
} |
| 737 |
|
| 738 |
HEADER_INLINE int32_t NoMoreTokensKns(const char* str) { |
| 739 |
return ((!str) || IsEolnKns(*str)); |
| 740 |
} |
| 741 |
|
| 742 |
HEADER_INLINE CXXCONST_CP FirstNonChar(const char* str_iter, char cc) { |
| 743 |
while (*str_iter == cc) { |
| 744 |
++str_iter; |
| 745 |
} |
| 746 |
return S_CAST(CXXCONST_CP, str_iter); |
| 747 |
} |
| 748 |
|
| 749 |
#ifdef __cplusplus |
| 750 |
HEADER_INLINE char* FirstNonChar(char* str_iter, char cc) { |
| 751 |
return const_cast<char*>(FirstNonChar(const_cast<const char*>(str_iter), cc)); |
| 752 |
} |
| 753 |
#endif |
| 754 |
|
| 755 |
HEADER_INLINE CXXCONST_CP FirstNonTspace(const char* str_iter) { |
| 756 |
while ((*str_iter == ' ') || (*str_iter == '\t')) { |
| 757 |
++str_iter; |
| 758 |
} |
| 759 |
return S_CAST(CXXCONST_CP, str_iter); |
| 760 |
} |
| 761 |
|
| 762 |
#ifdef __cplusplus |
| 763 |
HEADER_INLINE char* FirstNonTspace(char* str_iter) { |
| 764 |
return const_cast<char*>(FirstNonTspace(const_cast<const char*>(str_iter))); |
| 765 |
} |
| 766 |
#endif |
| 767 |
|
| 768 |
HEADER_INLINE CXXCONST_CP FirstPostspaceBounded(const char* str_iter, const char* str_end) { |
| 769 |
for (; str_iter != str_end; ++str_iter) { |
| 770 |
if (ctou32(*str_iter) > ' ') { |
| 771 |
break; |
| 772 |
} |
| 773 |
} |
| 774 |
return S_CAST(CXXCONST_CP, str_iter); |
| 775 |
} |
| 776 |
|
| 777 |
#ifdef __cplusplus |
| 778 |
HEADER_INLINE char* FirstPostspaceBounded(char* str_iter, char* str_end) { |
| 779 |
return const_cast<char*>(FirstPostspaceBounded(const_cast<const char*>(str_iter), const_cast<const char*>(str_end))); |
| 780 |
} |
| 781 |
#endif |
| 782 |
|
| 783 |
|
| 784 |
// See also (93) on TAOCP vol 4a, pp. 153. Todo: benchmark a |
| 785 |
// FirstPrecharUnsafe() function which uses that (unsafe because it reads up to |
| 786 |
// 7 characters past buffer end). |
| 787 |
HEADER_INLINE CXXCONST_CP FirstPrechar(const char* str_iter, uint32_t char_code) { |
| 788 |
while (ctou32(*str_iter) >= char_code) { |
| 789 |
++str_iter; |
| 790 |
} |
| 791 |
return S_CAST(CXXCONST_CP, str_iter); |
| 792 |
} |
| 793 |
|
| 794 |
// It is worth distinguishing between incremental parsing functions where it's |
| 795 |
// rarely necessary to scan more than 40 characters or so, and scans which are |
| 796 |
// likely to be long-range. The former is frequently best handled with a |
| 797 |
// simple loop even when AVX2 movemask or ARM narrowing-shift is available; it |
| 798 |
// may not even be worth inserting a conditional to select between the two when |
| 799 |
// length is known. The latter benefits greatly from movemask/shrn-4. |
| 800 |
// |
| 801 |
// The following standard library and plink2-library scanning functions can be |
| 802 |
// trusted to use movemask/shrn-4: |
| 803 |
// strlen, strchr, memchr |
| 804 |
// rawmemchr, strchrnul |
| 805 |
// rawmemchr2, rawmemchr3, strnul, strchrnul_n, strchrnul2, strchrnul3, |
| 806 |
// strchrnul_n_mov, incr_strchrnul_n_mov |
| 807 |
// NextTokenMultFar |
| 808 |
// AdvToNthDelimChecked, AdvToNthDelim, AdvToDelimOrEnd, Memrchr, |
| 809 |
// LastSpaceOrEoln |
| 810 |
|
| 811 |
/* |
| 812 |
#ifdef __LP64__ |
| 813 |
// Requires char_code <= 128. |
| 814 |
CXXCONST_CP FirstPrecharFar(const char* str_iter, uint32_t char_code); |
| 815 |
#else |
| 816 |
HEADER_INLINE CXXCONST_CP FirstPrecharFar(const char* str_iter, uint32_t char_code) { |
| 817 |
return FirstPrechar(str_iter, char_code); |
| 818 |
} |
| 819 |
#endif |
| 820 |
*/ |
| 821 |
|
| 822 |
HEADER_INLINE CXXCONST_CP FirstPrespace(const char* str_iter) { |
| 823 |
return S_CAST(CXXCONST_CP, FirstPrechar(str_iter, ' ')); |
| 824 |
} |
| 825 |
|
| 826 |
HEADER_INLINE CXXCONST_CP NextPrespace(const char* str_iter) { |
| 827 |
return S_CAST(CXXCONST_CP, FirstPrechar(&(str_iter[1]), ' ')); |
| 828 |
} |
| 829 |
|
| 830 |
HEADER_INLINE CXXCONST_CP FirstSpaceOrEoln(const char* str_iter) { |
| 831 |
return S_CAST(CXXCONST_CP, FirstPrechar(str_iter, 33)); |
| 832 |
} |
| 833 |
|
| 834 |
// assumes we are currently in a token -- UNSAFE OTHERWISE |
| 835 |
HEADER_INLINE CXXCONST_CP CurTokenEnd(const char* str_iter) { |
| 836 |
// assert(ctou32(*str_iter) > 32); |
| 837 |
return S_CAST(CXXCONST_CP, FirstPrechar(&(str_iter[1]), 33)); |
| 838 |
} |
| 839 |
|
| 840 |
#ifdef __cplusplus |
| 841 |
HEADER_INLINE char* FirstPrechar(char* str_iter, uint32_t char_code) { |
| 842 |
return const_cast<char*>(FirstPrechar(const_cast<const char*>(str_iter), char_code)); |
| 843 |
} |
| 844 |
|
| 845 |
/* |
| 846 |
HEADER_INLINE char* FirstPrecharFar(char* str_iter, uint32_t char_code) { |
| 847 |
return const_cast<char*>(FirstPrecharFar(const_cast<const char*>(str_iter), char_code)); |
| 848 |
} |
| 849 |
*/ |
| 850 |
|
| 851 |
HEADER_INLINE char* FirstPrespace(char* str_iter) { |
| 852 |
return const_cast<char*>(FirstPrespace(const_cast<const char*>(str_iter))); |
| 853 |
} |
| 854 |
|
| 855 |
HEADER_INLINE char* NextPrespace(char* str_iter) { |
| 856 |
return const_cast<char*>(NextPrespace(const_cast<const char*>(str_iter))); |
| 857 |
} |
| 858 |
|
| 859 |
HEADER_INLINE char* FirstSpaceOrEoln(char* str_iter) { |
| 860 |
return const_cast<char*>(FirstSpaceOrEoln(const_cast<const char*>(str_iter))); |
| 861 |
} |
| 862 |
|
| 863 |
HEADER_INLINE char* CurTokenEnd(char* str_iter) { |
| 864 |
return const_cast<char*>(CurTokenEnd(const_cast<const char*>(str_iter))); |
| 865 |
} |
| 866 |
#endif |
| 867 |
|
| 868 |
HEADER_INLINE CXXCONST_CP CsvFieldEnd(const char* token_iter) { |
| 869 |
unsigned char ucc = *token_iter; |
| 870 |
while ((ucc >= ' ') && (ucc != ',')) { |
| 871 |
ucc = *(++token_iter); |
| 872 |
} |
| 873 |
return S_CAST(CXXCONST_CP, token_iter); |
| 874 |
} |
| 875 |
|
| 876 |
// length-zero tokens and non-leading spaces are permitted in the |
| 877 |
// comma-delimiter case |
| 878 |
// assumes we're inside a token if comma_delim is false |
| 879 |
HEADER_INLINE CXXCONST_CP CommaOrTspaceTokenEnd(const char* token_iter, uint32_t comma_delim) { |
| 880 |
if (comma_delim) { |
| 881 |
return CsvFieldEnd(token_iter); |
| 882 |
} |
| 883 |
return S_CAST(CXXCONST_CP, CurTokenEnd(token_iter)); |
| 884 |
} |
| 885 |
|
| 886 |
#ifdef __cplusplus |
| 887 |
HEADER_INLINE char* CsvFieldEnd(char* token_iter) { |
| 888 |
return const_cast<char*>(CsvFieldEnd(const_cast<const char*>(token_iter))); |
| 889 |
} |
| 890 |
|
| 891 |
HEADER_INLINE char* CommaOrTspaceTokenEnd(char* token_iter, uint32_t comma_delim) { |
| 892 |
return const_cast<char*>(CommaOrTspaceTokenEnd(const_cast<const char*>(token_iter), comma_delim)); |
| 893 |
} |
| 894 |
#endif |
| 895 |
|
| 896 |
HEADER_INLINE CXXCONST_CP CommaOrTspaceFirstToken(const char* token_end_iter, uint32_t comma_delim) { |
| 897 |
// assumes token_end_iter is non-null, returns nullptr if there are no more |
| 898 |
// tokens |
| 899 |
// assert(token_end_iter); |
| 900 |
if (comma_delim) { |
| 901 |
if ((*token_end_iter) != ',') { |
| 902 |
return nullptr; |
| 903 |
} |
| 904 |
return S_CAST(CXXCONST_CP, FirstNonTspace(&(token_end_iter[1]))); |
| 905 |
} |
| 906 |
const char* str = FirstNonTspace(token_end_iter); |
| 907 |
return IsEolnKns(*str)? nullptr : S_CAST(CXXCONST_CP, str); |
| 908 |
} |
| 909 |
|
| 910 |
#ifdef __cplusplus |
| 911 |
HEADER_INLINE char* CommaOrTspaceFirstToken(char* token_end_iter, uint32_t comma_delim) { |
| 912 |
return const_cast<char*>(CommaOrTspaceFirstToken(const_cast<const char*>(token_end_iter), comma_delim)); |
| 913 |
} |
| 914 |
#endif |
| 915 |
|
| 916 |
|
| 917 |
// Returns whether uppercased str_iter matches nonempty fixed_str. Assumes |
| 918 |
// fixed_str contains nothing but letters and a null terminator. |
| 919 |
// uint32_t match_upper(const char* str_iter, const char* fixed_str); |
| 920 |
|
| 921 |
uint32_t MatchUpperCounted(const char* str, const char* fixed_str, uint32_t ct); |
| 922 |
|
| 923 |
HEADER_INLINE uint32_t MatchUpperKLen(const char* str, const char* fixed_str, uint32_t str_slen) { |
| 924 |
const uint32_t fixed_slen = strlen(fixed_str); |
| 925 |
if (str_slen != fixed_slen) { |
| 926 |
return 0; |
| 927 |
} |
| 928 |
return MatchUpperCounted(str, fixed_str, fixed_slen); |
| 929 |
} |
| 930 |
|
| 931 |
HEADER_INLINE uint32_t MatchUpperK(const char* str, const char* fixed_str) { |
| 932 |
return MatchUpperCounted(str, fixed_str, strlen(fixed_str)); |
| 933 |
} |
| 934 |
|
| 935 |
uint32_t strcaseequal(const char* str1, const char* str2, uint32_t ct); |
| 936 |
|
| 937 |
/* |
| 938 |
void str_toupper(char* str_iter); |
| 939 |
|
| 940 |
void buf_toupper(uint32_t slen, char* strbuf); |
| 941 |
|
| 942 |
void strcpy_toupper(char* target, const char* source); |
| 943 |
|
| 944 |
char* memcpya_toupper(char* __restrict target, const char* __restrict source, uint32_t slen); |
| 945 |
*/ |
| 946 |
|
| 947 |
uint32_t IsAlphanumeric(const char* str_iter); |
| 948 |
|
| 949 |
// ScanPosintCapped(), ScanUintCapped(), ScanIntAbsBounded(), ScanInt32(), |
| 950 |
// ScanPosintDefcap(), ScanUintDefcap(), ScanIntAbsDefcap(), ScanUintIcap() in |
| 951 |
// plink2_base |
| 952 |
|
| 953 |
BoolErr ScanPosintptr(const char* str_iter, uintptr_t* valp); |
| 954 |
|
| 955 |
// Requires (cap+1) * 100 < 2^64. |
| 956 |
BoolErr ScanmovU64Capped(uint64_t cap, const char** str_iterp, uint64_t* valp); |
| 957 |
|
| 958 |
#ifdef __LP64__ |
| 959 |
BoolErr ScanmovPosintCapped(uint64_t cap, const char** str_iterp, uint32_t* valp); |
| 960 |
|
| 961 |
BoolErr ScanmovUintCapped(uint64_t cap, const char** str_iterp, uint32_t* valp); |
| 962 |
|
| 963 |
// 2^{-31} < -abs_floor <= 0 <= cap < 2^31 |
| 964 |
BoolErr ScanmovIntBounded(uint64_t abs_floor, uint64_t cap, const char** str_iterp, int32_t* valp); |
| 965 |
#else |
| 966 |
BoolErr ScanmovPosintCapped32(uint32_t cap_div_10, uint32_t cap_mod_10, const char** str_iterp, uint32_t* valp); |
| 967 |
|
| 968 |
BoolErr ScanmovUintCapped32(uint32_t cap_div_10, uint32_t cap_mod_10, const char** str_iterp, uint32_t* valp); |
| 969 |
|
| 970 |
BoolErr ScanmovIntBounded32(uint32_t abs_floor_div_10, uint32_t abs_floor_mod_10, uint32_t cap_div_10, uint32_t cap_mod_10, const char** str_iterp, int32_t* valp); |
| 971 |
|
| 972 |
HEADER_INLINE BoolErr ScanmovPosintCapped(uint32_t cap, const char** str_iterp, uint32_t* valp) { |
| 973 |
return ScanmovPosintCapped32(cap / 10, cap % 10, str_iterp, valp); |
| 974 |
} |
| 975 |
|
| 976 |
HEADER_INLINE BoolErr ScanmovUintCapped(uint32_t cap, const char** str_iterp, uint32_t* valp) { |
| 977 |
return ScanmovUintCapped32(cap / 10, cap % 10, str_iterp, valp); |
| 978 |
} |
| 979 |
|
| 980 |
HEADER_INLINE BoolErr ScanmovIntBounded(uint32_t abs_floor, uint32_t cap, const char** str_iterp, int32_t* valp) { |
| 981 |
return ScanmovIntBounded32(abs_floor / 10, abs_floor % 10, cap / 10, cap % 10, str_iterp, valp); |
| 982 |
} |
| 983 |
#endif |
| 984 |
|
| 985 |
HEADER_INLINE BoolErr ScanmovUintDefcap(const char** str_iterp, uint32_t* valp) { |
| 986 |
return ScanmovUintCapped(0x7ffffffe, str_iterp, valp); |
| 987 |
} |
| 988 |
|
| 989 |
// This has different semantics from ScanmovPosintCapped, etc. since integer |
| 990 |
// readers don't take much code (so it's fine to have a bunch of similar |
| 991 |
// functions, optimized for slightly different use cases), but we only want one |
| 992 |
// core floating point reader. |
| 993 |
// (update, 3 Feb 2018: renamed the integer readers above to start with |
| 994 |
// scanmov_ instead of scanadv_, to reflect the interface difference between |
| 995 |
// returning a pointer and moving the input pointer forward.) |
| 996 |
CXXCONST_CP ScanadvDouble(const char* str_iter, double* valp); |
| 997 |
|
| 998 |
// Thin wrapper that verifies the token ends with whitespace/eoln; should be |
| 999 |
// used whenever comma/semicolon/etc. delimiters are not ok. |
| 1000 |
HEADER_INLINE CXXCONST_CP ScantokDouble(const char* str_iter, double* valp) { |
| 1001 |
CXXCONST_CP parsed_end = ScanadvDouble(str_iter, valp); |
| 1002 |
if ((!parsed_end) || (!IsSpaceOrEoln(*parsed_end))) { |
| 1003 |
return nullptr; |
| 1004 |
} |
| 1005 |
return parsed_end; |
| 1006 |
} |
| 1007 |
|
| 1008 |
#ifdef __cplusplus |
| 1009 |
HEADER_INLINE char* ScanadvDouble(char* str_iter, double* valp) { |
| 1010 |
return const_cast<char*>(ScanadvDouble(const_cast<const char*>(str_iter), valp)); |
| 1011 |
} |
| 1012 |
|
| 1013 |
HEADER_INLINE char* ScantokDouble(char* str_iter, double* valp) { |
| 1014 |
return const_cast<char*>(ScantokDouble(const_cast<const char*>(str_iter), valp)); |
| 1015 |
} |
| 1016 |
#endif |
| 1017 |
|
| 1018 |
// remove unlikely() if any caller ever tries to reparse the string as |
| 1019 |
// something else in error case (this is a valid ScanadvDouble() use case) |
| 1020 |
HEADER_INLINE BoolErr ScanFloat(const char* ss, float* valp) { |
| 1021 |
double dxx; |
| 1022 |
if (unlikely(!ScantokDouble(ss, &dxx))) { |
| 1023 |
return 1; |
| 1024 |
} |
| 1025 |
if (unlikely(fabs(dxx) > 3.4028235677973362e38)) { |
| 1026 |
return 1; |
| 1027 |
} |
| 1028 |
*valp = S_CAST(float, dxx); |
| 1029 |
return 0; |
| 1030 |
} |
| 1031 |
|
| 1032 |
CXXCONST_CP ScanadvLn(const char* str_iter, double* ln_ptr); |
| 1033 |
|
| 1034 |
HEADER_INLINE CXXCONST_CP ScantokLn(const char* str_iter, double* ln_ptr) { |
| 1035 |
CXXCONST_CP parsed_end = ScanadvLn(str_iter, ln_ptr); |
| 1036 |
if ((!parsed_end) || (!IsSpaceOrEoln(*parsed_end))) { |
| 1037 |
return nullptr; |
| 1038 |
} |
| 1039 |
return parsed_end; |
| 1040 |
} |
| 1041 |
|
| 1042 |
#ifdef __cplusplus |
| 1043 |
HEADER_INLINE char* ScanadvLn(char* str_iter, double* ln_ptr) { |
| 1044 |
return const_cast<char*>(ScanadvLn(const_cast<const char*>(str_iter), ln_ptr)); |
| 1045 |
} |
| 1046 |
|
| 1047 |
HEADER_INLINE char* ScantokLn(char* str_iter, double* ln_ptr) { |
| 1048 |
return const_cast<char*>(ScantokLn(const_cast<const char*>(str_iter), ln_ptr)); |
| 1049 |
} |
| 1050 |
#endif |
| 1051 |
|
| 1052 |
// These provide the same interface as ScanPosintCapped(), etc., but there are |
| 1053 |
// two differences in behavior to make these more suitable for parsing of |
| 1054 |
// command-line parameters: |
| 1055 |
// - The strings are initially parsed as floating-point, and then (if an |
| 1056 |
// integer is expected) the function errors out if the result isn't an exact |
| 1057 |
// integer. This allows exponential notation to be used. |
| 1058 |
// - Unlike atoi()/ScanPosintCapped(), the function errors out if parsing stops |
| 1059 |
// at non-whitespace. |
| 1060 |
// The performance cost of this behavior is relatively high: these functions |
| 1061 |
// shouldn't be used for internal file-reading loops. |
| 1062 |
BoolErr ScanPosintCappedx(const char* str_iter, uint64_t cap, uint32_t* valp); |
| 1063 |
|
| 1064 |
BoolErr ScanUintCappedx(const char* str_iter, uint64_t cap, uint32_t* valp); |
| 1065 |
|
| 1066 |
BoolErr ScanIntAbsBoundedx(const char* str_iter, int64_t bound, int32_t* valp); |
| 1067 |
|
| 1068 |
HEADER_INLINE BoolErr ScanInt32x(const char* str, int32_t* valp) { |
| 1069 |
return ScanIntAbsBoundedx(str, 0x7fffffff, valp); |
| 1070 |
} |
| 1071 |
|
| 1072 |
HEADER_INLINE BoolErr ScanPosintDefcapx(const char* str, uint32_t* valp) { |
| 1073 |
return ScanPosintCappedx(str, 0x7ffffffe, valp); |
| 1074 |
} |
| 1075 |
|
| 1076 |
HEADER_INLINE BoolErr ScanUintDefcapx(const char* str, uint32_t* valp) { |
| 1077 |
return ScanUintCappedx(str, 0x7ffffffe, valp); |
| 1078 |
} |
| 1079 |
|
| 1080 |
BoolErr ScanPosintptrx(const char* str_iter, uintptr_t* valp); |
| 1081 |
|
| 1082 |
|
| 1083 |
HEADER_INLINE void AppendBinaryEoln(char** dst_ptr) { |
| 1084 |
#ifdef _WIN32 |
| 1085 |
(*dst_ptr)[0] = '\r'; |
| 1086 |
(*dst_ptr)[1] = '\n'; |
| 1087 |
*dst_ptr += 2; |
| 1088 |
#else |
| 1089 |
**dst_ptr = '\n'; |
| 1090 |
*dst_ptr += 1; |
| 1091 |
#endif |
| 1092 |
} |
| 1093 |
|
| 1094 |
HEADER_INLINE void DecrAppendBinaryEoln(char** dst_ptr) { |
| 1095 |
#ifdef _WIN32 |
| 1096 |
(*dst_ptr)[-1] = '\r'; |
| 1097 |
(*dst_ptr)[0] = '\n'; |
| 1098 |
*dst_ptr += 1; |
| 1099 |
#else |
| 1100 |
(*dst_ptr)[-1] = '\n'; |
| 1101 |
#endif |
| 1102 |
} |
| 1103 |
|
| 1104 |
void GetTopTwoUi(const uint32_t* __restrict uint_arr, uintptr_t uia_size, uintptr_t* __restrict top_idx_ptr, uintptr_t* __restrict second_idx_ptr); |
| 1105 |
|
| 1106 |
// safer than CurTokenEnd(), since it handles length zero |
| 1107 |
// "se" = space/eoln treated as terminators |
| 1108 |
HEADER_INLINE uintptr_t strlen_se(const char* ss) { |
| 1109 |
const char* ss2 = ss; |
| 1110 |
while (!IsSpaceOrEoln(*ss2)) { |
| 1111 |
ss2++; |
| 1112 |
} |
| 1113 |
return ss2 - ss; |
| 1114 |
} |
| 1115 |
|
| 1116 |
// just an alias for rawmemchr which doesn't require a subsequent static-cast. |
| 1117 |
HEADER_INLINE CXXCONST_CP AdvToDelim(const char* str_iter, char delim) { |
| 1118 |
return S_CAST(CXXCONST_CP, rawmemchr(str_iter, delim)); |
| 1119 |
} |
| 1120 |
|
| 1121 |
#ifdef __cplusplus |
| 1122 |
HEADER_INLINE char* AdvToDelim(char* str_iter, char delim) { |
| 1123 |
return const_cast<char*>(AdvToDelim(const_cast<const char*>(str_iter), delim)); |
| 1124 |
} |
| 1125 |
#endif |
| 1126 |
|
| 1127 |
HEADER_INLINE CXXCONST_CP AdvPastDelim(const char* str_iter, char delim) { |
| 1128 |
return &(AdvToDelim(str_iter, delim)[1]); |
| 1129 |
} |
| 1130 |
|
| 1131 |
#ifdef __cplusplus |
| 1132 |
HEADER_INLINE char* AdvPastDelim(char* str_iter, char delim) { |
| 1133 |
return &(AdvToDelim(str_iter, delim)[1]); |
| 1134 |
} |
| 1135 |
#endif |
| 1136 |
|
| 1137 |
|
| 1138 |
#ifdef __LP64__ |
| 1139 |
// This is a major VCF-parsing bottleneck, and inlining it makes a big |
| 1140 |
// difference. |
| 1141 |
|
| 1142 |
// ct must be nonzero |
| 1143 |
HEADER_INLINE CXXCONST_CP AdvToNthDelimChecked(const char* str_iter, const char* str_end, uint32_t ct, char delim) { |
| 1144 |
const uintptr_t starting_addr = R_CAST(uintptr_t, str_iter); |
| 1145 |
const uintptr_t ending_addr = R_CAST(uintptr_t, str_end); |
| 1146 |
VecUc* str_viter = R_CAST(VecUc*, RoundDownPow2(starting_addr, kBytesPerVec)); |
| 1147 |
const VecUc vvec_all_delim = vecuc_set1(delim); |
| 1148 |
VecUc cur_vvec = *str_viter; |
| 1149 |
VecUc delim_vvec = (cur_vvec == vvec_all_delim); |
| 1150 |
const uint32_t leading_byte_ct = starting_addr - R_CAST(uintptr_t, str_viter); |
| 1151 |
# ifndef SIMDE_ARM_NEON_A32V8_NATIVE |
| 1152 |
uint32_t delimiter_bytes = vecuc_movemask(delim_vvec) >> leading_byte_ct; |
| 1153 |
for (uint32_t remaining_delim_ct = ct; ; ) { |
| 1154 |
const uint32_t cur_delim_ct = PopcountVec8thUint(delimiter_bytes); |
| 1155 |
if (cur_delim_ct >= remaining_delim_ct) { |
| 1156 |
// todo: confirm this is faster when done outside vector-space, despite |
| 1157 |
// additional comparison relative to AdvToNthDelimOverread |
| 1158 |
|
| 1159 |
// must not check preceding bytes if this happens on first loop iteration |
| 1160 |
if (R_CAST(const char*, str_viter) > str_iter) { |
| 1161 |
str_iter = R_CAST(const char*, str_viter); |
| 1162 |
} |
| 1163 |
for (; str_iter != str_end; ++str_iter) { |
| 1164 |
const char cc = *str_iter; |
| 1165 |
if (cc != delim) { |
| 1166 |
continue; |
| 1167 |
} |
| 1168 |
if (!(--remaining_delim_ct)) { |
| 1169 |
return S_CAST(CXXCONST_CP, str_iter); |
| 1170 |
} |
| 1171 |
} |
| 1172 |
return nullptr; |
| 1173 |
} |
| 1174 |
remaining_delim_ct -= cur_delim_ct; |
| 1175 |
++str_viter; |
| 1176 |
if (R_CAST(uintptr_t, str_viter) >= ending_addr) { |
| 1177 |
return nullptr; |
| 1178 |
} |
| 1179 |
cur_vvec = *str_viter; |
| 1180 |
delim_vvec = (cur_vvec == vvec_all_delim); |
| 1181 |
delimiter_bytes = vecuc_movemask(delim_vvec); |
| 1182 |
} |
| 1183 |
# else |
| 1184 |
uint64_t delimiter_nybbles = arm_shrn4_uc(delim_vvec) >> (4 * leading_byte_ct); |
| 1185 |
for (uint32_t remaining_delim_ct = ct; ; ) { |
| 1186 |
const uint32_t cur_delim_ct = count_set_nybbles(delimiter_nybbles); |
| 1187 |
if (cur_delim_ct >= remaining_delim_ct) { |
| 1188 |
if (R_CAST(const char*, str_viter) > str_iter) { |
| 1189 |
str_iter = R_CAST(const char*, str_viter); |
| 1190 |
} |
| 1191 |
for (; str_iter != str_end; ++str_iter) { |
| 1192 |
const char cc = *str_iter; |
| 1193 |
if (cc != delim) { |
| 1194 |
continue; |
| 1195 |
} |
| 1196 |
if (!(--remaining_delim_ct)) { |
| 1197 |
return S_CAST(CXXCONST_CP, str_iter); |
| 1198 |
} |
| 1199 |
} |
| 1200 |
return nullptr; |
| 1201 |
} |
| 1202 |
remaining_delim_ct -= cur_delim_ct; |
| 1203 |
++str_viter; |
| 1204 |
if (R_CAST(uintptr_t, str_viter) >= ending_addr) { |
| 1205 |
return nullptr; |
| 1206 |
} |
| 1207 |
cur_vvec = *str_viter; |
| 1208 |
delim_vvec = (cur_vvec == vvec_all_delim); |
| 1209 |
delimiter_nybbles = arm_shrn4_uc(delim_vvec); |
| 1210 |
} |
| 1211 |
# endif |
| 1212 |
} |
| 1213 |
|
| 1214 |
HEADER_INLINE CXXCONST_CP AdvToNthDelim(const char* str_iter, uint32_t ct, char delim) { |
| 1215 |
const VecUc vvec_all_delim = vecuc_set1(delim); |
| 1216 |
const uintptr_t starting_addr = R_CAST(uintptr_t, str_iter); |
| 1217 |
VecUc* str_viter = R_CAST(VecUc*, RoundDownPow2(starting_addr, kBytesPerVec)); |
| 1218 |
VecUc cur_vvec = *str_viter; |
| 1219 |
VecUc delim_vvec = (cur_vvec == vvec_all_delim); |
| 1220 |
const uint32_t leading_byte_ct = starting_addr - R_CAST(uintptr_t, str_viter); |
| 1221 |
# ifndef SIMDE_ARM_NEON_A32V8_NATIVE |
| 1222 |
uint32_t delimiter_bytes = vecuc_movemask(delim_vvec) >> leading_byte_ct; |
| 1223 |
for (uint32_t remaining_delim_ct = ct; ; ) { |
| 1224 |
const uint32_t cur_delim_ct = PopcountVec8thUint(delimiter_bytes); |
| 1225 |
if (cur_delim_ct >= remaining_delim_ct) { |
| 1226 |
if (R_CAST(const char*, str_viter) > str_iter) { |
| 1227 |
str_iter = R_CAST(const char*, str_viter); |
| 1228 |
} |
| 1229 |
for (; ; ++str_iter) { |
| 1230 |
const char cc = *str_iter; |
| 1231 |
if (cc != delim) { |
| 1232 |
continue; |
| 1233 |
} |
| 1234 |
if (!(--remaining_delim_ct)) { |
| 1235 |
return S_CAST(CXXCONST_CP, str_iter); |
| 1236 |
} |
| 1237 |
} |
| 1238 |
} |
| 1239 |
remaining_delim_ct -= cur_delim_ct; |
| 1240 |
++str_viter; |
| 1241 |
cur_vvec = *str_viter; |
| 1242 |
delim_vvec = (cur_vvec == vvec_all_delim); |
| 1243 |
delimiter_bytes = vecuc_movemask(delim_vvec); |
| 1244 |
} |
| 1245 |
# else |
| 1246 |
uint64_t delimiter_nybbles = arm_shrn4_uc(delim_vvec) >> (4 * leading_byte_ct); |
| 1247 |
for (uint32_t remaining_delim_ct = ct; ; ) { |
| 1248 |
const uint32_t cur_delim_ct = count_set_nybbles(delimiter_nybbles); |
| 1249 |
if (cur_delim_ct >= remaining_delim_ct) { |
| 1250 |
if (R_CAST(const char*, str_viter) > str_iter) { |
| 1251 |
str_iter = R_CAST(const char*, str_viter); |
| 1252 |
} |
| 1253 |
for (; ; ++str_iter) { |
| 1254 |
const char cc = *str_iter; |
| 1255 |
if (cc != delim) { |
| 1256 |
continue; |
| 1257 |
} |
| 1258 |
if (!(--remaining_delim_ct)) { |
| 1259 |
return S_CAST(CXXCONST_CP, str_iter); |
| 1260 |
} |
| 1261 |
} |
| 1262 |
} |
| 1263 |
remaining_delim_ct -= cur_delim_ct; |
| 1264 |
++str_viter; |
| 1265 |
cur_vvec = *str_viter; |
| 1266 |
delim_vvec = (cur_vvec == vvec_all_delim); |
| 1267 |
delimiter_nybbles = arm_shrn4_uc(delim_vvec); |
| 1268 |
} |
| 1269 |
# endif |
| 1270 |
} |
| 1271 |
|
| 1272 |
HEADER_INLINE CXXCONST_CP AdvToNthDelimOverread(const char* str_iter, uint32_t ct, char delim) { |
| 1273 |
const VecUc vvec_all_delim = vecuc_set1(delim); |
| 1274 |
for (uint32_t remaining_delim_ct = ct; ; ) { |
| 1275 |
VecUc cur_vvec = vecuc_loadu(str_iter); |
| 1276 |
VecUc delim_vvec = (cur_vvec == vvec_all_delim); |
| 1277 |
# ifndef SIMDE_ARM_NEON_A32V8_NATIVE |
| 1278 |
uint32_t delimiter_bytes = vecuc_movemask(delim_vvec); |
| 1279 |
const uint32_t cur_delim_ct = PopcountVec8thUint(delimiter_bytes); |
| 1280 |
# else |
| 1281 |
const uint64_t delimiter_nybbles = arm_shrn4_uc(delim_vvec); |
| 1282 |
const uint32_t cur_delim_ct = count_set_nybbles(delimiter_nybbles); |
| 1283 |
# endif |
| 1284 |
if (cur_delim_ct >= remaining_delim_ct) { |
| 1285 |
// Faster to do this part outside of vector-space in my testing. |
| 1286 |
for (; ; ++str_iter) { |
| 1287 |
const char cc = *str_iter; |
| 1288 |
if (cc != delim) { |
| 1289 |
continue; |
| 1290 |
} |
| 1291 |
if (!(--remaining_delim_ct)) { |
| 1292 |
return S_CAST(CXXCONST_CP, str_iter); |
| 1293 |
} |
| 1294 |
} |
| 1295 |
} |
| 1296 |
remaining_delim_ct -= cur_delim_ct; |
| 1297 |
str_iter = &(str_iter[kBytesPerVec]); |
| 1298 |
} |
| 1299 |
} |
| 1300 |
#else // !__LP64__ |
| 1301 |
HEADER_INLINE CXXCONST_CP AdvToNthDelimChecked(const char* str_iter, const char* str_end, uint32_t ct, char delim) { |
| 1302 |
for (uint32_t remaining_delim_ct = ct; ; ) { |
| 1303 |
const char* next_delim = S_CAST(const char*, memchr(str_iter, delim, str_end - str_iter)); |
| 1304 |
if (!next_delim) { |
| 1305 |
return nullptr; |
| 1306 |
} |
| 1307 |
if (!(--remaining_delim_ct)) { |
| 1308 |
return S_CAST(CXXCONST_CP, next_delim); |
| 1309 |
} |
| 1310 |
str_iter = &(next_delim[1]); |
| 1311 |
} |
| 1312 |
} |
| 1313 |
|
| 1314 |
HEADER_INLINE CXXCONST_CP AdvToNthDelim(const char* str_iter, uint32_t ct, char delim) { |
| 1315 |
for (uint32_t remaining_delim_ct = ct; ; ) { |
| 1316 |
const char* next_delim = AdvToDelim(str_iter, delim); |
| 1317 |
if (!(--remaining_delim_ct)) { |
| 1318 |
return S_CAST(CXXCONST_CP, next_delim); |
| 1319 |
} |
| 1320 |
str_iter = &(next_delim[1]); |
| 1321 |
} |
| 1322 |
} |
| 1323 |
|
| 1324 |
HEADER_INLINE CXXCONST_CP AdvToNthDelimOverread(const char* str_iter, uint32_t ct, char delim) { |
| 1325 |
return AdvToNthDelim(str_iter, ct, delim); |
| 1326 |
} |
| 1327 |
#endif |
| 1328 |
|
| 1329 |
#ifdef __cplusplus |
| 1330 |
HEADER_INLINE char* AdvToNthDelimChecked(char* str_iter, char* str_end, uint32_t ct, char delim) { |
| 1331 |
return const_cast<char*>(AdvToNthDelimChecked(const_cast<const char*>(str_iter), const_cast<const char*>(str_end), ct, delim)); |
| 1332 |
} |
| 1333 |
|
| 1334 |
HEADER_INLINE char* AdvToNthDelim(char* str_iter, uint32_t ct, char delim) { |
| 1335 |
return const_cast<char*>(AdvToNthDelim(const_cast<const char*>(str_iter), ct, delim)); |
| 1336 |
} |
| 1337 |
#endif |
| 1338 |
|
| 1339 |
// ok if str_iter is at end of current token |
| 1340 |
HEADER_INLINE CXXCONST_CP NextToken(const char* str_iter) { |
| 1341 |
if (!str_iter) { |
| 1342 |
return nullptr; |
| 1343 |
} |
| 1344 |
unsigned char ucc = *str_iter; |
| 1345 |
while (ucc > ' ') { |
| 1346 |
ucc = *(++str_iter); |
| 1347 |
} |
| 1348 |
while ((ucc == ' ') || (ucc == '\t')) { |
| 1349 |
ucc = *(++str_iter); |
| 1350 |
} |
| 1351 |
return (ucc > 32)? S_CAST(CXXCONST_CP, str_iter) : nullptr; |
| 1352 |
} |
| 1353 |
|
| 1354 |
#ifdef __cplusplus |
| 1355 |
HEADER_INLINE char* NextToken(char* str_iter) { |
| 1356 |
return const_cast<char*>(NextToken(const_cast<const char*>(str_iter))); |
| 1357 |
} |
| 1358 |
#endif |
| 1359 |
|
| 1360 |
HEADER_INLINE CXXCONST_CP NextTokenMult(const char* str_iter, uint32_t ct) { |
| 1361 |
// assert(ct); |
| 1362 |
if (!str_iter) { |
| 1363 |
return nullptr; |
| 1364 |
} |
| 1365 |
unsigned char ucc = *str_iter; |
| 1366 |
do { |
| 1367 |
while (ucc > 32) { |
| 1368 |
ucc = *(++str_iter); |
| 1369 |
} |
| 1370 |
while ((ucc == ' ') || (ucc == '\t')) { |
| 1371 |
ucc = *(++str_iter); |
| 1372 |
} |
| 1373 |
if (ucc <= 32) { |
| 1374 |
return nullptr; |
| 1375 |
} |
| 1376 |
} while (--ct); |
| 1377 |
return S_CAST(CXXCONST_CP, str_iter); |
| 1378 |
} |
| 1379 |
|
| 1380 |
#ifdef USE_AVX2 |
| 1381 |
// todo: determine minimum ct where this pays off. |
| 1382 |
CXXCONST_CP NextTokenMultFar(const char* str_iter, uint32_t ct); |
| 1383 |
#else |
| 1384 |
HEADER_INLINE CXXCONST_CP NextTokenMultFar(const char* str_iter, uint32_t ct) { |
| 1385 |
return NextTokenMult(str_iter, ct); |
| 1386 |
} |
| 1387 |
#endif |
| 1388 |
|
| 1389 |
#ifdef __cplusplus |
| 1390 |
HEADER_INLINE char* NextTokenMult(char* str_iter, uint32_t ct) { |
| 1391 |
return const_cast<char*>(NextTokenMult(const_cast<const char*>(str_iter), ct)); |
| 1392 |
} |
| 1393 |
|
| 1394 |
HEADER_INLINE char* NextTokenMultFar(char* str_iter, uint32_t ct) { |
| 1395 |
return const_cast<char*>(NextTokenMultFar(const_cast<const char*>(str_iter), ct)); |
| 1396 |
} |
| 1397 |
#endif |
| 1398 |
|
| 1399 |
HEADER_INLINE CXXCONST_CP NextTokenMult0(const char* str, uint32_t ct) { |
| 1400 |
// tried replacing this with ternary operator, but that actually seemed to |
| 1401 |
// slow things down a bit under gcc 4.2.1 (tail call optimization issue?). |
| 1402 |
// todo: recheck this under newer gcc/clang. |
| 1403 |
if (ct) { |
| 1404 |
return S_CAST(CXXCONST_CP, NextTokenMult(str, ct)); |
| 1405 |
} |
| 1406 |
return S_CAST(CXXCONST_CP, str); |
| 1407 |
} |
| 1408 |
|
| 1409 |
#ifdef __cplusplus |
| 1410 |
HEADER_INLINE char* NextTokenMult0(char* str, uint32_t ct) { |
| 1411 |
return const_cast<char*>(NextTokenMult0(const_cast<const char*>(str), ct)); |
| 1412 |
} |
| 1413 |
#endif |
| 1414 |
|
| 1415 |
#ifdef USE_AVX2 |
| 1416 |
const char* TokenLexK0(const char* str_iter, const uint32_t* col_types, const uint32_t* col_skips, uint32_t relevant_col_ct, const char** token_ptrs, uint32_t* token_slens); |
| 1417 |
|
| 1418 |
HEADER_INLINE const char* TokenLexK(const char* str_iter, const uint32_t* col_types, const uint32_t* col_skips, uint32_t relevant_col_ct, const char** token_ptrs, uint32_t* token_slens) { |
| 1419 |
return TokenLexK0(str_iter, col_types, col_skips, relevant_col_ct, token_ptrs, token_slens); |
| 1420 |
} |
| 1421 |
#else |
| 1422 |
// assumes str_iter != nullptr |
| 1423 |
// returns nullptr on missing token, otherwise returns pointer to end of last |
| 1424 |
// lexed token |
| 1425 |
HEADER_INLINE const char* TokenLexK(const char* str_iter, const uint32_t* col_types, const uint32_t* col_skips, uint32_t relevant_col_ct, const char** token_ptrs, uint32_t* token_slens) { |
| 1426 |
for (uint32_t relevant_col_idx = 0; relevant_col_idx != relevant_col_ct; ++relevant_col_idx) { |
| 1427 |
const uint32_t cur_col_type = col_types[relevant_col_idx]; |
| 1428 |
str_iter = NextTokenMult(str_iter, col_skips[relevant_col_idx]); |
| 1429 |
if (!str_iter) { |
| 1430 |
return nullptr; |
| 1431 |
} |
| 1432 |
token_ptrs[cur_col_type] = str_iter; |
| 1433 |
const char* token_end = CurTokenEnd(str_iter); |
| 1434 |
token_slens[cur_col_type] = token_end - str_iter; |
| 1435 |
str_iter = token_end; |
| 1436 |
} |
| 1437 |
return str_iter; |
| 1438 |
} |
| 1439 |
|
| 1440 |
HEADER_INLINE const char* TokenLexK0(const char* str_iter, const uint32_t* col_types, const uint32_t* col_skips, uint32_t relevant_col_ct, const char** token_ptrs, uint32_t* token_slens) { |
| 1441 |
if (!col_skips[0]) { |
| 1442 |
const uint32_t cur_col_type = col_types[0]; |
| 1443 |
const char* first_token_end = CurTokenEnd(str_iter); |
| 1444 |
token_ptrs[cur_col_type] = str_iter; |
| 1445 |
token_slens[cur_col_type] = first_token_end - str_iter; |
| 1446 |
str_iter = first_token_end; |
| 1447 |
++col_types; |
| 1448 |
++col_skips; |
| 1449 |
--relevant_col_ct; |
| 1450 |
} |
| 1451 |
return TokenLexK(str_iter, col_types, col_skips, relevant_col_ct, token_ptrs, token_slens); |
| 1452 |
} |
| 1453 |
#endif |
| 1454 |
|
| 1455 |
HEADER_INLINE char* TokenLex(char* str_iter, const uint32_t* col_types, const uint32_t* col_skips, uint32_t relevant_col_ct, char** token_ptrs, uint32_t* token_slens) { |
| 1456 |
return K_CAST(char*, TokenLexK(str_iter, col_types, col_skips, relevant_col_ct, K_CAST(const char**, token_ptrs), token_slens)); |
| 1457 |
} |
| 1458 |
|
| 1459 |
HEADER_INLINE char* TokenLex0(char* str_iter, const uint32_t* col_types, const uint32_t* col_skips, uint32_t relevant_col_ct, char** token_ptrs, uint32_t* token_slens) { |
| 1460 |
return K_CAST(char*, TokenLexK0(str_iter, col_types, col_skips, relevant_col_ct, K_CAST(const char**, token_ptrs), token_slens)); |
| 1461 |
} |
| 1462 |
|
| 1463 |
// ct must be positive for these functions. |
| 1464 |
CXXCONST_CP NextCsvMult(const char* str_iter, uint32_t ct); |
| 1465 |
|
| 1466 |
HEADER_INLINE CXXCONST_CP CommaOrTspaceNextTokenMult(const char* str_iter, uint32_t ct, uint32_t comma_delim) { |
| 1467 |
if (!comma_delim) { |
| 1468 |
return NextTokenMult(str_iter, ct); |
| 1469 |
} |
| 1470 |
return NextCsvMult(str_iter, ct); |
| 1471 |
} |
| 1472 |
|
| 1473 |
#ifdef __cplusplus |
| 1474 |
HEADER_INLINE char* NextCsvMult(char* str_iter, uint32_t ct) { |
| 1475 |
return const_cast<char*>(NextCsvMult(const_cast<const char*>(str_iter), ct)); |
| 1476 |
} |
| 1477 |
|
| 1478 |
HEADER_INLINE char* CommaOrTspaceNextTokenMult(char* str_iter, uint32_t ct, uint32_t comma_delim) { |
| 1479 |
return const_cast<char*>(CommaOrTspaceNextTokenMult(const_cast<const char*>(str_iter), ct, comma_delim)); |
| 1480 |
} |
| 1481 |
#endif |
| 1482 |
|
| 1483 |
#ifdef USE_AVX2 |
| 1484 |
const char* CsvLexK(const char* str_iter, const uint32_t* col_types, const uint32_t* col_skips, uint32_t relevant_col_ct, const char** token_ptrs, uint32_t* token_slens); |
| 1485 |
#else |
| 1486 |
HEADER_INLINE const char* CsvLexK(const char* str_iter, const uint32_t* col_types, const uint32_t* col_skips, uint32_t relevant_col_ct, const char** token_ptrs, uint32_t* token_slens) { |
| 1487 |
for (uint32_t relevant_col_idx = 0; relevant_col_idx != relevant_col_ct; ++relevant_col_idx) { |
| 1488 |
const uint32_t cur_col_type = col_types[relevant_col_idx]; |
| 1489 |
str_iter = NextCsvMult(str_iter, col_skips[relevant_col_idx]); |
| 1490 |
if (!str_iter) { |
| 1491 |
return nullptr; |
| 1492 |
} |
| 1493 |
token_ptrs[cur_col_type] = str_iter; |
| 1494 |
const char* token_end = CsvFieldEnd(str_iter); |
| 1495 |
token_slens[cur_col_type] = token_end - str_iter; |
| 1496 |
str_iter = token_end; |
| 1497 |
} |
| 1498 |
return str_iter; |
| 1499 |
} |
| 1500 |
#endif |
| 1501 |
|
| 1502 |
HEADER_INLINE uintptr_t CountByteInStr(const char* str_iter, char match) { |
| 1503 |
uintptr_t result = 0; |
| 1504 |
while (1) { |
| 1505 |
const char cc = *str_iter; |
| 1506 |
if (!cc) { |
| 1507 |
return result; |
| 1508 |
} |
| 1509 |
++str_iter; |
| 1510 |
result += (cc == match); |
| 1511 |
} |
| 1512 |
} |
| 1513 |
|
| 1514 |
// todo: movemask version of this |
| 1515 |
uint32_t CountTokens(const char* str_iter); |
| 1516 |
|
| 1517 |
// uint32_t CommaOrSpaceCountTokens(const char* str_iter, uint32_t comma_delim); |
| 1518 |
|
| 1519 |
HEADER_INLINE uint32_t CountMultistr(const char* multistr) { |
| 1520 |
uint32_t ct = 0; |
| 1521 |
while (*multistr) { |
| 1522 |
multistr = strnul(multistr); |
| 1523 |
++multistr; |
| 1524 |
++ct; |
| 1525 |
} |
| 1526 |
return ct; |
| 1527 |
} |
| 1528 |
|
| 1529 |
// empty multistr ok |
| 1530 |
uint32_t CountAndMeasureMultistr(const char* multistr, uintptr_t* max_blen_ptr); |
| 1531 |
|
| 1532 |
// better than e.g. hash table if multistr usually contains only one entry |
| 1533 |
// could benchmark against having precomputed strlen array |
| 1534 |
HEADER_INLINE uint32_t InMultistr(const char* multistr, const char* query, uintptr_t query_slen) { |
| 1535 |
while (*multistr) { |
| 1536 |
const uintptr_t slen = strlen(multistr); |
| 1537 |
if ((slen == query_slen) && memequal(multistr, query, query_slen)) { |
| 1538 |
return 1; |
| 1539 |
} |
| 1540 |
multistr = &(multistr[slen + 1]); |
| 1541 |
} |
| 1542 |
return 0; |
| 1543 |
} |
| 1544 |
|
| 1545 |
char* i32toa(int32_t ii, char* start); |
| 1546 |
|
| 1547 |
char* u32toa_z5(uint32_t uii, char* start); |
| 1548 |
|
| 1549 |
char* u32toa_z6(uint32_t uii, char* start); |
| 1550 |
|
| 1551 |
char* u32toa_trunc4(uint32_t uii, char* start); |
| 1552 |
|
| 1553 |
// Write-buffer-sizing constants, to support replacement of dtoa_g() with |
| 1554 |
// higher-precision variants like dtoa_g_p8() or full-blown Ryu. |
| 1555 |
|
| 1556 |
// -0.000123456 |
| 1557 |
// -1.23456e-38 |
| 1558 |
CONSTI32(kMaxFloatGSlen, 12); |
| 1559 |
|
| 1560 |
CONSTI32(kMaxDoubleGSlen, 13); |
| 1561 |
|
| 1562 |
// 1.23456e-2147483647 |
| 1563 |
CONSTI32(kMaxLnGSlen, 19); |
| 1564 |
|
| 1565 |
char* dtoa_g(double dxx, char* start); |
| 1566 |
|
| 1567 |
// We try to avoid micromanaging floating point printing and just use %g |
| 1568 |
// everywhere, but occasionally we explicitly need more precision. |
| 1569 |
// |
| 1570 |
// dtoa_g_p8 provides generic 8-digit precision (instead of %g's 6-digit |
| 1571 |
// default), while print_dosage in plink2_common provides up to 3 places after |
| 1572 |
// the decimal point when dealing with dosages (which are internally |
| 1573 |
// represented as 32768ths). |
| 1574 |
// (may want to replace _p8 with _p10 for perfect int32 handling.) |
| 1575 |
char* dtoa_g_p8(double dxx, char* start); |
| 1576 |
|
| 1577 |
static const double kLn10 = 2.3025850929940457; |
| 1578 |
static const double kRecipLn10 = 0.43429448190325176; |
| 1579 |
static const double kLnNormalMin = -708.3964185322641; |
| 1580 |
|
| 1581 |
char* lntoa_g(double ln_val, char* start); |
| 1582 |
|
| 1583 |
HEADER_INLINE void TrailingZeroesToSpaces(char* start) { |
| 1584 |
--start; |
| 1585 |
while (*start == '0') { |
| 1586 |
*start-- = ' '; |
| 1587 |
} |
| 1588 |
if (*start == '.') { |
| 1589 |
*start = ' '; |
| 1590 |
} |
| 1591 |
} |
| 1592 |
|
| 1593 |
HEADER_INLINE char* ClipTrailingZeroes(char* start) { |
| 1594 |
char cc; |
| 1595 |
do { |
| 1596 |
cc = *(--start); |
| 1597 |
} while (cc == '0'); |
| 1598 |
return &(start[(cc != '.')]); |
| 1599 |
} |
| 1600 |
|
| 1601 |
// "prob" means that the number is guaranteed to be in [0, 1]. |
| 1602 |
// no leading space is printed. trailing zeroes (/decimal point) are erased |
| 1603 |
// iff there is equality to ~13 decimal places. |
| 1604 |
char* dtoa_f_probp6_spaced(double dxx, char* start); |
| 1605 |
|
| 1606 |
char* dtoa_f_probp6_clipped(double dxx, char* start); |
| 1607 |
|
| 1608 |
// char* dtoa_f_p5_clipped(double dxx, char* start); |
| 1609 |
|
| 1610 |
// dedicated ftoa_g() discontinued |
| 1611 |
HEADER_INLINE char* ftoa_g(float fxx, char* start) { |
| 1612 |
return dtoa_g(S_CAST(double, fxx), start); |
| 1613 |
} |
| 1614 |
|
| 1615 |
HEADER_INLINE char* u32toa_x(uint32_t uii, char extra_char, char* start) { |
| 1616 |
char* penult = u32toa(uii, start); |
| 1617 |
*penult = extra_char; |
| 1618 |
return &(penult[1]); |
| 1619 |
} |
| 1620 |
|
| 1621 |
HEADER_INLINE char* i32toa_x(int32_t ii, char extra_char, char* start) { |
| 1622 |
char* penult = i32toa(ii, start); |
| 1623 |
*penult = extra_char; |
| 1624 |
return &(penult[1]); |
| 1625 |
} |
| 1626 |
|
| 1627 |
|
| 1628 |
// overread must be ok. |
| 1629 |
CXXCONST_CP FindSortedStrboxDuplicate(const char* sorted_ids, uintptr_t id_ct, uintptr_t max_id_blen); |
| 1630 |
|
| 1631 |
#ifdef __cplusplus |
| 1632 |
HEADER_INLINE char* FindSortedStrboxDuplicate(char* sorted_ids, uintptr_t id_ct, uintptr_t max_id_blen) { |
| 1633 |
return const_cast<char*>(FindSortedStrboxDuplicate(const_cast<const char*>(sorted_ids), id_ct, max_id_blen)); |
| 1634 |
} |
| 1635 |
#endif |
| 1636 |
|
| 1637 |
// Collapses array of sorted IDs to remove duplicates, and writes pre-collapse |
| 1638 |
// positions to id_starts (so e.g. duplication count of any sample ID can be |
| 1639 |
// determined via subtraction) if it isn't nullptr. |
| 1640 |
// Overread must be ok. |
| 1641 |
// Returns id_ct of collapsed array. |
| 1642 |
// uint32_t CollapseSortedStrbox(uintptr_t id_ct, uintptr_t max_id_blen, char* sorted_ids, uint32_t* id_starts); |
| 1643 |
|
| 1644 |
|
| 1645 |
// returns position of string, or -1 if not found. |
| 1646 |
int32_t bsearch_strbox(const char* idbuf, const char* sorted_strbox, uintptr_t cur_id_slen, uintptr_t max_id_blen, uintptr_t end_idx); |
| 1647 |
|
| 1648 |
// requires null-terminated string |
| 1649 |
int32_t bsearch_strbox_natural(const char* idbuf, const char* sorted_strbox, uintptr_t max_id_blen, uintptr_t end_idx); |
| 1650 |
|
| 1651 |
int32_t bsearch_strptr_overread(const char* idbuf, const char* const* sorted_strptrs, uintptr_t end_idx); |
| 1652 |
|
| 1653 |
int32_t bsearch_strptr_natural(const char* idbuf, const char* const* sorted_strptrs, uintptr_t end_idx); |
| 1654 |
|
| 1655 |
|
| 1656 |
// returns number of elements in sorted_strbox[] less than idbuf. |
| 1657 |
uintptr_t bsearch_strbox_lb(const char* idbuf, const char* sorted_strbox, uintptr_t cur_id_slen, uintptr_t max_id_blen, uintptr_t end_idx); |
| 1658 |
|
| 1659 |
// same result as bsearch_strbox_lb(), but checks against [cur_idx], |
| 1660 |
// [cur_idx + 1], [cur_idx + 3], [cur_idx + 7], etc. before finishing with a |
| 1661 |
// binary search, and assumes cur_id_slen <= max_id_blen and end_idx > 0. |
| 1662 |
uintptr_t ExpsearchStrLb(const char* idbuf, const char* sorted_strbox, uintptr_t cur_id_slen, uintptr_t max_id_blen, uintptr_t end_idx, uintptr_t cur_idx); |
| 1663 |
|
| 1664 |
// Null-terminated string required. |
| 1665 |
uintptr_t ExpsearchNsortStrLb(const char* idbuf, const char* nsorted_strbox, uintptr_t max_id_blen, uintptr_t end_idx, uintptr_t cur_idx); |
| 1666 |
|
| 1667 |
// this is frequently preferable to bsearch_strbox(), since it's way too easy |
| 1668 |
// to forget to convert the sorted-stringbox index to the final index |
| 1669 |
// sample_id_map == nullptr is permitted; in this case id will be an index into |
| 1670 |
// the sorted array |
| 1671 |
HEADER_INLINE BoolErr SortedIdboxFind(const char* idbuf, const char* sorted_idbox, const uint32_t* id_map, uintptr_t cur_id_slen, uintptr_t max_id_blen, uintptr_t end_idx, uint32_t* id_ptr) { |
| 1672 |
const int32_t ii = bsearch_strbox(idbuf, sorted_idbox, cur_id_slen, max_id_blen, end_idx); |
| 1673 |
if (ii == -1) { |
| 1674 |
return 1; |
| 1675 |
} |
| 1676 |
*id_ptr = id_map? id_map[S_CAST(uint32_t, ii)] : S_CAST(uint32_t, ii); |
| 1677 |
return 0; |
| 1678 |
} |
| 1679 |
|
| 1680 |
// This returns 1 on any capitalization of 'na' or 'nan', 0 otherwise. |
| 1681 |
// todo: check whether there's actually any point to the uint16_t type-pun |
| 1682 |
HEADER_INLINE uint32_t IsNanStr(const char* ss, uint32_t slen) { |
| 1683 |
if ((slen > 3) || (slen == 1)) { |
| 1684 |
return 0; |
| 1685 |
} |
| 1686 |
if (!slen) { |
| 1687 |
return 1; |
| 1688 |
} |
| 1689 |
#ifndef NO_UNALIGNED |
| 1690 |
const uint32_t first_two_chars_code = R_CAST(const uint16_t*, ss)[0]; |
| 1691 |
#else |
| 1692 |
uint16_t first_two_chars_code_u16; |
| 1693 |
memcpy_k(&first_two_chars_code_u16, ss, 2); |
| 1694 |
const uint32_t first_two_chars_code = first_two_chars_code_u16; |
| 1695 |
#endif |
| 1696 |
// assumes little-endian |
| 1697 |
if ((first_two_chars_code & 0xdfdf) != 0x414e) { |
| 1698 |
return 0; |
| 1699 |
} |
| 1700 |
return (slen == 2) || ((ctou32(ss[2]) & 0xdf) == 78); |
| 1701 |
} |
| 1702 |
|
| 1703 |
// Matches "inf"/"infinity", any capitalization, can have sign in front. |
| 1704 |
// Assumes is_neg zero-initialized. |
| 1705 |
// Assumes one-char overread is ok. |
| 1706 |
uint32_t IsInfStr(const char* ss, uint32_t slen, uint32_t* is_neg_ptr); |
| 1707 |
|
| 1708 |
// This only accepts explicit infinity; out-of-range values are not converted |
| 1709 |
// to infinity. |
| 1710 |
HEADER_INLINE BoolErr ScanFloatAllowInf(const char* ss, float* valp) { |
| 1711 |
double dxx; |
| 1712 |
if (!ScantokDouble(ss, &dxx)) { |
| 1713 |
uint32_t is_neg = 0; |
| 1714 |
if (likely(IsInfStr(ss, strlen_se(ss), &is_neg))) { |
| 1715 |
// INFINITY is usually a float, but apparently not in w64-mingw32-g++ |
| 1716 |
// case? |
| 1717 |
*valp = is_neg? S_CAST(float, -INFINITY) : S_CAST(float, INFINITY); |
| 1718 |
return 0; |
| 1719 |
} else { |
| 1720 |
return 1; |
| 1721 |
} |
| 1722 |
} |
| 1723 |
if (unlikely(fabs(dxx) > 3.4028235677973362e38)) { |
| 1724 |
return 1; |
| 1725 |
} |
| 1726 |
*valp = S_CAST(float, dxx); |
| 1727 |
return 0; |
| 1728 |
} |
| 1729 |
|
| 1730 |
|
| 1731 |
HEADER_INLINE CXXCONST_CP AdvToDelimOrEnd(const char* str_iter, const char* str_end, char delim) { |
| 1732 |
CXXCONST_CP memchr_result = S_CAST(CXXCONST_CP, memchr(str_iter, delim, str_end - str_iter)); |
| 1733 |
if (memchr_result) { |
| 1734 |
return memchr_result; |
| 1735 |
} |
| 1736 |
return S_CAST(CXXCONST_CP, str_end); |
| 1737 |
} |
| 1738 |
|
| 1739 |
// tried memchr_likely_short(), not worth it |
| 1740 |
|
| 1741 |
#ifdef __cplusplus |
| 1742 |
HEADER_INLINE char* AdvToDelimOrEnd(char* str_iter, char* str_end, char needle) { |
| 1743 |
return const_cast<char*>(AdvToDelimOrEnd(const_cast<const char*>(str_iter), const_cast<const char*>(str_end), needle)); |
| 1744 |
} |
| 1745 |
#endif |
| 1746 |
|
| 1747 |
// memrchr() not available on some platforms. This implementation also |
| 1748 |
// includes a tweak which trades off a bit of performance around length 35 for |
| 1749 |
// substantially better performance on the longer lines often seen in e.g. VCF |
| 1750 |
// files, so we don't normally use the base implementation when it's available, |
| 1751 |
// hence the initial capital letter. |
| 1752 |
#ifdef __LP64__ |
| 1753 |
CXXCONST_CP Memrchr(const char* str_start, char needle, uintptr_t slen); |
| 1754 |
|
| 1755 |
CXXCONST_CP LastSpaceOrEoln(const char* str_start, uintptr_t slen); |
| 1756 |
#else // !__LP64__ |
| 1757 |
HEADER_INLINE CXXCONST_CP Memrchr(const char* str_start, char needle, uintptr_t slen) { |
| 1758 |
# ifdef _GNU_SOURCE |
| 1759 |
return S_CAST(CXXCONST_CP, memrchr(str_start, ctou32(needle), slen)); |
| 1760 |
# else // !_GNU_SOURCE |
| 1761 |
// Could check one word at a time for not-that-small slen. |
| 1762 |
for (uintptr_t pos = slen; pos; ) { |
| 1763 |
if (str_start[--pos] == needle) { |
| 1764 |
return S_CAST(CXXCONST_CP, &(str_start[pos])); |
| 1765 |
} |
| 1766 |
} |
| 1767 |
return nullptr; |
| 1768 |
# endif // !_GNU_SOURCE |
| 1769 |
} |
| 1770 |
|
| 1771 |
HEADER_INLINE CXXCONST_CP LastSpaceOrEoln(const char* str_start, uintptr_t slen) { |
| 1772 |
for (uintptr_t pos = slen; pos; ) { |
| 1773 |
if (ctou32(str_start[--pos]) <= 32) { |
| 1774 |
return S_CAST(CXXCONST_CP, &(str_start[pos])); |
| 1775 |
} |
| 1776 |
} |
| 1777 |
return nullptr; |
| 1778 |
} |
| 1779 |
#endif // !__LP64__ |
| 1780 |
|
| 1781 |
#ifdef __cplusplus |
| 1782 |
HEADER_INLINE char* Memrchr(char* str_start, char needle, uintptr_t slen) { |
| 1783 |
return const_cast<char*>(Memrchr(const_cast<const char*>(str_start), needle, slen)); |
| 1784 |
} |
| 1785 |
|
| 1786 |
HEADER_INLINE char* LastSpaceOrEoln(char* str_start, uintptr_t slen) { |
| 1787 |
return const_cast<char*>(LastSpaceOrEoln(const_cast<const char*>(str_start), slen)); |
| 1788 |
} |
| 1789 |
#endif |
| 1790 |
|
| 1791 |
// void ReplaceAllInstances(char old_char, char new_char, uint32_t slen, char* dst); |
| 1792 |
|
| 1793 |
void TabsToSpaces(char* ss_iter); |
| 1794 |
|
| 1795 |
// Errors out if new_char is already present. |
| 1796 |
BoolErr ReplaceCharAdvChecked(char old_char, char new_char, char** str_ptr); |
| 1797 |
|
| 1798 |
#ifdef __cplusplus |
| 1799 |
} // namespace plink2 |
| 1800 |
#endif |
| 1801 |
|
| 1802 |
#endif // __PLINK2_STRING_H__ |