1 /* 2 * This file is part of gtkD. 3 * 4 * gtkD is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU Lesser General Public License 6 * as published by the Free Software Foundation; either version 3 7 * of the License, or (at your option) any later version, with 8 * some exceptions, please read the COPYING file. 9 * 10 * gtkD is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU Lesser General Public License for more details. 14 * 15 * You should have received a copy of the GNU Lesser General Public License 16 * along with gtkD; if not, write to the Free Software 17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA 18 */ 19 20 // generated automatically - do not change 21 // find conversion definition on APILookup.txt 22 // implement new conversion functionalities on the wrap.utils pakage 23 24 25 module glib.Str; 26 27 private import core.stdc.stdio; 28 private import core.stdc.string; 29 private import glib.ErrorG; 30 private import glib.GException; 31 private import glib.Str; 32 private import glib.c.functions; 33 public import glib.c.types; 34 public import gobject.c.types; 35 public import gtkc.glibtypes; 36 37 38 /** */ 39 public struct Str 40 { 41 /* 42 * Convert C-style 0 terminated string s to char[] string. 43 * copied from phobos 44 */ 45 public static string toString(const(char)* s, size_t len = 0) pure 46 { 47 if ( s is null ) 48 return cast(string)null; 49 50 if ( len == 0 ) 51 len = strlen(s); 52 53 return s[0 .. len].idup; 54 } 55 56 /* 57 * Convert array of chars s[] to a C-style 0 terminated string. 58 * copied from phobos 59 */ 60 public static char* toStringz(string s) pure 61 { 62 if ( s is null ) return null; 63 char[] copy; 64 65 if (s.length == 0) 66 { 67 copy = "\0".dup; 68 } 69 else 70 { 71 // Need to make a copy 72 copy = new char[s.length + 1]; 73 copy[0..s.length] = s[]; 74 copy[s.length] = 0; 75 } 76 77 return copy.ptr; 78 } 79 80 /** */ 81 public static char** toStringzArray(string[] args) pure 82 { 83 if ( args is null ) 84 { 85 return null; 86 } 87 char** argv = (new char*[args.length + 1]).ptr; 88 int argc = 0; 89 foreach (string p; args) 90 { 91 argv[argc++] = cast(char*)(p.dup~'\0'); 92 } 93 argv[argc] = null; 94 95 return argv; 96 } 97 98 /** */ 99 public static char*** toStringzArray(string[][] args) pure 100 { 101 if ( args is null ) 102 { 103 return null; 104 } 105 char**[] argv = new char**[args.length + 1]; 106 int argc = 0; 107 foreach( string[] p; args ) 108 { 109 argv[argc++] = toStringzArray(p); 110 } 111 argv[argc] = null; 112 113 return argv.ptr; 114 } 115 116 /** */ 117 public static string[] toStringArray(const(char*)* args) pure 118 { 119 if ( args is null ) 120 { 121 return null; 122 } 123 string[] argv; 124 125 while ( *args !is null ) 126 { 127 argv ~= toString(*args); 128 args++; 129 } 130 131 return argv; 132 } 133 134 /** */ 135 public static string[] toStringArray(const(char*)* args, size_t len) pure 136 { 137 string[] argv = new string[len]; 138 139 for ( int i; i < len; i++ ) 140 { 141 argv[i] = toString(args[i]); 142 } 143 144 return argv; 145 } 146 147 /** */ 148 public static string[][] toStringArray(char*** args) pure 149 { 150 string[][] argv; 151 152 if ( args is null ) 153 { 154 return null; 155 } 156 157 while ( *args !is null ) 158 { 159 argv ~= toStringArray(*args); 160 args++; 161 } 162 163 return argv; 164 } 165 166 /** */ 167 public static void freeString(char* str) 168 { 169 g_free(str); 170 } 171 172 /** */ 173 public static void freeStringArray(char** str) 174 { 175 g_strfreev(str); 176 } 177 178 /** */ 179 public static void freeStringArray(char*** str) 180 { 181 while ( *str !is null ) 182 { 183 g_strfreev(*str); 184 str++; 185 } 186 187 g_free(str); 188 } 189 190 /** 191 */ 192 193 /** 194 * Determines the numeric value of a character as a decimal digit. 195 * Differs from g_unichar_digit_value() because it takes a char, so 196 * there's no worry about sign extension if characters are signed. 197 * 198 * Params: 199 * c = an ASCII character 200 * 201 * Returns: If @c is a decimal digit (according to g_ascii_isdigit()), 202 * its numeric value. Otherwise, -1. 203 */ 204 public static int asciiDigitValue(char c) 205 { 206 return g_ascii_digit_value(c); 207 } 208 209 /** 210 * Converts a #gdouble to a string, using the '.' as 211 * decimal point. 212 * 213 * This function generates enough precision that converting 214 * the string back using g_ascii_strtod() gives the same machine-number 215 * (on machines with IEEE compatible 64bit doubles). It is 216 * guaranteed that the size of the resulting string will never 217 * be larger than @G_ASCII_DTOSTR_BUF_SIZE bytes, including the terminating 218 * nul character, which is always added. 219 * 220 * Params: 221 * buffer = A buffer to place the resulting string in 222 * bufLen = The length of the buffer. 223 * d = The #gdouble to convert 224 * 225 * Returns: The pointer to the buffer with the converted string. 226 */ 227 public static string asciiDtostr(string buffer, int bufLen, double d) 228 { 229 auto retStr = g_ascii_dtostr(Str.toStringz(buffer), bufLen, d); 230 231 scope(exit) Str.freeString(retStr); 232 return Str.toString(retStr); 233 } 234 235 /** 236 * Converts a #gdouble to a string, using the '.' as 237 * decimal point. To format the number you pass in 238 * a printf()-style format string. Allowed conversion 239 * specifiers are 'e', 'E', 'f', 'F', 'g' and 'G'. 240 * 241 * The returned buffer is guaranteed to be nul-terminated. 242 * 243 * If you just want to want to serialize the value into a 244 * string, use g_ascii_dtostr(). 245 * 246 * Params: 247 * buffer = A buffer to place the resulting string in 248 * bufLen = The length of the buffer. 249 * format = The printf()-style format to use for the 250 * code to use for converting. 251 * d = The #gdouble to convert 252 * 253 * Returns: The pointer to the buffer with the converted string. 254 */ 255 public static string asciiFormatd(string buffer, int bufLen, string format, double d) 256 { 257 auto retStr = g_ascii_formatd(Str.toStringz(buffer), bufLen, Str.toStringz(format), d); 258 259 scope(exit) Str.freeString(retStr); 260 return Str.toString(retStr); 261 } 262 263 /** 264 * Compare two strings, ignoring the case of ASCII characters. 265 * 266 * Unlike the BSD strcasecmp() function, this only recognizes standard 267 * ASCII letters and ignores the locale, treating all non-ASCII 268 * bytes as if they are not letters. 269 * 270 * This function should be used only on strings that are known to be 271 * in encodings where the bytes corresponding to ASCII letters always 272 * represent themselves. This includes UTF-8 and the ISO-8859-* 273 * charsets, but not for instance double-byte encodings like the 274 * Windows Codepage 932, where the trailing bytes of double-byte 275 * characters include all ASCII letters. If you compare two CP932 276 * strings using this function, you will get false matches. 277 * 278 * Both @s1 and @s2 must be non-%NULL. 279 * 280 * Params: 281 * s1 = string to compare with @s2 282 * s2 = string to compare with @s1 283 * 284 * Returns: 0 if the strings match, a negative value if @s1 < @s2, 285 * or a positive value if @s1 > @s2. 286 */ 287 public static int asciiStrcasecmp(string s1, string s2) 288 { 289 return g_ascii_strcasecmp(Str.toStringz(s1), Str.toStringz(s2)); 290 } 291 292 /** 293 * Converts all upper case ASCII letters to lower case ASCII letters. 294 * 295 * Params: 296 * str = a string 297 * len = length of @str in bytes, or -1 if @str is nul-terminated 298 * 299 * Returns: a newly-allocated string, with all the upper case 300 * characters in @str converted to lower case, with semantics that 301 * exactly match g_ascii_tolower(). (Note that this is unlike the 302 * old g_strdown(), which modified the string in place.) 303 */ 304 public static string asciiStrdown(string str, ptrdiff_t len) 305 { 306 auto retStr = g_ascii_strdown(Str.toStringz(str), len); 307 308 scope(exit) Str.freeString(retStr); 309 return Str.toString(retStr); 310 } 311 312 /** 313 * Compare @s1 and @s2, ignoring the case of ASCII characters and any 314 * characters after the first @n in each string. 315 * 316 * Unlike the BSD strcasecmp() function, this only recognizes standard 317 * ASCII letters and ignores the locale, treating all non-ASCII 318 * characters as if they are not letters. 319 * 320 * The same warning as in g_ascii_strcasecmp() applies: Use this 321 * function only on strings known to be in encodings where bytes 322 * corresponding to ASCII letters always represent themselves. 323 * 324 * Params: 325 * s1 = string to compare with @s2 326 * s2 = string to compare with @s1 327 * n = number of characters to compare 328 * 329 * Returns: 0 if the strings match, a negative value if @s1 < @s2, 330 * or a positive value if @s1 > @s2. 331 */ 332 public static int asciiStrncasecmp(string s1, string s2, size_t n) 333 { 334 return g_ascii_strncasecmp(Str.toStringz(s1), Str.toStringz(s2), n); 335 } 336 337 /** 338 * Converts a string to a #gdouble value. 339 * 340 * This function behaves like the standard strtod() function 341 * does in the C locale. It does this without actually changing 342 * the current locale, since that would not be thread-safe. 343 * A limitation of the implementation is that this function 344 * will still accept localized versions of infinities and NANs. 345 * 346 * This function is typically used when reading configuration 347 * files or other non-user input that should be locale independent. 348 * To handle input from the user you should normally use the 349 * locale-sensitive system strtod() function. 350 * 351 * To convert from a #gdouble to a string in a locale-insensitive 352 * way, use g_ascii_dtostr(). 353 * 354 * If the correct value would cause overflow, plus or minus %HUGE_VAL 355 * is returned (according to the sign of the value), and %ERANGE is 356 * stored in %errno. If the correct value would cause underflow, 357 * zero is returned and %ERANGE is stored in %errno. 358 * 359 * This function resets %errno before calling strtod() so that 360 * you can reliably detect overflow and underflow. 361 * 362 * Params: 363 * nptr = the string to convert to a numeric value. 364 * endptr = if non-%NULL, it returns the 365 * character after the last character used in the conversion. 366 * 367 * Returns: the #gdouble value. 368 */ 369 public static double asciiStrtod(string nptr, out string endptr) 370 { 371 char* outendptr = null; 372 373 auto p = g_ascii_strtod(Str.toStringz(nptr), &outendptr); 374 375 endptr = Str.toString(outendptr); 376 377 return p; 378 } 379 380 /** 381 * Converts a string to a #gint64 value. 382 * This function behaves like the standard strtoll() function 383 * does in the C locale. It does this without actually 384 * changing the current locale, since that would not be 385 * thread-safe. 386 * 387 * This function is typically used when reading configuration 388 * files or other non-user input that should be locale independent. 389 * To handle input from the user you should normally use the 390 * locale-sensitive system strtoll() function. 391 * 392 * If the correct value would cause overflow, %G_MAXINT64 or %G_MININT64 393 * is returned, and `ERANGE` is stored in `errno`. 394 * If the base is outside the valid range, zero is returned, and 395 * `EINVAL` is stored in `errno`. If the 396 * string conversion fails, zero is returned, and @endptr returns @nptr 397 * (if @endptr is non-%NULL). 398 * 399 * Params: 400 * nptr = the string to convert to a numeric value. 401 * endptr = if non-%NULL, it returns the 402 * character after the last character used in the conversion. 403 * base = to be used for the conversion, 2..36 or 0 404 * 405 * Returns: the #gint64 value or zero on error. 406 * 407 * Since: 2.12 408 */ 409 public static long asciiStrtoll(string nptr, out string endptr, uint base) 410 { 411 char* outendptr = null; 412 413 auto p = g_ascii_strtoll(Str.toStringz(nptr), &outendptr, base); 414 415 endptr = Str.toString(outendptr); 416 417 return p; 418 } 419 420 /** 421 * Converts a string to a #guint64 value. 422 * This function behaves like the standard strtoull() function 423 * does in the C locale. It does this without actually 424 * changing the current locale, since that would not be 425 * thread-safe. 426 * 427 * Note that input with a leading minus sign (`-`) is accepted, and will return 428 * the negation of the parsed number, unless that would overflow a #guint64. 429 * Critically, this means you cannot assume that a short fixed length input will 430 * never result in a low return value, as the input could have a leading `-`. 431 * 432 * This function is typically used when reading configuration 433 * files or other non-user input that should be locale independent. 434 * To handle input from the user you should normally use the 435 * locale-sensitive system strtoull() function. 436 * 437 * If the correct value would cause overflow, %G_MAXUINT64 438 * is returned, and `ERANGE` is stored in `errno`. 439 * If the base is outside the valid range, zero is returned, and 440 * `EINVAL` is stored in `errno`. 441 * If the string conversion fails, zero is returned, and @endptr returns 442 * @nptr (if @endptr is non-%NULL). 443 * 444 * Params: 445 * nptr = the string to convert to a numeric value. 446 * endptr = if non-%NULL, it returns the 447 * character after the last character used in the conversion. 448 * base = to be used for the conversion, 2..36 or 0 449 * 450 * Returns: the #guint64 value or zero on error. 451 * 452 * Since: 2.2 453 */ 454 public static ulong asciiStrtoull(string nptr, out string endptr, uint base) 455 { 456 char* outendptr = null; 457 458 auto p = g_ascii_strtoull(Str.toStringz(nptr), &outendptr, base); 459 460 endptr = Str.toString(outendptr); 461 462 return p; 463 } 464 465 /** 466 * Converts all lower case ASCII letters to upper case ASCII letters. 467 * 468 * Params: 469 * str = a string 470 * len = length of @str in bytes, or -1 if @str is nul-terminated 471 * 472 * Returns: a newly allocated string, with all the lower case 473 * characters in @str converted to upper case, with semantics that 474 * exactly match g_ascii_toupper(). (Note that this is unlike the 475 * old g_strup(), which modified the string in place.) 476 */ 477 public static string asciiStrup(string str, ptrdiff_t len) 478 { 479 auto retStr = g_ascii_strup(Str.toStringz(str), len); 480 481 scope(exit) Str.freeString(retStr); 482 return Str.toString(retStr); 483 } 484 485 /** 486 * Convert a character to ASCII lower case. 487 * 488 * Unlike the standard C library tolower() function, this only 489 * recognizes standard ASCII letters and ignores the locale, returning 490 * all non-ASCII characters unchanged, even if they are lower case 491 * letters in a particular character set. Also unlike the standard 492 * library function, this takes and returns a char, not an int, so 493 * don't call it on %EOF but no need to worry about casting to #guchar 494 * before passing a possibly non-ASCII character in. 495 * 496 * Params: 497 * c = any character 498 * 499 * Returns: the result of converting @c to lower case. If @c is 500 * not an ASCII upper case letter, @c is returned unchanged. 501 */ 502 public static char asciiTolower(char c) 503 { 504 return g_ascii_tolower(c); 505 } 506 507 /** 508 * Convert a character to ASCII upper case. 509 * 510 * Unlike the standard C library toupper() function, this only 511 * recognizes standard ASCII letters and ignores the locale, returning 512 * all non-ASCII characters unchanged, even if they are upper case 513 * letters in a particular character set. Also unlike the standard 514 * library function, this takes and returns a char, not an int, so 515 * don't call it on %EOF but no need to worry about casting to #guchar 516 * before passing a possibly non-ASCII character in. 517 * 518 * Params: 519 * c = any character 520 * 521 * Returns: the result of converting @c to upper case. If @c is not 522 * an ASCII lower case letter, @c is returned unchanged. 523 */ 524 public static char asciiToupper(char c) 525 { 526 return g_ascii_toupper(c); 527 } 528 529 /** 530 * Determines the numeric value of a character as a hexidecimal 531 * digit. Differs from g_unichar_xdigit_value() because it takes 532 * a char, so there's no worry about sign extension if characters 533 * are signed. 534 * 535 * Params: 536 * c = an ASCII character. 537 * 538 * Returns: If @c is a hex digit (according to g_ascii_isxdigit()), 539 * its numeric value. Otherwise, -1. 540 */ 541 public static int asciiXdigitValue(char c) 542 { 543 return g_ascii_xdigit_value(c); 544 } 545 546 /** 547 * Calculates the maximum space needed to store the output 548 * of the sprintf() function. 549 * 550 * Params: 551 * format = the format string. See the printf() documentation 552 * args = the parameters to be inserted into the format string 553 * 554 * Returns: the maximum space needed to store the formatted string 555 */ 556 public static size_t printfStringUpperBound(string format, void* args) 557 { 558 return g_printf_string_upper_bound(Str.toStringz(format), args); 559 } 560 561 /** 562 * Copies a nul-terminated string into the dest buffer, include the 563 * trailing nul, and return a pointer to the trailing nul byte. 564 * This is useful for concatenating multiple strings together 565 * without having to repeatedly scan for the end. 566 * 567 * Params: 568 * dest = destination buffer. 569 * src = source string. 570 * 571 * Returns: a pointer to trailing nul byte. 572 */ 573 public static string stpcpy(string dest, string src) 574 { 575 auto retStr = g_stpcpy(Str.toStringz(dest), Str.toStringz(src)); 576 577 scope(exit) Str.freeString(retStr); 578 return Str.toString(retStr); 579 } 580 581 /** 582 * Looks whether the string @str begins with @prefix. 583 * 584 * Params: 585 * str = a nul-terminated string 586 * prefix = the nul-terminated prefix to look for 587 * 588 * Returns: %TRUE if @str begins with @prefix, %FALSE otherwise. 589 * 590 * Since: 2.2 591 */ 592 public static bool hasPrefix(string str, string prefix) 593 { 594 return g_str_has_prefix(Str.toStringz(str), Str.toStringz(prefix)) != 0; 595 } 596 597 /** 598 * Looks whether the string @str ends with @suffix. 599 * 600 * Params: 601 * str = a nul-terminated string 602 * suffix = the nul-terminated suffix to look for 603 * 604 * Returns: %TRUE if @str end with @suffix, %FALSE otherwise. 605 * 606 * Since: 2.2 607 */ 608 public static bool hasSuffix(string str, string suffix) 609 { 610 return g_str_has_suffix(Str.toStringz(str), Str.toStringz(suffix)) != 0; 611 } 612 613 /** 614 * Determines if a string is pure ASCII. A string is pure ASCII if it 615 * contains no bytes with the high bit set. 616 * 617 * Params: 618 * str = a string 619 * 620 * Returns: %TRUE if @str is ASCII 621 * 622 * Since: 2.40 623 */ 624 public static bool isAscii(string str) 625 { 626 return g_str_is_ascii(Str.toStringz(str)) != 0; 627 } 628 629 /** 630 * Checks if a search conducted for @search_term should match 631 * @potential_hit. 632 * 633 * This function calls g_str_tokenize_and_fold() on both 634 * @search_term and @potential_hit. ASCII alternates are never taken 635 * for @search_term but will be taken for @potential_hit according to 636 * the value of @accept_alternates. 637 * 638 * A hit occurs when each folded token in @search_term is a prefix of a 639 * folded token from @potential_hit. 640 * 641 * Depending on how you're performing the search, it will typically be 642 * faster to call g_str_tokenize_and_fold() on each string in 643 * your corpus and build an index on the returned folded tokens, then 644 * call g_str_tokenize_and_fold() on the search term and 645 * perform lookups into that index. 646 * 647 * As some examples, searching for ‘fred’ would match the potential hit 648 * ‘Smith, Fred’ and also ‘Frédéric’. Searching for ‘Fréd’ would match 649 * ‘Frédéric’ but not ‘Frederic’ (due to the one-directional nature of 650 * accent matching). Searching ‘fo’ would match ‘Foo’ and ‘Bar Foo 651 * Baz’, but not ‘SFO’ (because no word has ‘fo’ as a prefix). 652 * 653 * Params: 654 * searchTerm = the search term from the user 655 * potentialHit = the text that may be a hit 656 * acceptAlternates = %TRUE to accept ASCII alternates 657 * 658 * Returns: %TRUE if @potential_hit is a hit 659 * 660 * Since: 2.40 661 */ 662 public static bool matchString(string searchTerm, string potentialHit, bool acceptAlternates) 663 { 664 return g_str_match_string(Str.toStringz(searchTerm), Str.toStringz(potentialHit), acceptAlternates) != 0; 665 } 666 667 /** 668 * Transliterate @str to plain ASCII. 669 * 670 * For best results, @str should be in composed normalised form. 671 * 672 * This function performs a reasonably good set of character 673 * replacements. The particular set of replacements that is done may 674 * change by version or even by runtime environment. 675 * 676 * If the source language of @str is known, it can used to improve the 677 * accuracy of the translation by passing it as @from_locale. It should 678 * be a valid POSIX locale string (of the form 679 * `language[_territory][.codeset][@modifier]`). 680 * 681 * If @from_locale is %NULL then the current locale is used. 682 * 683 * If you want to do translation for no specific locale, and you want it 684 * to be done independently of the currently locale, specify `"C"` for 685 * @from_locale. 686 * 687 * Params: 688 * str = a string, in UTF-8 689 * fromLocale = the source locale, if known 690 * 691 * Returns: a string in plain ASCII 692 * 693 * Since: 2.40 694 */ 695 public static string toAscii(string str, string fromLocale) 696 { 697 auto retStr = g_str_to_ascii(Str.toStringz(str), Str.toStringz(fromLocale)); 698 699 scope(exit) Str.freeString(retStr); 700 return Str.toString(retStr); 701 } 702 703 /** 704 * Tokenises @string and performs folding on each token. 705 * 706 * A token is a non-empty sequence of alphanumeric characters in the 707 * source string, separated by non-alphanumeric characters. An 708 * "alphanumeric" character for this purpose is one that matches 709 * g_unichar_isalnum() or g_unichar_ismark(). 710 * 711 * Each token is then (Unicode) normalised and case-folded. If 712 * @ascii_alternates is non-%NULL and some of the returned tokens 713 * contain non-ASCII characters, ASCII alternatives will be generated. 714 * 715 * The number of ASCII alternatives that are generated and the method 716 * for doing so is unspecified, but @translit_locale (if specified) may 717 * improve the transliteration if the language of the source string is 718 * known. 719 * 720 * Params: 721 * string_ = a string 722 * translitLocale = the language code (like 'de' or 723 * 'en_GB') from which @string originates 724 * asciiAlternates = a 725 * return location for ASCII alternates 726 * 727 * Returns: the folded tokens 728 * 729 * Since: 2.40 730 */ 731 public static string[] tokenizeAndFold(string string_, string translitLocale, out string[] asciiAlternates) 732 { 733 char** outasciiAlternates = null; 734 735 auto retStr = g_str_tokenize_and_fold(Str.toStringz(string_), Str.toStringz(translitLocale), &outasciiAlternates); 736 737 asciiAlternates = Str.toStringArray(outasciiAlternates); 738 739 scope(exit) Str.freeStringArray(retStr); 740 return Str.toStringArray(retStr); 741 } 742 743 /** 744 * For each character in @string, if the character is not in @valid_chars, 745 * replaces the character with @substitutor. Modifies @string in place, 746 * and return @string itself, not a copy. The return value is to allow 747 * nesting such as 748 * |[<!-- language="C" --> 749 * g_ascii_strup (g_strcanon (str, "abc", '?')) 750 * ]| 751 * 752 * Params: 753 * string_ = a nul-terminated array of bytes 754 * validChars = bytes permitted in @string 755 * substitutor = replacement character for disallowed bytes 756 * 757 * Returns: @string 758 */ 759 public static string strcanon(string string_, string validChars, char substitutor) 760 { 761 auto retStr = g_strcanon(Str.toStringz(string_), Str.toStringz(validChars), substitutor); 762 763 scope(exit) Str.freeString(retStr); 764 return Str.toString(retStr); 765 } 766 767 /** 768 * A case-insensitive string comparison, corresponding to the standard 769 * strcasecmp() function on platforms which support it. 770 * 771 * Deprecated: See g_strncasecmp() for a discussion of why this 772 * function is deprecated and how to replace it. 773 * 774 * Params: 775 * s1 = a string 776 * s2 = a string to compare with @s1 777 * 778 * Returns: 0 if the strings match, a negative value if @s1 < @s2, 779 * or a positive value if @s1 > @s2. 780 */ 781 public static int strcasecmp(string s1, string s2) 782 { 783 return g_strcasecmp(Str.toStringz(s1), Str.toStringz(s2)); 784 } 785 786 /** 787 * Removes trailing whitespace from a string. 788 * 789 * This function doesn't allocate or reallocate any memory; 790 * it modifies @string in place. Therefore, it cannot be used 791 * on statically allocated strings. 792 * 793 * The pointer to @string is returned to allow the nesting of functions. 794 * 795 * Also see g_strchug() and g_strstrip(). 796 * 797 * Params: 798 * string_ = a string to remove the trailing whitespace from 799 * 800 * Returns: @string 801 */ 802 public static string strchomp(string string_) 803 { 804 auto retStr = g_strchomp(Str.toStringz(string_)); 805 806 scope(exit) Str.freeString(retStr); 807 return Str.toString(retStr); 808 } 809 810 /** 811 * Removes leading whitespace from a string, by moving the rest 812 * of the characters forward. 813 * 814 * This function doesn't allocate or reallocate any memory; 815 * it modifies @string in place. Therefore, it cannot be used on 816 * statically allocated strings. 817 * 818 * The pointer to @string is returned to allow the nesting of functions. 819 * 820 * Also see g_strchomp() and g_strstrip(). 821 * 822 * Params: 823 * string_ = a string to remove the leading whitespace from 824 * 825 * Returns: @string 826 */ 827 public static string strchug(string string_) 828 { 829 auto retStr = g_strchug(Str.toStringz(string_)); 830 831 scope(exit) Str.freeString(retStr); 832 return Str.toString(retStr); 833 } 834 835 /** 836 * Compares @str1 and @str2 like strcmp(). Handles %NULL 837 * gracefully by sorting it before non-%NULL strings. 838 * Comparing two %NULL pointers returns 0. 839 * 840 * Params: 841 * str1 = a C string or %NULL 842 * str2 = another C string or %NULL 843 * 844 * Returns: an integer less than, equal to, or greater than zero, if @str1 is <, == or > than @str2. 845 * 846 * Since: 2.16 847 */ 848 public static int strcmp0(string str1, string str2) 849 { 850 return g_strcmp0(Str.toStringz(str1), Str.toStringz(str2)); 851 } 852 853 /** 854 * Replaces all escaped characters with their one byte equivalent. 855 * 856 * This function does the reverse conversion of g_strescape(). 857 * 858 * Params: 859 * source = a string to compress 860 * 861 * Returns: a newly-allocated copy of @source with all escaped 862 * character compressed 863 */ 864 public static string strcompress(string source) 865 { 866 auto retStr = g_strcompress(Str.toStringz(source)); 867 868 scope(exit) Str.freeString(retStr); 869 return Str.toString(retStr); 870 } 871 872 /** 873 * Converts any delimiter characters in @string to @new_delimiter. 874 * Any characters in @string which are found in @delimiters are 875 * changed to the @new_delimiter character. Modifies @string in place, 876 * and returns @string itself, not a copy. The return value is to 877 * allow nesting such as 878 * |[<!-- language="C" --> 879 * g_ascii_strup (g_strdelimit (str, "abc", '?')) 880 * ]| 881 * 882 * Params: 883 * string_ = the string to convert 884 * delimiters = a string containing the current delimiters, 885 * or %NULL to use the standard delimiters defined in #G_STR_DELIMITERS 886 * newDelimiter = the new delimiter character 887 * 888 * Returns: @string 889 */ 890 public static string strdelimit(string string_, string delimiters, char newDelimiter) 891 { 892 auto retStr = g_strdelimit(Str.toStringz(string_), Str.toStringz(delimiters), newDelimiter); 893 894 scope(exit) Str.freeString(retStr); 895 return Str.toString(retStr); 896 } 897 898 /** 899 * Converts a string to lower case. 900 * 901 * Deprecated: This function is totally broken for the reasons discussed 902 * in the g_strncasecmp() docs - use g_ascii_strdown() or g_utf8_strdown() 903 * instead. 904 * 905 * Params: 906 * string_ = the string to convert. 907 * 908 * Returns: the string 909 */ 910 public static string strdown(string string_) 911 { 912 auto retStr = g_strdown(Str.toStringz(string_)); 913 914 scope(exit) Str.freeString(retStr); 915 return Str.toString(retStr); 916 } 917 918 /** 919 * Duplicates a string. If @str is %NULL it returns %NULL. 920 * The returned string should be freed with g_free() 921 * when no longer needed. 922 * 923 * Params: 924 * str = the string to duplicate 925 * 926 * Returns: a newly-allocated copy of @str 927 */ 928 public static string strdup(string str) 929 { 930 auto retStr = g_strdup(Str.toStringz(str)); 931 932 scope(exit) Str.freeString(retStr); 933 return Str.toString(retStr); 934 } 935 936 /** 937 * Similar to the standard C vsprintf() function but safer, since it 938 * calculates the maximum space required and allocates memory to hold 939 * the result. The returned string should be freed with g_free() when 940 * no longer needed. 941 * 942 * See also g_vasprintf(), which offers the same functionality, but 943 * additionally returns the length of the allocated string. 944 * 945 * Params: 946 * format = a standard printf() format string, but notice 947 * [string precision pitfalls][string-precision] 948 * args = the list of parameters to insert into the format string 949 * 950 * Returns: a newly-allocated string holding the result 951 */ 952 public static string strdupVprintf(string format, void* args) 953 { 954 auto retStr = g_strdup_vprintf(Str.toStringz(format), args); 955 956 scope(exit) Str.freeString(retStr); 957 return Str.toString(retStr); 958 } 959 960 /** 961 * Copies %NULL-terminated array of strings. The copy is a deep copy; 962 * the new array should be freed by first freeing each string, then 963 * the array itself. g_strfreev() does this for you. If called 964 * on a %NULL value, g_strdupv() simply returns %NULL. 965 * 966 * Params: 967 * strArray = a %NULL-terminated array of strings 968 * 969 * Returns: a new %NULL-terminated array of strings. 970 */ 971 public static string[] strdupv(string[] strArray) 972 { 973 return Str.toStringArray(g_strdupv(Str.toStringzArray(strArray))); 974 } 975 976 /** 977 * Returns a string corresponding to the given error code, e.g. "no 978 * such process". Unlike strerror(), this always returns a string in 979 * UTF-8 encoding, and the pointer is guaranteed to remain valid for 980 * the lifetime of the process. 981 * 982 * Note that the string may be translated according to the current locale. 983 * 984 * The value of %errno will not be changed by this function. However, it may 985 * be changed by intermediate function calls, so you should save its value 986 * as soon as the call returns: 987 * |[ 988 * int saved_errno; 989 * 990 * ret = read (blah); 991 * saved_errno = errno; 992 * 993 * g_strerror (saved_errno); 994 * ]| 995 * 996 * Params: 997 * errnum = the system error number. See the standard C %errno 998 * documentation 999 * 1000 * Returns: a UTF-8 string describing the error code. If the error code 1001 * is unknown, it returns a string like "unknown error (<code>)". 1002 */ 1003 public static string strerror(int errnum) 1004 { 1005 return Str.toString(g_strerror(errnum)); 1006 } 1007 1008 /** 1009 * Escapes the special characters '\b', '\f', '\n', '\r', '\t', '\v', '\' 1010 * and '"' in the string @source by inserting a '\' before 1011 * them. Additionally all characters in the range 0x01-0x1F (everything 1012 * below SPACE) and in the range 0x7F-0xFF (all non-ASCII chars) are 1013 * replaced with a '\' followed by their octal representation. 1014 * Characters supplied in @exceptions are not escaped. 1015 * 1016 * g_strcompress() does the reverse conversion. 1017 * 1018 * Params: 1019 * source = a string to escape 1020 * exceptions = a string of characters not to escape in @source 1021 * 1022 * Returns: a newly-allocated copy of @source with certain 1023 * characters escaped. See above. 1024 */ 1025 public static string strescape(string source, string exceptions) 1026 { 1027 auto retStr = g_strescape(Str.toStringz(source), Str.toStringz(exceptions)); 1028 1029 scope(exit) Str.freeString(retStr); 1030 return Str.toString(retStr); 1031 } 1032 1033 /** 1034 * Frees a %NULL-terminated array of strings, as well as each 1035 * string it contains. 1036 * 1037 * If @str_array is %NULL, this function simply returns. 1038 * 1039 * Params: 1040 * strArray = a %NULL-terminated array of strings to free 1041 */ 1042 public static void strfreev(string[] strArray) 1043 { 1044 g_strfreev(Str.toStringzArray(strArray)); 1045 } 1046 1047 /** 1048 * Joins a number of strings together to form one long string, with the 1049 * optional @separator inserted between each of them. The returned string 1050 * should be freed with g_free(). 1051 * 1052 * If @str_array has no items, the return value will be an 1053 * empty string. If @str_array contains a single item, @separator will not 1054 * appear in the resulting string. 1055 * 1056 * Params: 1057 * separator = a string to insert between each of the 1058 * strings, or %NULL 1059 * strArray = a %NULL-terminated array of strings to join 1060 * 1061 * Returns: a newly-allocated string containing all of the strings joined 1062 * together, with @separator between them 1063 */ 1064 public static string strjoinv(string separator, string[] strArray) 1065 { 1066 auto retStr = g_strjoinv(Str.toStringz(separator), Str.toStringzArray(strArray)); 1067 1068 scope(exit) Str.freeString(retStr); 1069 return Str.toString(retStr); 1070 } 1071 1072 /** 1073 * Portability wrapper that calls strlcat() on systems which have it, 1074 * and emulates it otherwise. Appends nul-terminated @src string to @dest, 1075 * guaranteeing nul-termination for @dest. The total size of @dest won't 1076 * exceed @dest_size. 1077 * 1078 * At most @dest_size - 1 characters will be copied. Unlike strncat(), 1079 * @dest_size is the full size of dest, not the space left over. This 1080 * function does not allocate memory. It always nul-terminates (unless 1081 * @dest_size == 0 or there were no nul characters in the @dest_size 1082 * characters of dest to start with). 1083 * 1084 * Caveat: this is supposedly a more secure alternative to strcat() or 1085 * strncat(), but for real security g_strconcat() is harder to mess up. 1086 * 1087 * Params: 1088 * dest = destination buffer, already containing one nul-terminated string 1089 * src = source buffer 1090 * destSize = length of @dest buffer in bytes (not length of existing string 1091 * inside @dest) 1092 * 1093 * Returns: size of attempted result, which is MIN (dest_size, strlen 1094 * (original dest)) + strlen (src), so if retval >= dest_size, 1095 * truncation occurred. 1096 */ 1097 public static size_t strlcat(string dest, string src, size_t destSize) 1098 { 1099 return g_strlcat(Str.toStringz(dest), Str.toStringz(src), destSize); 1100 } 1101 1102 /** 1103 * Portability wrapper that calls strlcpy() on systems which have it, 1104 * and emulates strlcpy() otherwise. Copies @src to @dest; @dest is 1105 * guaranteed to be nul-terminated; @src must be nul-terminated; 1106 * @dest_size is the buffer size, not the number of bytes to copy. 1107 * 1108 * At most @dest_size - 1 characters will be copied. Always nul-terminates 1109 * (unless @dest_size is 0). This function does not allocate memory. Unlike 1110 * strncpy(), this function doesn't pad @dest (so it's often faster). It 1111 * returns the size of the attempted result, strlen (src), so if 1112 * @retval >= @dest_size, truncation occurred. 1113 * 1114 * Caveat: strlcpy() is supposedly more secure than strcpy() or strncpy(), 1115 * but if you really want to avoid screwups, g_strdup() is an even better 1116 * idea. 1117 * 1118 * Params: 1119 * dest = destination buffer 1120 * src = source buffer 1121 * destSize = length of @dest in bytes 1122 * 1123 * Returns: length of @src 1124 */ 1125 public static size_t strlcpy(string dest, string src, size_t destSize) 1126 { 1127 return g_strlcpy(Str.toStringz(dest), Str.toStringz(src), destSize); 1128 } 1129 1130 /** 1131 * A case-insensitive string comparison, corresponding to the standard 1132 * strncasecmp() function on platforms which support it. It is similar 1133 * to g_strcasecmp() except it only compares the first @n characters of 1134 * the strings. 1135 * 1136 * Deprecated: The problem with g_strncasecmp() is that it does 1137 * the comparison by calling toupper()/tolower(). These functions 1138 * are locale-specific and operate on single bytes. However, it is 1139 * impossible to handle things correctly from an internationalization 1140 * standpoint by operating on bytes, since characters may be multibyte. 1141 * Thus g_strncasecmp() is broken if your string is guaranteed to be 1142 * ASCII, since it is locale-sensitive, and it's broken if your string 1143 * is localized, since it doesn't work on many encodings at all, 1144 * including UTF-8, EUC-JP, etc. 1145 * 1146 * There are therefore two replacement techniques: g_ascii_strncasecmp(), 1147 * which only works on ASCII and is not locale-sensitive, and 1148 * g_utf8_casefold() followed by strcmp() on the resulting strings, 1149 * which is good for case-insensitive sorting of UTF-8. 1150 * 1151 * Params: 1152 * s1 = a string 1153 * s2 = a string to compare with @s1 1154 * n = the maximum number of characters to compare 1155 * 1156 * Returns: 0 if the strings match, a negative value if @s1 < @s2, 1157 * or a positive value if @s1 > @s2. 1158 */ 1159 public static int strncasecmp(string s1, string s2, uint n) 1160 { 1161 return g_strncasecmp(Str.toStringz(s1), Str.toStringz(s2), n); 1162 } 1163 1164 /** 1165 * Duplicates the first @n bytes of a string, returning a newly-allocated 1166 * buffer @n + 1 bytes long which will always be nul-terminated. If @str 1167 * is less than @n bytes long the buffer is padded with nuls. If @str is 1168 * %NULL it returns %NULL. The returned value should be freed when no longer 1169 * needed. 1170 * 1171 * To copy a number of characters from a UTF-8 encoded string, 1172 * use g_utf8_strncpy() instead. 1173 * 1174 * Params: 1175 * str = the string to duplicate 1176 * n = the maximum number of bytes to copy from @str 1177 * 1178 * Returns: a newly-allocated buffer containing the first @n bytes 1179 * of @str, nul-terminated 1180 */ 1181 public static string strndup(string str, size_t n) 1182 { 1183 auto retStr = g_strndup(Str.toStringz(str), n); 1184 1185 scope(exit) Str.freeString(retStr); 1186 return Str.toString(retStr); 1187 } 1188 1189 /** 1190 * Creates a new string @length bytes long filled with @fill_char. 1191 * The returned string should be freed when no longer needed. 1192 * 1193 * Params: 1194 * length = the length of the new string 1195 * fillChar = the byte to fill the string with 1196 * 1197 * Returns: a newly-allocated string filled the @fill_char 1198 */ 1199 public static string strnfill(size_t length, char fillChar) 1200 { 1201 auto retStr = g_strnfill(length, fillChar); 1202 1203 scope(exit) Str.freeString(retStr); 1204 return Str.toString(retStr); 1205 } 1206 1207 /** 1208 * Reverses all of the bytes in a string. For example, 1209 * `g_strreverse ("abcdef")` will result in "fedcba". 1210 * 1211 * Note that g_strreverse() doesn't work on UTF-8 strings 1212 * containing multibyte characters. For that purpose, use 1213 * g_utf8_strreverse(). 1214 * 1215 * Params: 1216 * string_ = the string to reverse 1217 * 1218 * Returns: the same pointer passed in as @string 1219 */ 1220 public static string strreverse(string string_) 1221 { 1222 auto retStr = g_strreverse(Str.toStringz(string_)); 1223 1224 scope(exit) Str.freeString(retStr); 1225 return Str.toString(retStr); 1226 } 1227 1228 /** 1229 * Searches the string @haystack for the last occurrence 1230 * of the string @needle. 1231 * 1232 * Params: 1233 * haystack = a nul-terminated string 1234 * needle = the nul-terminated string to search for 1235 * 1236 * Returns: a pointer to the found occurrence, or 1237 * %NULL if not found. 1238 */ 1239 public static string strrstr(string haystack, string needle) 1240 { 1241 auto retStr = g_strrstr(Str.toStringz(haystack), Str.toStringz(needle)); 1242 1243 scope(exit) Str.freeString(retStr); 1244 return Str.toString(retStr); 1245 } 1246 1247 /** 1248 * Searches the string @haystack for the last occurrence 1249 * of the string @needle, limiting the length of the search 1250 * to @haystack_len. 1251 * 1252 * Params: 1253 * haystack = a nul-terminated string 1254 * haystackLen = the maximum length of @haystack 1255 * needle = the nul-terminated string to search for 1256 * 1257 * Returns: a pointer to the found occurrence, or 1258 * %NULL if not found. 1259 */ 1260 public static string strrstrLen(string haystack, ptrdiff_t haystackLen, string needle) 1261 { 1262 auto retStr = g_strrstr_len(Str.toStringz(haystack), haystackLen, Str.toStringz(needle)); 1263 1264 scope(exit) Str.freeString(retStr); 1265 return Str.toString(retStr); 1266 } 1267 1268 /** 1269 * Returns a string describing the given signal, e.g. "Segmentation fault". 1270 * You should use this function in preference to strsignal(), because it 1271 * returns a string in UTF-8 encoding, and since not all platforms support 1272 * the strsignal() function. 1273 * 1274 * Params: 1275 * signum = the signal number. See the `signal` documentation 1276 * 1277 * Returns: a UTF-8 string describing the signal. If the signal is unknown, 1278 * it returns "unknown signal (<signum>)". 1279 */ 1280 public static string strsignal(int signum) 1281 { 1282 return Str.toString(g_strsignal(signum)); 1283 } 1284 1285 /** 1286 * Splits a string into a maximum of @max_tokens pieces, using the given 1287 * @delimiter. If @max_tokens is reached, the remainder of @string is 1288 * appended to the last token. 1289 * 1290 * As an example, the result of g_strsplit (":a:bc::d:", ":", -1) is a 1291 * %NULL-terminated vector containing the six strings "", "a", "bc", "", "d" 1292 * and "". 1293 * 1294 * As a special case, the result of splitting the empty string "" is an empty 1295 * vector, not a vector containing a single string. The reason for this 1296 * special case is that being able to represent a empty vector is typically 1297 * more useful than consistent handling of empty elements. If you do need 1298 * to represent empty elements, you'll need to check for the empty string 1299 * before calling g_strsplit(). 1300 * 1301 * Params: 1302 * string_ = a string to split 1303 * delimiter = a string which specifies the places at which to split 1304 * the string. The delimiter is not included in any of the resulting 1305 * strings, unless @max_tokens is reached. 1306 * maxTokens = the maximum number of pieces to split @string into. 1307 * If this is less than 1, the string is split completely. 1308 * 1309 * Returns: a newly-allocated %NULL-terminated array of strings. Use 1310 * g_strfreev() to free it. 1311 */ 1312 public static string[] strsplit(string string_, string delimiter, int maxTokens) 1313 { 1314 return Str.toStringArray(g_strsplit(Str.toStringz(string_), Str.toStringz(delimiter), maxTokens)); 1315 } 1316 1317 /** 1318 * Splits @string into a number of tokens not containing any of the characters 1319 * in @delimiter. A token is the (possibly empty) longest string that does not 1320 * contain any of the characters in @delimiters. If @max_tokens is reached, the 1321 * remainder is appended to the last token. 1322 * 1323 * For example the result of g_strsplit_set ("abc:def/ghi", ":/", -1) is a 1324 * %NULL-terminated vector containing the three strings "abc", "def", 1325 * and "ghi". 1326 * 1327 * The result of g_strsplit_set (":def/ghi:", ":/", -1) is a %NULL-terminated 1328 * vector containing the four strings "", "def", "ghi", and "". 1329 * 1330 * As a special case, the result of splitting the empty string "" is an empty 1331 * vector, not a vector containing a single string. The reason for this 1332 * special case is that being able to represent a empty vector is typically 1333 * more useful than consistent handling of empty elements. If you do need 1334 * to represent empty elements, you'll need to check for the empty string 1335 * before calling g_strsplit_set(). 1336 * 1337 * Note that this function works on bytes not characters, so it can't be used 1338 * to delimit UTF-8 strings for anything but ASCII characters. 1339 * 1340 * Params: 1341 * string_ = The string to be tokenized 1342 * delimiters = A nul-terminated string containing bytes that are used 1343 * to split the string. 1344 * maxTokens = The maximum number of tokens to split @string into. 1345 * If this is less than 1, the string is split completely 1346 * 1347 * Returns: a newly-allocated %NULL-terminated array of strings. Use 1348 * g_strfreev() to free it. 1349 * 1350 * Since: 2.4 1351 */ 1352 public static string[] strsplitSet(string string_, string delimiters, int maxTokens) 1353 { 1354 return Str.toStringArray(g_strsplit_set(Str.toStringz(string_), Str.toStringz(delimiters), maxTokens)); 1355 } 1356 1357 /** 1358 * Searches the string @haystack for the first occurrence 1359 * of the string @needle, limiting the length of the search 1360 * to @haystack_len. 1361 * 1362 * Params: 1363 * haystack = a string 1364 * haystackLen = the maximum length of @haystack. Note that -1 is 1365 * a valid length, if @haystack is nul-terminated, meaning it will 1366 * search through the whole string. 1367 * needle = the string to search for 1368 * 1369 * Returns: a pointer to the found occurrence, or 1370 * %NULL if not found. 1371 */ 1372 public static string strstrLen(string haystack, ptrdiff_t haystackLen, string needle) 1373 { 1374 auto retStr = g_strstr_len(Str.toStringz(haystack), haystackLen, Str.toStringz(needle)); 1375 1376 scope(exit) Str.freeString(retStr); 1377 return Str.toString(retStr); 1378 } 1379 1380 /** 1381 * Converts a string to a #gdouble value. 1382 * It calls the standard strtod() function to handle the conversion, but 1383 * if the string is not completely converted it attempts the conversion 1384 * again with g_ascii_strtod(), and returns the best match. 1385 * 1386 * This function should seldom be used. The normal situation when reading 1387 * numbers not for human consumption is to use g_ascii_strtod(). Only when 1388 * you know that you must expect both locale formatted and C formatted numbers 1389 * should you use this. Make sure that you don't pass strings such as comma 1390 * separated lists of values, since the commas may be interpreted as a decimal 1391 * point in some locales, causing unexpected results. 1392 * 1393 * Params: 1394 * nptr = the string to convert to a numeric value. 1395 * endptr = if non-%NULL, it returns the 1396 * character after the last character used in the conversion. 1397 * 1398 * Returns: the #gdouble value. 1399 */ 1400 public static double strtod(string nptr, out string endptr) 1401 { 1402 char* outendptr = null; 1403 1404 auto p = g_strtod(Str.toStringz(nptr), &outendptr); 1405 1406 endptr = Str.toString(outendptr); 1407 1408 return p; 1409 } 1410 1411 /** 1412 * Converts a string to upper case. 1413 * 1414 * Deprecated: This function is totally broken for the reasons 1415 * discussed in the g_strncasecmp() docs - use g_ascii_strup() 1416 * or g_utf8_strup() instead. 1417 * 1418 * Params: 1419 * string_ = the string to convert 1420 * 1421 * Returns: the string 1422 */ 1423 public static string strup(string string_) 1424 { 1425 auto retStr = g_strup(Str.toStringz(string_)); 1426 1427 scope(exit) Str.freeString(retStr); 1428 return Str.toString(retStr); 1429 } 1430 1431 /** */ 1432 public static GType strvGetType() 1433 { 1434 return g_strv_get_type(); 1435 } 1436 1437 /** 1438 * Returns the length of the given %NULL-terminated 1439 * string array @str_array. @str_array must not be %NULL. 1440 * 1441 * Params: 1442 * strArray = a %NULL-terminated array of strings 1443 * 1444 * Returns: length of @str_array. 1445 * 1446 * Since: 2.6 1447 */ 1448 public static uint strvLength(string[] strArray) 1449 { 1450 return g_strv_length(Str.toStringzArray(strArray)); 1451 } 1452 1453 /** 1454 * Checks if @strv contains @str. @strv must not be %NULL. 1455 * 1456 * Params: 1457 * strv = a %NULL-terminated array of strings 1458 * str = a string 1459 * 1460 * Returns: %TRUE if @str is an element of @strv, according to g_str_equal(). 1461 * 1462 * Since: 2.44 1463 */ 1464 public static bool strvContains(string strv, string str) 1465 { 1466 return g_strv_contains(Str.toStringz(strv), Str.toStringz(str)) != 0; 1467 } 1468 1469 /** 1470 * An implementation of the GNU vasprintf() function which supports 1471 * positional parameters, as specified in the Single Unix Specification. 1472 * This function is similar to g_vsprintf(), except that it allocates a 1473 * string to hold the output, instead of putting the output in a buffer 1474 * you allocate in advance. 1475 * 1476 * `glib/gprintf.h` must be explicitly included in order to use this function. 1477 * 1478 * Params: 1479 * string_ = the return location for the newly-allocated string. 1480 * format = a standard printf() format string, but notice 1481 * [string precision pitfalls][string-precision] 1482 * args = the list of arguments to insert in the output. 1483 * 1484 * Returns: the number of bytes printed. 1485 * 1486 * Since: 2.4 1487 */ 1488 public static int vasprintf(string[] string_, string format, void* args) 1489 { 1490 return g_vasprintf(Str.toStringzArray(string_), Str.toStringz(format), args); 1491 } 1492 1493 /** 1494 * An implementation of the standard vprintf() function which supports 1495 * positional parameters, as specified in the Single Unix Specification. 1496 * 1497 * `glib/gprintf.h` must be explicitly included in order to use this function. 1498 * 1499 * Params: 1500 * format = a standard printf() format string, but notice 1501 * [string precision pitfalls][string-precision] 1502 * args = the list of arguments to insert in the output. 1503 * 1504 * Returns: the number of bytes printed. 1505 * 1506 * Since: 2.2 1507 */ 1508 public static int vprintf(string format, void* args) 1509 { 1510 return g_vprintf(Str.toStringz(format), args); 1511 } 1512 1513 /** 1514 * A safer form of the standard vsprintf() function. The output is guaranteed 1515 * to not exceed @n characters (including the terminating nul character), so 1516 * it is easy to ensure that a buffer overflow cannot occur. 1517 * 1518 * See also g_strdup_vprintf(). 1519 * 1520 * In versions of GLib prior to 1.2.3, this function may return -1 if the 1521 * output was truncated, and the truncated string may not be nul-terminated. 1522 * In versions prior to 1.3.12, this function returns the length of the output 1523 * string. 1524 * 1525 * The return value of g_vsnprintf() conforms to the vsnprintf() function 1526 * as standardized in ISO C99. Note that this is different from traditional 1527 * vsnprintf(), which returns the length of the output string. 1528 * 1529 * The format string may contain positional parameters, as specified in 1530 * the Single Unix Specification. 1531 * 1532 * Params: 1533 * string_ = the buffer to hold the output. 1534 * n = the maximum number of bytes to produce (including the 1535 * terminating nul character). 1536 * format = a standard printf() format string, but notice 1537 * string precision pitfalls][string-precision] 1538 * args = the list of arguments to insert in the output. 1539 * 1540 * Returns: the number of bytes which would be produced if the buffer 1541 * was large enough. 1542 */ 1543 public static int vsnprintf(string string_, gulong n, string format, void* args) 1544 { 1545 return g_vsnprintf(Str.toStringz(string_), n, Str.toStringz(format), args); 1546 } 1547 1548 /** 1549 * An implementation of the standard vsprintf() function which supports 1550 * positional parameters, as specified in the Single Unix Specification. 1551 * 1552 * `glib/gprintf.h` must be explicitly included in order to use this function. 1553 * 1554 * Params: 1555 * string_ = the buffer to hold the output. 1556 * format = a standard printf() format string, but notice 1557 * [string precision pitfalls][string-precision] 1558 * args = the list of arguments to insert in the output. 1559 * 1560 * Returns: the number of bytes printed. 1561 * 1562 * Since: 2.2 1563 */ 1564 public static int vsprintf(string string_, string format, void* args) 1565 { 1566 return g_vsprintf(Str.toStringz(string_), Str.toStringz(format), args); 1567 } 1568 1569 /** 1570 * An implementation of the standard fprintf() function which supports 1571 * positional parameters, as specified in the Single Unix Specification. 1572 * 1573 * `glib/gprintf.h` must be explicitly included in order to use this function. 1574 * 1575 * Params: 1576 * file = the stream to write to. 1577 * format = a standard printf() format string, but notice 1578 * [string precision pitfalls][string-precision] 1579 * args = the list of arguments to insert in the output. 1580 * 1581 * Returns: the number of bytes printed. 1582 * 1583 * Since: 2.2 1584 */ 1585 public static int vfprintf(FILE* file, string format, void* args) 1586 { 1587 return g_vfprintf(file, Str.toStringz(format), args); 1588 } 1589 1590 /** 1591 * A convenience function for converting a string to a signed number. 1592 * 1593 * This function assumes that @str contains only a number of the given 1594 * @base that is within inclusive bounds limited by @min and @max. If 1595 * this is true, then the converted number is stored in @out_num. An 1596 * empty string is not a valid input. A string with leading or 1597 * trailing whitespace is also an invalid input. 1598 * 1599 * @base can be between 2 and 36 inclusive. Hexadecimal numbers must 1600 * not be prefixed with "0x" or "0X". Such a problem does not exist 1601 * for octal numbers, since they were usually prefixed with a zero 1602 * which does not change the value of the parsed number. 1603 * 1604 * Parsing failures result in an error with the %G_NUMBER_PARSER_ERROR 1605 * domain. If the input is invalid, the error code will be 1606 * %G_NUMBER_PARSER_ERROR_INVALID. If the parsed number is out of 1607 * bounds - %G_NUMBER_PARSER_ERROR_OUT_OF_BOUNDS. 1608 * 1609 * See g_ascii_strtoll() if you have more complex needs such as 1610 * parsing a string which starts with a number, but then has other 1611 * characters. 1612 * 1613 * Params: 1614 * str = a string 1615 * base = base of a parsed number 1616 * min = a lower bound (inclusive) 1617 * max = an upper bound (inclusive) 1618 * outNum = a return location for a number 1619 * 1620 * Returns: %TRUE if @str was a number, otherwise %FALSE. 1621 * 1622 * Since: 2.54 1623 * 1624 * Throws: GException on failure. 1625 */ 1626 public static bool asciiStringToSigned(string str, uint base, long min, long max, out long outNum) 1627 { 1628 GError* err = null; 1629 1630 auto p = g_ascii_string_to_signed(Str.toStringz(str), base, min, max, &outNum, &err) != 0; 1631 1632 if (err !is null) 1633 { 1634 throw new GException( new ErrorG(err) ); 1635 } 1636 1637 return p; 1638 } 1639 1640 /** 1641 * A convenience function for converting a string to an unsigned number. 1642 * 1643 * This function assumes that @str contains only a number of the given 1644 * @base that is within inclusive bounds limited by @min and @max. If 1645 * this is true, then the converted number is stored in @out_num. An 1646 * empty string is not a valid input. A string with leading or 1647 * trailing whitespace is also an invalid input. A string with a leading sign 1648 * (`-` or `+`) is not a valid input for the unsigned parser. 1649 * 1650 * @base can be between 2 and 36 inclusive. Hexadecimal numbers must 1651 * not be prefixed with "0x" or "0X". Such a problem does not exist 1652 * for octal numbers, since they were usually prefixed with a zero 1653 * which does not change the value of the parsed number. 1654 * 1655 * Parsing failures result in an error with the %G_NUMBER_PARSER_ERROR 1656 * domain. If the input is invalid, the error code will be 1657 * %G_NUMBER_PARSER_ERROR_INVALID. If the parsed number is out of 1658 * bounds - %G_NUMBER_PARSER_ERROR_OUT_OF_BOUNDS. 1659 * 1660 * See g_ascii_strtoull() if you have more complex needs such as 1661 * parsing a string which starts with a number, but then has other 1662 * characters. 1663 * 1664 * Params: 1665 * str = a string 1666 * base = base of a parsed number 1667 * min = a lower bound (inclusive) 1668 * max = an upper bound (inclusive) 1669 * outNum = a return location for a number 1670 * 1671 * Returns: %TRUE if @str was a number, otherwise %FALSE. 1672 * 1673 * Since: 2.54 1674 * 1675 * Throws: GException on failure. 1676 */ 1677 public static bool asciiStringToUnsigned(string str, uint base, ulong min, ulong max, out ulong outNum) 1678 { 1679 GError* err = null; 1680 1681 auto p = g_ascii_string_to_unsigned(Str.toStringz(str), base, min, max, &outNum, &err) != 0; 1682 1683 if (err !is null) 1684 { 1685 throw new GException( new ErrorG(err) ); 1686 } 1687 1688 return p; 1689 } 1690 1691 /** 1692 * Checks if @strv1 and @strv2 contain exactly the same elements in exactly the 1693 * same order. Elements are compared using g_str_equal(). To match independently 1694 * of order, sort the arrays first (using g_qsort_with_data() or similar). 1695 * 1696 * Two empty arrays are considered equal. Neither @strv1 not @strv2 may be 1697 * %NULL. 1698 * 1699 * Params: 1700 * strv1 = a %NULL-terminated array of strings 1701 * strv2 = another %NULL-terminated array of strings 1702 * 1703 * Returns: %TRUE if @strv1 and @strv2 are equal 1704 * 1705 * Since: 2.60 1706 */ 1707 public static bool strvEqual(string strv1, string strv2) 1708 { 1709 return g_strv_equal(Str.toStringz(strv1), Str.toStringz(strv2)) != 0; 1710 } 1711 }