bwstring.c (71ec05a21257e159f40d54e26ad0011bb19b5134) | bwstring.c (d053fb22f6d3b6bb0f4e47e4507fefc20cbe0e32) |
---|---|
1/*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (C) 2009 Gabor Kovesdan <gabor@FreeBSD.org> 5 * Copyright (C) 2012 Oleg Moskalenko <mom040267@gmail.com> 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without --- 99 unchanged lines hidden (view full) --- 108} 109 110/* 111 * Compare two wide-character strings 112 */ 113static int 114wide_str_coll(const wchar_t *s1, const wchar_t *s2) 115{ | 1/*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (C) 2009 Gabor Kovesdan <gabor@FreeBSD.org> 5 * Copyright (C) 2012 Oleg Moskalenko <mom040267@gmail.com> 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without --- 99 unchanged lines hidden (view full) --- 108} 109 110/* 111 * Compare two wide-character strings 112 */ 113static int 114wide_str_coll(const wchar_t *s1, const wchar_t *s2) 115{ |
116 int ret = 0; | 116 int ret; |
117 118 errno = 0; 119 ret = wcscoll(s1, s2); 120 if (errno == EILSEQ) { 121 errno = 0; 122 ret = wcscmp(s1, s2); 123 if (errno != 0) { 124 for (size_t i = 0; ; ++i) { --- 14 unchanged lines hidden (view full) --- 139 140/* counterparts of wcs functions */ 141 142void 143bwsprintf(FILE *f, struct bwstring *bws, const char *prefix, const char *suffix) 144{ 145 146 if (mb_cur_max == 1) | 117 118 errno = 0; 119 ret = wcscoll(s1, s2); 120 if (errno == EILSEQ) { 121 errno = 0; 122 ret = wcscmp(s1, s2); 123 if (errno != 0) { 124 for (size_t i = 0; ; ++i) { --- 14 unchanged lines hidden (view full) --- 139 140/* counterparts of wcs functions */ 141 142void 143bwsprintf(FILE *f, struct bwstring *bws, const char *prefix, const char *suffix) 144{ 145 146 if (mb_cur_max == 1) |
147 fprintf(f, "%s%s%s", prefix, bws->data.cstr, suffix); | 147 fprintf(f, "%s%s%s", prefix, bws->cdata.str, suffix); |
148 else | 148 else |
149 fprintf(f, "%s%S%s", prefix, bws->data.wstr, suffix); | 149 fprintf(f, "%s%S%s", prefix, bws->wdata.str, suffix); |
150} 151 152const void* bwsrawdata(const struct bwstring *bws) 153{ 154 | 150} 151 152const void* bwsrawdata(const struct bwstring *bws) 153{ 154 |
155 return (&(bws->data)); | 155 return (&(bws->wdata)); |
156} 157 158size_t bwsrawlen(const struct bwstring *bws) 159{ 160 | 156} 157 158size_t bwsrawlen(const struct bwstring *bws) 159{ 160 |
161 return ((mb_cur_max == 1) ? bws->len : SIZEOF_WCHAR_STRING(bws->len)); | 161 return ((mb_cur_max == 1) ? bws->cdata.len : 162 SIZEOF_WCHAR_STRING(bws->wdata.len)); |
162} 163 164size_t 165bws_memsize(const struct bwstring *bws) 166{ 167 | 163} 164 165size_t 166bws_memsize(const struct bwstring *bws) 167{ 168 |
168 return ((mb_cur_max == 1) ? (bws->len + 2 + sizeof(struct bwstring)) : 169 (SIZEOF_WCHAR_STRING(bws->len + 1) + sizeof(struct bwstring))); | 169 return ((mb_cur_max == 1) ? 170 (bws->cdata.len + 2 + sizeof(struct bwstring)) : 171 (SIZEOF_WCHAR_STRING(bws->wdata.len + 1) + sizeof(struct bwstring))); |
170} 171 172void 173bws_setlen(struct bwstring *bws, size_t newlen) 174{ 175 | 172} 173 174void 175bws_setlen(struct bwstring *bws, size_t newlen) 176{ 177 |
176 if (bws && newlen != bws->len && newlen <= bws->len) { 177 bws->len = newlen; 178 if (mb_cur_max == 1) 179 bws->data.cstr[newlen] = '\0'; 180 else 181 bws->data.wstr[newlen] = L'\0'; | 178 if (mb_cur_max == 1 && bws && newlen != bws->cdata.len && 179 newlen <= bws->cdata.len) { 180 bws->cdata.len = newlen; 181 bws->cdata.str[newlen] = '\0'; 182 } else if (bws && newlen != bws->wdata.len && newlen <= bws->wdata.len) { 183 bws->wdata.len = newlen; 184 bws->wdata.str[newlen] = L'\0'; |
182 } 183} 184 185/* 186 * Allocate a new binary string of specified size 187 */ 188struct bwstring * 189bwsalloc(size_t sz) 190{ 191 struct bwstring *ret; 192 | 185 } 186} 187 188/* 189 * Allocate a new binary string of specified size 190 */ 191struct bwstring * 192bwsalloc(size_t sz) 193{ 194 struct bwstring *ret; 195 |
193 if (mb_cur_max == 1) | 196 if (mb_cur_max == 1) { |
194 ret = sort_malloc(sizeof(struct bwstring) + 1 + sz); | 197 ret = sort_malloc(sizeof(struct bwstring) + 1 + sz); |
195 else 196 ret = sort_malloc(sizeof(struct bwstring) + 197 SIZEOF_WCHAR_STRING(sz + 1)); 198 ret->len = sz; | 198 ret->cdata.len = sz; 199 ret->cdata.str[sz] = '\0'; 200 } else { 201 ret = sort_malloc( 202 sizeof(struct bwstring) + SIZEOF_WCHAR_STRING(sz + 1)); 203 ret->wdata.len = sz; 204 ret->wdata.str[sz] = L'\0'; 205 } |
199 | 206 |
200 if (mb_cur_max == 1) 201 ret->data.cstr[ret->len] = '\0'; 202 else 203 ret->data.wstr[ret->len] = L'\0'; 204 | |
205 return (ret); 206} 207 208/* 209 * Create a copy of binary string. 210 * New string size equals the length of the old string. 211 */ 212struct bwstring * 213bwsdup(const struct bwstring *s) 214{ 215 216 if (s == NULL) 217 return (NULL); 218 else { | 207 return (ret); 208} 209 210/* 211 * Create a copy of binary string. 212 * New string size equals the length of the old string. 213 */ 214struct bwstring * 215bwsdup(const struct bwstring *s) 216{ 217 218 if (s == NULL) 219 return (NULL); 220 else { |
219 struct bwstring *ret = bwsalloc(s->len); | 221 struct bwstring *ret = bwsalloc(BWSLEN(s)); |
220 221 if (mb_cur_max == 1) | 222 223 if (mb_cur_max == 1) |
222 memcpy(ret->data.cstr, s->data.cstr, (s->len)); | 224 memcpy(ret->cdata.str, s->cdata.str, (s->cdata.len)); |
223 else | 225 else |
224 memcpy(ret->data.wstr, s->data.wstr, 225 SIZEOF_WCHAR_STRING(s->len)); | 226 memcpy(ret->wdata.str, s->wdata.str, 227 SIZEOF_WCHAR_STRING(s->wdata.len)); |
226 227 return (ret); 228 } 229} 230 231/* 232 * Create a new binary string from a wide character buffer. 233 */ --- 5 unchanged lines hidden (view full) --- 239 return ((len == 0) ? bwsalloc(0) : NULL); 240 else { 241 struct bwstring *ret; 242 243 ret = bwsalloc(len); 244 245 if (mb_cur_max == 1) 246 for (size_t i = 0; i < len; ++i) | 228 229 return (ret); 230 } 231} 232 233/* 234 * Create a new binary string from a wide character buffer. 235 */ --- 5 unchanged lines hidden (view full) --- 241 return ((len == 0) ? bwsalloc(0) : NULL); 242 else { 243 struct bwstring *ret; 244 245 ret = bwsalloc(len); 246 247 if (mb_cur_max == 1) 248 for (size_t i = 0; i < len; ++i) |
247 ret->data.cstr[i] = (unsigned char) str[i]; | 249 ret->cdata.str[i] = (char)str[i]; |
248 else | 250 else |
249 memcpy(ret->data.wstr, str, SIZEOF_WCHAR_STRING(len)); | 251 memcpy(ret->wdata.str, str, SIZEOF_WCHAR_STRING(len)); |
250 251 return (ret); 252 } 253} 254 255/* 256 * Create a new binary string from a raw binary buffer. 257 */ 258struct bwstring * 259bwscsbdup(const unsigned char *str, size_t len) 260{ 261 struct bwstring *ret; 262 263 ret = bwsalloc(len); 264 265 if (str) { 266 if (mb_cur_max == 1) | 252 253 return (ret); 254 } 255} 256 257/* 258 * Create a new binary string from a raw binary buffer. 259 */ 260struct bwstring * 261bwscsbdup(const unsigned char *str, size_t len) 262{ 263 struct bwstring *ret; 264 265 ret = bwsalloc(len); 266 267 if (str) { 268 if (mb_cur_max == 1) |
267 memcpy(ret->data.cstr, str, len); | 269 memcpy(ret->cdata.str, str, len); |
268 else { 269 mbstate_t mbs; 270 const char *s; 271 size_t charlen, chars, cptr; 272 273 chars = 0; 274 cptr = 0; 275 s = (const char *) str; --- 7 unchanged lines hidden (view full) --- 283 n = len - cptr; 284 charlen = mbrlen(s + cptr, n, &mbs); 285 switch (charlen) { 286 case 0: 287 /* FALLTHROUGH */ 288 case (size_t) -1: 289 /* FALLTHROUGH */ 290 case (size_t) -2: | 270 else { 271 mbstate_t mbs; 272 const char *s; 273 size_t charlen, chars, cptr; 274 275 chars = 0; 276 cptr = 0; 277 s = (const char *) str; --- 7 unchanged lines hidden (view full) --- 285 n = len - cptr; 286 charlen = mbrlen(s + cptr, n, &mbs); 287 switch (charlen) { 288 case 0: 289 /* FALLTHROUGH */ 290 case (size_t) -1: 291 /* FALLTHROUGH */ 292 case (size_t) -2: |
291 ret->data.wstr[chars++] = | 293 ret->wdata.str[chars++] = |
292 (unsigned char) s[cptr]; 293 ++cptr; 294 break; 295 default: | 294 (unsigned char) s[cptr]; 295 ++cptr; 296 break; 297 default: |
296 n = mbrtowc(ret->data.wstr + (chars++), | 298 n = mbrtowc(ret->wdata.str + (chars++), |
297 s + cptr, charlen, &mbs); 298 if ((n == (size_t)-1) || (n == (size_t)-2)) 299 /* NOTREACHED */ 300 err(2, "mbrtowc error"); 301 cptr += charlen; 302 } 303 } 304 | 299 s + cptr, charlen, &mbs); 300 if ((n == (size_t)-1) || (n == (size_t)-2)) 301 /* NOTREACHED */ 302 err(2, "mbrtowc error"); 303 cptr += charlen; 304 } 305 } 306 |
305 ret->len = chars; 306 ret->data.wstr[ret->len] = L'\0'; | 307 ret->wdata.len = chars; 308 ret->wdata.str[ret->wdata.len] = L'\0'; |
307 } 308 } 309 return (ret); 310} 311 312/* 313 * De-allocate object memory 314 */ --- 8 unchanged lines hidden (view full) --- 323/* 324 * Copy content of src binary string to dst. 325 * If the capacity of the dst string is not sufficient, 326 * then the data is truncated. 327 */ 328size_t 329bwscpy(struct bwstring *dst, const struct bwstring *src) 330{ | 309 } 310 } 311 return (ret); 312} 313 314/* 315 * De-allocate object memory 316 */ --- 8 unchanged lines hidden (view full) --- 325/* 326 * Copy content of src binary string to dst. 327 * If the capacity of the dst string is not sufficient, 328 * then the data is truncated. 329 */ 330size_t 331bwscpy(struct bwstring *dst, const struct bwstring *src) 332{ |
331 size_t nums = src->len; | 333 size_t nums = BWSLEN(src); |
332 | 334 |
333 if (nums > dst->len) 334 nums = dst->len; 335 dst->len = nums; | 335 if (nums > BWSLEN(dst)) 336 nums = BWSLEN(dst); |
336 337 if (mb_cur_max == 1) { | 337 338 if (mb_cur_max == 1) { |
338 memcpy(dst->data.cstr, src->data.cstr, nums); 339 dst->data.cstr[dst->len] = '\0'; | 339 memcpy(dst->cdata.str, src->cdata.str, nums); 340 dst->cdata.len = nums; 341 dst->cdata.str[dst->cdata.len] = '\0'; |
340 } else { | 342 } else { |
341 memcpy(dst->data.wstr, src->data.wstr, 342 SIZEOF_WCHAR_STRING(nums + 1)); 343 dst->data.wstr[dst->len] = L'\0'; | 343 memcpy(dst->wdata.str, src->wdata.str, 344 SIZEOF_WCHAR_STRING(nums)); 345 dst->wdata.len = nums; 346 dst->wdata.str[nums] = L'\0'; |
344 } 345 346 return (nums); 347} 348 349/* 350 * Copy content of src binary string to dst, 351 * with specified number of symbols to be copied. 352 * If the capacity of the dst string is not sufficient, 353 * then the data is truncated. 354 */ 355struct bwstring * 356bwsncpy(struct bwstring *dst, const struct bwstring *src, size_t size) 357{ | 347 } 348 349 return (nums); 350} 351 352/* 353 * Copy content of src binary string to dst, 354 * with specified number of symbols to be copied. 355 * If the capacity of the dst string is not sufficient, 356 * then the data is truncated. 357 */ 358struct bwstring * 359bwsncpy(struct bwstring *dst, const struct bwstring *src, size_t size) 360{ |
358 size_t nums = src->len; | 361 size_t nums = BWSLEN(src); |
359 | 362 |
360 if (nums > dst->len) 361 nums = dst->len; | 363 if (nums > BWSLEN(dst)) 364 nums = BWSLEN(dst); |
362 if (nums > size) 363 nums = size; | 365 if (nums > size) 366 nums = size; |
364 dst->len = nums; | |
365 366 if (mb_cur_max == 1) { | 367 368 if (mb_cur_max == 1) { |
367 memcpy(dst->data.cstr, src->data.cstr, nums); 368 dst->data.cstr[dst->len] = '\0'; | 369 memcpy(dst->cdata.str, src->cdata.str, nums); 370 dst->cdata.len = nums; 371 dst->cdata.str[nums] = '\0'; |
369 } else { | 372 } else { |
370 memcpy(dst->data.wstr, src->data.wstr, 371 SIZEOF_WCHAR_STRING(nums + 1)); 372 dst->data.wstr[dst->len] = L'\0'; | 373 memcpy(dst->wdata.str, src->wdata.str, 374 SIZEOF_WCHAR_STRING(nums)); 375 dst->wdata.len = nums; 376 dst->wdata.str[nums] = L'\0'; |
373 } 374 375 return (dst); 376} 377 378/* 379 * Copy content of src binary string to dst, 380 * with specified number of symbols to be copied. 381 * An offset value can be specified, from the start of src string. 382 * If the capacity of the dst string is not sufficient, 383 * then the data is truncated. 384 */ 385struct bwstring * 386bwsnocpy(struct bwstring *dst, const struct bwstring *src, size_t offset, 387 size_t size) 388{ 389 | 377 } 378 379 return (dst); 380} 381 382/* 383 * Copy content of src binary string to dst, 384 * with specified number of symbols to be copied. 385 * An offset value can be specified, from the start of src string. 386 * If the capacity of the dst string is not sufficient, 387 * then the data is truncated. 388 */ 389struct bwstring * 390bwsnocpy(struct bwstring *dst, const struct bwstring *src, size_t offset, 391 size_t size) 392{ 393 |
390 if (offset >= src->len) { 391 dst->data.wstr[0] = 0; 392 dst->len = 0; | 394 if (offset >= BWSLEN(src)) { 395 bws_setlen(dst, 0); |
393 } else { | 396 } else { |
394 size_t nums = src->len - offset; | 397 size_t nums = BWSLEN(src) - offset; |
395 | 398 |
396 if (nums > dst->len) 397 nums = dst->len; | 399 if (nums > BWSLEN(dst)) 400 nums = BWSLEN(dst); |
398 if (nums > size) 399 nums = size; | 401 if (nums > size) 402 nums = size; |
400 dst->len = nums; | |
401 if (mb_cur_max == 1) { | 403 if (mb_cur_max == 1) { |
402 memcpy(dst->data.cstr, src->data.cstr + offset, 403 (nums)); 404 dst->data.cstr[dst->len] = '\0'; | 404 memcpy(dst->cdata.str, src->cdata.str + offset, nums); 405 dst->cdata.len = nums; 406 dst->cdata.str[nums] = '\0'; |
405 } else { | 407 } else { |
406 memcpy(dst->data.wstr, src->data.wstr + offset, | 408 memcpy(dst->wdata.str, src->wdata.str + offset, |
407 SIZEOF_WCHAR_STRING(nums)); | 409 SIZEOF_WCHAR_STRING(nums)); |
408 dst->data.wstr[dst->len] = L'\0'; | 410 dst->wdata.len = nums; 411 dst->wdata.str[nums] = L'\0'; |
409 } 410 } 411 return (dst); 412} 413 414/* 415 * Write binary string to the file. 416 * The output is ended either with '\n' (nl == true) 417 * or '\0' (nl == false). 418 */ 419size_t 420bwsfwrite(struct bwstring *bws, FILE *f, bool zero_ended) 421{ 422 423 if (mb_cur_max == 1) { | 412 } 413 } 414 return (dst); 415} 416 417/* 418 * Write binary string to the file. 419 * The output is ended either with '\n' (nl == true) 420 * or '\0' (nl == false). 421 */ 422size_t 423bwsfwrite(struct bwstring *bws, FILE *f, bool zero_ended) 424{ 425 426 if (mb_cur_max == 1) { |
424 size_t len = bws->len; | 427 size_t len = bws->cdata.len; |
425 426 if (!zero_ended) { | 428 429 if (!zero_ended) { |
427 bws->data.cstr[len] = '\n'; | 430 bws->cdata.str[len] = '\n'; |
428 | 431 |
429 if (fwrite(bws->data.cstr, len + 1, 1, f) < 1) | 432 if (fwrite(bws->cdata.str, len + 1, 1, f) < 1) |
430 err(2, NULL); 431 | 433 err(2, NULL); 434 |
432 bws->data.cstr[len] = '\0'; 433 } else if (fwrite(bws->data.cstr, len + 1, 1, f) < 1) | 435 bws->cdata.str[len] = '\0'; 436 } else if (fwrite(bws->cdata.str, len + 1, 1, f) < 1) |
434 err(2, NULL); 435 436 return (len + 1); 437 438 } else { 439 wchar_t eols; 440 size_t printed = 0; 441 442 eols = zero_ended ? btowc('\0') : btowc('\n'); 443 444 while (printed < BWSLEN(bws)) { | 437 err(2, NULL); 438 439 return (len + 1); 440 441 } else { 442 wchar_t eols; 443 size_t printed = 0; 444 445 eols = zero_ended ? btowc('\0') : btowc('\n'); 446 447 while (printed < BWSLEN(bws)) { |
445 const wchar_t *s = bws->data.wstr + printed; | 448 const wchar_t *s = bws->wdata.str + printed; |
446 447 if (*s == L'\0') { 448 int nums; 449 450 nums = fwprintf(f, L"%lc", *s); 451 452 if (nums != 1) 453 err(2, NULL); --- 49 unchanged lines hidden (view full) --- 503 if (!feof(f)) 504 err(2, NULL); 505 return (NULL); 506 } 507 if (*len > 0) { 508 if (ret[*len - 1] == '\n') 509 --(*len); 510 } | 449 450 if (*s == L'\0') { 451 int nums; 452 453 nums = fwprintf(f, L"%lc", *s); 454 455 if (nums != 1) 456 err(2, NULL); --- 49 unchanged lines hidden (view full) --- 506 if (!feof(f)) 507 err(2, NULL); 508 return (NULL); 509 } 510 if (*len > 0) { 511 if (ret[*len - 1] == '\n') 512 --(*len); 513 } |
511 return (bwscsbdup((unsigned char*)ret, *len)); | 514 return (bwscsbdup((unsigned char *)ret, *len)); |
512 513 } else { 514 *len = 0; 515 516 if (feof(f)) 517 return (NULL); 518 519 if (2 >= rb->fgetwln_z_buffer_size) { --- 23 unchanged lines hidden (view full) --- 543 SIZEOF_WCHAR_STRING(rb->fgetwln_z_buffer_size)); 544 } 545 546 rb->fgetwln_z_buffer[*len] = c; 547 rb->fgetwln_z_buffer[++(*len)] = 0; 548 } 549 else 550 while (!feof(f)) { | 515 516 } else { 517 *len = 0; 518 519 if (feof(f)) 520 return (NULL); 521 522 if (2 >= rb->fgetwln_z_buffer_size) { --- 23 unchanged lines hidden (view full) --- 546 SIZEOF_WCHAR_STRING(rb->fgetwln_z_buffer_size)); 547 } 548 549 rb->fgetwln_z_buffer[*len] = c; 550 rb->fgetwln_z_buffer[++(*len)] = 0; 551 } 552 else 553 while (!feof(f)) { |
551 wint_t c = 0; | 554 wint_t c; |
552 553 c = fgetwc(f); 554 555 if (c == WEOF) { 556 if (*len == 0) 557 return (NULL); 558 goto line_read_done; 559 } --- 16 unchanged lines hidden (view full) --- 576 } 577} 578 579int 580bwsncmp(const struct bwstring *bws1, const struct bwstring *bws2, 581 size_t offset, size_t len) 582{ 583 size_t cmp_len, len1, len2; | 555 556 c = fgetwc(f); 557 558 if (c == WEOF) { 559 if (*len == 0) 560 return (NULL); 561 goto line_read_done; 562 } --- 16 unchanged lines hidden (view full) --- 579 } 580} 581 582int 583bwsncmp(const struct bwstring *bws1, const struct bwstring *bws2, 584 size_t offset, size_t len) 585{ 586 size_t cmp_len, len1, len2; |
584 int res = 0; | 587 int res; |
585 | 588 |
586 len1 = bws1->len; 587 len2 = bws2->len; | 589 len1 = BWSLEN(bws1); 590 len2 = BWSLEN(bws2); |
588 589 if (len1 <= offset) { 590 return ((len2 <= offset) ? 0 : -1); 591 } else { 592 if (len2 <= offset) 593 return (+1); 594 else { 595 len1 -= offset; 596 len2 -= offset; 597 598 cmp_len = len1; 599 600 if (len2 < cmp_len) 601 cmp_len = len2; 602 603 if (len < cmp_len) 604 cmp_len = len; 605 606 if (mb_cur_max == 1) { | 591 592 if (len1 <= offset) { 593 return ((len2 <= offset) ? 0 : -1); 594 } else { 595 if (len2 <= offset) 596 return (+1); 597 else { 598 len1 -= offset; 599 len2 -= offset; 600 601 cmp_len = len1; 602 603 if (len2 < cmp_len) 604 cmp_len = len2; 605 606 if (len < cmp_len) 607 cmp_len = len; 608 609 if (mb_cur_max == 1) { |
607 const unsigned char *s1, *s2; | 610 const char *s1, *s2; |
608 | 611 |
609 s1 = bws1->data.cstr + offset; 610 s2 = bws2->data.cstr + offset; | 612 s1 = bws1->cdata.str + offset; 613 s2 = bws2->cdata.str + offset; |
611 612 res = memcmp(s1, s2, cmp_len); 613 614 } else { 615 const wchar_t *s1, *s2; 616 | 614 615 res = memcmp(s1, s2, cmp_len); 616 617 } else { 618 const wchar_t *s1, *s2; 619 |
617 s1 = bws1->data.wstr + offset; 618 s2 = bws2->data.wstr + offset; | 620 s1 = bws1->wdata.str + offset; 621 s2 = bws2->wdata.str + offset; |
619 620 res = memcmp(s1, s2, SIZEOF_WCHAR_STRING(cmp_len)); 621 } 622 } 623 } 624 625 if (res == 0) { 626 if (len1 < cmp_len && len1 < len2) --- 6 unchanged lines hidden (view full) --- 633} 634 635int 636bwscmp(const struct bwstring *bws1, const struct bwstring *bws2, size_t offset) 637{ 638 size_t len1, len2, cmp_len; 639 int res; 640 | 622 623 res = memcmp(s1, s2, SIZEOF_WCHAR_STRING(cmp_len)); 624 } 625 } 626 } 627 628 if (res == 0) { 629 if (len1 < cmp_len && len1 < len2) --- 6 unchanged lines hidden (view full) --- 636} 637 638int 639bwscmp(const struct bwstring *bws1, const struct bwstring *bws2, size_t offset) 640{ 641 size_t len1, len2, cmp_len; 642 int res; 643 |
641 len1 = bws1->len; 642 len2 = bws2->len; | 644 len1 = BWSLEN(bws1); 645 len2 = BWSLEN(bws2); |
643 644 len1 -= offset; 645 len2 -= offset; 646 647 cmp_len = len1; 648 649 if (len2 < cmp_len) 650 cmp_len = len2; --- 9 unchanged lines hidden (view full) --- 660 661 return (res); 662} 663 664int 665bws_iterator_cmp(bwstring_iterator iter1, bwstring_iterator iter2, size_t len) 666{ 667 wchar_t c1, c2; | 646 647 len1 -= offset; 648 len2 -= offset; 649 650 cmp_len = len1; 651 652 if (len2 < cmp_len) 653 cmp_len = len2; --- 9 unchanged lines hidden (view full) --- 663 664 return (res); 665} 666 667int 668bws_iterator_cmp(bwstring_iterator iter1, bwstring_iterator iter2, size_t len) 669{ 670 wchar_t c1, c2; |
668 size_t i = 0; | 671 size_t i; |
669 670 for (i = 0; i < len; ++i) { 671 c1 = bws_get_iter_value(iter1); 672 c2 = bws_get_iter_value(iter2); 673 if (c1 != c2) 674 return (c1 - c2); 675 iter1 = bws_iterator_inc(iter1, 1); 676 iter2 = bws_iterator_inc(iter2, 1); 677 } 678 679 return (0); 680} 681 682int 683bwscoll(const struct bwstring *bws1, const struct bwstring *bws2, size_t offset) 684{ 685 size_t len1, len2; 686 | 672 673 for (i = 0; i < len; ++i) { 674 c1 = bws_get_iter_value(iter1); 675 c2 = bws_get_iter_value(iter2); 676 if (c1 != c2) 677 return (c1 - c2); 678 iter1 = bws_iterator_inc(iter1, 1); 679 iter2 = bws_iterator_inc(iter2, 1); 680 } 681 682 return (0); 683} 684 685int 686bwscoll(const struct bwstring *bws1, const struct bwstring *bws2, size_t offset) 687{ 688 size_t len1, len2; 689 |
687 len1 = bws1->len; 688 len2 = bws2->len; | 690 len1 = BWSLEN(bws1); 691 len2 = BWSLEN(bws2); |
689 690 if (len1 <= offset) 691 return ((len2 <= offset) ? 0 : -1); 692 else { 693 if (len2 <= offset) 694 return (+1); 695 else { 696 len1 -= offset; 697 len2 -= offset; 698 699 if (mb_cur_max == 1) { | 692 693 if (len1 <= offset) 694 return ((len2 <= offset) ? 0 : -1); 695 else { 696 if (len2 <= offset) 697 return (+1); 698 else { 699 len1 -= offset; 700 len2 -= offset; 701 702 if (mb_cur_max == 1) { |
700 const unsigned char *s1, *s2; | 703 const char *s1, *s2; |
701 | 704 |
702 s1 = bws1->data.cstr + offset; 703 s2 = bws2->data.cstr + offset; | 705 s1 = bws1->cdata.str + offset; 706 s2 = bws2->cdata.str + offset; |
704 705 if (byte_sort) { | 707 708 if (byte_sort) { |
706 int res = 0; | 709 int res; |
707 708 if (len1 > len2) { 709 res = memcmp(s1, s2, len2); 710 if (!res) 711 res = +1; 712 } else if (len1 < len2) { 713 res = memcmp(s1, s2, len1); 714 if (!res) 715 res = -1; 716 } else 717 res = memcmp(s1, s2, len1); 718 719 return (res); 720 721 } else { | 710 711 if (len1 > len2) { 712 res = memcmp(s1, s2, len2); 713 if (!res) 714 res = +1; 715 } else if (len1 < len2) { 716 res = memcmp(s1, s2, len1); 717 if (!res) 718 res = -1; 719 } else 720 res = memcmp(s1, s2, len1); 721 722 return (res); 723 724 } else { |
722 int res = 0; | 725 int res; |
723 size_t i, maxlen; 724 725 i = 0; 726 maxlen = len1; 727 728 if (maxlen > len2) 729 maxlen = len2; 730 --- 44 unchanged lines hidden (view full) --- 775 else if (len1 > len2) 776 return (+1); 777 778 return (0); 779 } 780 } else { 781 const wchar_t *s1, *s2; 782 size_t i, maxlen; | 726 size_t i, maxlen; 727 728 i = 0; 729 maxlen = len1; 730 731 if (maxlen > len2) 732 maxlen = len2; 733 --- 44 unchanged lines hidden (view full) --- 778 else if (len1 > len2) 779 return (+1); 780 781 return (0); 782 } 783 } else { 784 const wchar_t *s1, *s2; 785 size_t i, maxlen; |
783 int res = 0; | 786 int res; |
784 | 787 |
785 s1 = bws1->data.wstr + offset; 786 s2 = bws2->data.wstr + offset; | 788 s1 = bws1->wdata.str + offset; 789 s2 = bws2->wdata.str + offset; |
787 788 i = 0; 789 maxlen = len1; 790 791 if (maxlen > len2) 792 maxlen = len2; 793 794 while (i < maxlen) { --- 50 unchanged lines hidden (view full) --- 845} 846 847/* 848 * Correction of the system API 849 */ 850double 851bwstod(struct bwstring *s0, bool *empty) 852{ | 790 791 i = 0; 792 maxlen = len1; 793 794 if (maxlen > len2) 795 maxlen = len2; 796 797 while (i < maxlen) { --- 50 unchanged lines hidden (view full) --- 848} 849 850/* 851 * Correction of the system API 852 */ 853double 854bwstod(struct bwstring *s0, bool *empty) 855{ |
853 double ret = 0; | 856 double ret; |
854 855 if (mb_cur_max == 1) { | 857 858 if (mb_cur_max == 1) { |
856 unsigned char *end, *s; | 859 char *end, *s; |
857 char *ep; 858 | 860 char *ep; 861 |
859 s = s0->data.cstr; 860 end = s + s0->len; | 862 s = s0->cdata.str; 863 end = s + s0->cdata.len; |
861 ep = NULL; 862 863 while (isblank(*s) && s < end) 864 ++s; 865 866 if (!isprint(*s)) { 867 *empty = true; 868 return (0); 869 } 870 871 ret = strtod((char*)s, &ep); | 864 ep = NULL; 865 866 while (isblank(*s) && s < end) 867 ++s; 868 869 if (!isprint(*s)) { 870 *empty = true; 871 return (0); 872 } 873 874 ret = strtod((char*)s, &ep); |
872 if ((unsigned char*) ep == s) { | 875 if (ep == s) { |
873 *empty = true; 874 return (0); 875 } 876 } else { 877 wchar_t *end, *ep, *s; 878 | 876 *empty = true; 877 return (0); 878 } 879 } else { 880 wchar_t *end, *ep, *s; 881 |
879 s = s0->data.wstr; 880 end = s + s0->len; | 882 s = s0->wdata.str; 883 end = s + s0->wdata.len; |
881 ep = NULL; 882 883 while (iswblank(*s) && s < end) 884 ++s; 885 886 if (!iswprint(*s)) { 887 *empty = true; 888 return (0); --- 16 unchanged lines hidden (view full) --- 905 * while if there is no match, it just return -1. 906 */ 907 908int 909bws_month_score(const struct bwstring *s0) 910{ 911 912 if (mb_cur_max == 1) { | 884 ep = NULL; 885 886 while (iswblank(*s) && s < end) 887 ++s; 888 889 if (!iswprint(*s)) { 890 *empty = true; 891 return (0); --- 16 unchanged lines hidden (view full) --- 908 * while if there is no match, it just return -1. 909 */ 910 911int 912bws_month_score(const struct bwstring *s0) 913{ 914 915 if (mb_cur_max == 1) { |
913 const unsigned char *end, *s; | 916 const char *end, *s; |
914 | 917 |
915 s = s0->data.cstr; 916 end = s + s0->len; | 918 s = s0->cdata.str; 919 end = s + s0->cdata.len; |
917 918 while (isblank(*s) && s < end) 919 ++s; 920 921 for (int i = 11; i >= 0; --i) { 922 if (cmonths[i] && | 920 921 while (isblank(*s) && s < end) 922 ++s; 923 924 for (int i = 11; i >= 0; --i) { 925 if (cmonths[i] && |
923 (s == (unsigned char*)strstr((const char*)s, (char*)(cmonths[i])))) | 926 (s == strstr(s, cmonths[i]))) |
924 return (i); 925 } 926 927 } else { 928 const wchar_t *end, *s; 929 | 927 return (i); 928 } 929 930 } else { 931 const wchar_t *end, *s; 932 |
930 s = s0->data.wstr; 931 end = s + s0->len; | 933 s = s0->wdata.str; 934 end = s + s0->wdata.len; |
932 933 while (iswblank(*s) && s < end) 934 ++s; 935 936 for (int i = 11; i >= 0; --i) { 937 if (wmonths[i] && (s == wcsstr(s, wmonths[i]))) 938 return (i); 939 } --- 5 unchanged lines hidden (view full) --- 945/* 946 * Rips out leading blanks (-b). 947 */ 948struct bwstring * 949ignore_leading_blanks(struct bwstring *str) 950{ 951 952 if (mb_cur_max == 1) { | 935 936 while (iswblank(*s) && s < end) 937 ++s; 938 939 for (int i = 11; i >= 0; --i) { 940 if (wmonths[i] && (s == wcsstr(s, wmonths[i]))) 941 return (i); 942 } --- 5 unchanged lines hidden (view full) --- 948/* 949 * Rips out leading blanks (-b). 950 */ 951struct bwstring * 952ignore_leading_blanks(struct bwstring *str) 953{ 954 955 if (mb_cur_max == 1) { |
953 unsigned char *dst, *end, *src; | 956 char *dst, *end, *src; |
954 | 957 |
955 src = str->data.cstr; | 958 src = str->cdata.str; |
956 dst = src; | 959 dst = src; |
957 end = src + str->len; | 960 end = src + str->cdata.len; |
958 959 while (src < end && isblank(*src)) 960 ++src; 961 962 if (src != dst) { 963 size_t newlen; 964 965 newlen = BWSLEN(str) - (src - dst); 966 967 while (src < end) { 968 *dst = *src; 969 ++dst; 970 ++src; 971 } 972 bws_setlen(str, newlen); 973 } 974 } else { 975 wchar_t *dst, *end, *src; 976 | 961 962 while (src < end && isblank(*src)) 963 ++src; 964 965 if (src != dst) { 966 size_t newlen; 967 968 newlen = BWSLEN(str) - (src - dst); 969 970 while (src < end) { 971 *dst = *src; 972 ++dst; 973 ++src; 974 } 975 bws_setlen(str, newlen); 976 } 977 } else { 978 wchar_t *dst, *end, *src; 979 |
977 src = str->data.wstr; | 980 src = str->wdata.str; |
978 dst = src; | 981 dst = src; |
979 end = src + str->len; | 982 end = src + str->wdata.len; |
980 981 while (src < end && iswblank(*src)) 982 ++src; 983 984 if (src != dst) { 985 986 size_t newlen = BWSLEN(str) - (src - dst); 987 --- 10 unchanged lines hidden (view full) --- 998} 999 1000/* 1001 * Rips out nonprinting characters (-i). 1002 */ 1003struct bwstring * 1004ignore_nonprinting(struct bwstring *str) 1005{ | 983 984 while (src < end && iswblank(*src)) 985 ++src; 986 987 if (src != dst) { 988 989 size_t newlen = BWSLEN(str) - (src - dst); 990 --- 10 unchanged lines hidden (view full) --- 1001} 1002 1003/* 1004 * Rips out nonprinting characters (-i). 1005 */ 1006struct bwstring * 1007ignore_nonprinting(struct bwstring *str) 1008{ |
1006 size_t newlen = str->len; | 1009 size_t newlen = BWSLEN(str); |
1007 1008 if (mb_cur_max == 1) { | 1010 1011 if (mb_cur_max == 1) { |
1009 unsigned char *dst, *end, *src; 1010 unsigned char c; | 1012 char *dst, *end, *src; 1013 char c; |
1011 | 1014 |
1012 src = str->data.cstr; | 1015 src = str->cdata.str; |
1013 dst = src; | 1016 dst = src; |
1014 end = src + str->len; | 1017 end = src + str->cdata.len; |
1015 1016 while (src < end) { 1017 c = *src; 1018 if (isprint(c)) { 1019 *dst = c; 1020 ++dst; 1021 ++src; 1022 } else { 1023 ++src; 1024 --newlen; 1025 } 1026 } 1027 } else { 1028 wchar_t *dst, *end, *src; 1029 wchar_t c; 1030 | 1018 1019 while (src < end) { 1020 c = *src; 1021 if (isprint(c)) { 1022 *dst = c; 1023 ++dst; 1024 ++src; 1025 } else { 1026 ++src; 1027 --newlen; 1028 } 1029 } 1030 } else { 1031 wchar_t *dst, *end, *src; 1032 wchar_t c; 1033 |
1031 src = str->data.wstr; | 1034 src = str->wdata.str; |
1032 dst = src; | 1035 dst = src; |
1033 end = src + str->len; | 1036 end = src + str->wdata.len; |
1034 1035 while (src < end) { 1036 c = *src; 1037 if (iswprint(c)) { 1038 *dst = c; 1039 ++dst; 1040 ++src; 1041 } else { --- 9 unchanged lines hidden (view full) --- 1051 1052/* 1053 * Rips out any characters that are not alphanumeric characters 1054 * nor blanks (-d). 1055 */ 1056struct bwstring * 1057dictionary_order(struct bwstring *str) 1058{ | 1037 1038 while (src < end) { 1039 c = *src; 1040 if (iswprint(c)) { 1041 *dst = c; 1042 ++dst; 1043 ++src; 1044 } else { --- 9 unchanged lines hidden (view full) --- 1054 1055/* 1056 * Rips out any characters that are not alphanumeric characters 1057 * nor blanks (-d). 1058 */ 1059struct bwstring * 1060dictionary_order(struct bwstring *str) 1061{ |
1059 size_t newlen = str->len; | 1062 size_t newlen = BWSLEN(str); |
1060 1061 if (mb_cur_max == 1) { | 1063 1064 if (mb_cur_max == 1) { |
1062 unsigned char *dst, *end, *src; 1063 unsigned char c; | 1065 char *dst, *end, *src; 1066 char c; |
1064 | 1067 |
1065 src = str->data.cstr; | 1068 src = str->cdata.str; |
1066 dst = src; | 1069 dst = src; |
1067 end = src + str->len; | 1070 end = src + str->cdata.len; |
1068 1069 while (src < end) { 1070 c = *src; 1071 if (isalnum(c) || isblank(c)) { 1072 *dst = c; 1073 ++dst; 1074 ++src; 1075 } else { 1076 ++src; 1077 --newlen; 1078 } 1079 } 1080 } else { 1081 wchar_t *dst, *end, *src; 1082 wchar_t c; 1083 | 1071 1072 while (src < end) { 1073 c = *src; 1074 if (isalnum(c) || isblank(c)) { 1075 *dst = c; 1076 ++dst; 1077 ++src; 1078 } else { 1079 ++src; 1080 --newlen; 1081 } 1082 } 1083 } else { 1084 wchar_t *dst, *end, *src; 1085 wchar_t c; 1086 |
1084 src = str->data.wstr; | 1087 src = str->wdata.str; |
1085 dst = src; | 1088 dst = src; |
1086 end = src + str->len; | 1089 end = src + str->wdata.len; |
1087 1088 while (src < end) { 1089 c = *src; 1090 if (iswalnum(c) || iswblank(c)) { 1091 *dst = c; 1092 ++dst; 1093 ++src; 1094 } else { --- 10 unchanged lines hidden (view full) --- 1105/* 1106 * Converts string to lower case(-f). 1107 */ 1108struct bwstring * 1109ignore_case(struct bwstring *str) 1110{ 1111 1112 if (mb_cur_max == 1) { | 1090 1091 while (src < end) { 1092 c = *src; 1093 if (iswalnum(c) || iswblank(c)) { 1094 *dst = c; 1095 ++dst; 1096 ++src; 1097 } else { --- 10 unchanged lines hidden (view full) --- 1108/* 1109 * Converts string to lower case(-f). 1110 */ 1111struct bwstring * 1112ignore_case(struct bwstring *str) 1113{ 1114 1115 if (mb_cur_max == 1) { |
1113 unsigned char *end, *s; | 1116 char *end, *s; |
1114 | 1117 |
1115 s = str->data.cstr; 1116 end = s + str->len; | 1118 s = str->cdata.str; 1119 end = s + str->cdata.len; |
1117 1118 while (s < end) { 1119 *s = toupper(*s); 1120 ++s; 1121 } 1122 } else { 1123 wchar_t *end, *s; 1124 | 1120 1121 while (s < end) { 1122 *s = toupper(*s); 1123 ++s; 1124 } 1125 } else { 1126 wchar_t *end, *s; 1127 |
1125 s = str->data.wstr; 1126 end = s + str->len; | 1128 s = str->wdata.str; 1129 end = s + str->wdata.len; |
1127 1128 while (s < end) { 1129 *s = towupper(*s); 1130 ++s; 1131 } 1132 } 1133 return (str); 1134} 1135 1136void 1137bws_disorder_warnx(struct bwstring *s, const char *fn, size_t pos) 1138{ 1139 1140 if (mb_cur_max == 1) | 1130 1131 while (s < end) { 1132 *s = towupper(*s); 1133 ++s; 1134 } 1135 } 1136 return (str); 1137} 1138 1139void 1140bws_disorder_warnx(struct bwstring *s, const char *fn, size_t pos) 1141{ 1142 1143 if (mb_cur_max == 1) |
1141 warnx("%s:%zu: disorder: %s", fn, pos + 1, s->data.cstr); | 1144 warnx("%s:%zu: disorder: %s", fn, pos + 1, s->cdata.str); |
1142 else | 1145 else |
1143 warnx("%s:%zu: disorder: %ls", fn, pos + 1, s->data.wstr); | 1146 warnx("%s:%zu: disorder: %ls", fn, pos + 1, s->wdata.str); |
1144} | 1147} |