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]).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]; 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 * This function is typically used when reading configuration 428 * files or other non-user input that should be locale independent. 429 * To handle input from the user you should normally use the 430 * locale-sensitive system strtoull() function. 431 * 432 * If the correct value would cause overflow, %G_MAXUINT64 433 * is returned, and `ERANGE` is stored in `errno`. 434 * If the base is outside the valid range, zero is returned, and 435 * `EINVAL` is stored in `errno`. 436 * If the string conversion fails, zero is returned, and @endptr returns 437 * @nptr (if @endptr is non-%NULL). 438 * 439 * Params: 440 * nptr = the string to convert to a numeric value. 441 * endptr = if non-%NULL, it returns the 442 * character after the last character used in the conversion. 443 * base = to be used for the conversion, 2..36 or 0 444 * 445 * Returns: the #guint64 value or zero on error. 446 * 447 * Since: 2.2 448 */ 449 public static ulong asciiStrtoull(string nptr, out string endptr, uint base) 450 { 451 char* outendptr = null; 452 453 auto p = g_ascii_strtoull(Str.toStringz(nptr), &outendptr, base); 454 455 endptr = Str.toString(outendptr); 456 457 return p; 458 } 459 460 /** 461 * Converts all lower case ASCII letters to upper case ASCII letters. 462 * 463 * Params: 464 * str = a string 465 * len = length of @str in bytes, or -1 if @str is nul-terminated 466 * 467 * Returns: a newly allocated string, with all the lower case 468 * characters in @str converted to upper case, with semantics that 469 * exactly match g_ascii_toupper(). (Note that this is unlike the 470 * old g_strup(), which modified the string in place.) 471 */ 472 public static string asciiStrup(string str, ptrdiff_t len) 473 { 474 auto retStr = g_ascii_strup(Str.toStringz(str), len); 475 476 scope(exit) Str.freeString(retStr); 477 return Str.toString(retStr); 478 } 479 480 /** 481 * Convert a character to ASCII lower case. 482 * 483 * Unlike the standard C library tolower() function, this only 484 * recognizes standard ASCII letters and ignores the locale, returning 485 * all non-ASCII characters unchanged, even if they are lower case 486 * letters in a particular character set. Also unlike the standard 487 * library function, this takes and returns a char, not an int, so 488 * don't call it on %EOF but no need to worry about casting to #guchar 489 * before passing a possibly non-ASCII character in. 490 * 491 * Params: 492 * c = any character 493 * 494 * Returns: the result of converting @c to lower case. If @c is 495 * not an ASCII upper case letter, @c is returned unchanged. 496 */ 497 public static char asciiTolower(char c) 498 { 499 return g_ascii_tolower(c); 500 } 501 502 /** 503 * Convert a character to ASCII upper case. 504 * 505 * Unlike the standard C library toupper() function, this only 506 * recognizes standard ASCII letters and ignores the locale, returning 507 * all non-ASCII characters unchanged, even if they are upper case 508 * letters in a particular character set. Also unlike the standard 509 * library function, this takes and returns a char, not an int, so 510 * don't call it on %EOF but no need to worry about casting to #guchar 511 * before passing a possibly non-ASCII character in. 512 * 513 * Params: 514 * c = any character 515 * 516 * Returns: the result of converting @c to upper case. If @c is not 517 * an ASCII lower case letter, @c is returned unchanged. 518 */ 519 public static char asciiToupper(char c) 520 { 521 return g_ascii_toupper(c); 522 } 523 524 /** 525 * Determines the numeric value of a character as a hexidecimal 526 * digit. Differs from g_unichar_xdigit_value() because it takes 527 * a char, so there's no worry about sign extension if characters 528 * are signed. 529 * 530 * Params: 531 * c = an ASCII character. 532 * 533 * Returns: If @c is a hex digit (according to g_ascii_isxdigit()), 534 * its numeric value. Otherwise, -1. 535 */ 536 public static int asciiXdigitValue(char c) 537 { 538 return g_ascii_xdigit_value(c); 539 } 540 541 /** 542 * Calculates the maximum space needed to store the output 543 * of the sprintf() function. 544 * 545 * Params: 546 * format = the format string. See the printf() documentation 547 * args = the parameters to be inserted into the format string 548 * 549 * Returns: the maximum space needed to store the formatted string 550 */ 551 public static size_t printfStringUpperBound(string format, void* args) 552 { 553 return g_printf_string_upper_bound(Str.toStringz(format), args); 554 } 555 556 /** 557 * Copies a nul-terminated string into the dest buffer, include the 558 * trailing nul, and return a pointer to the trailing nul byte. 559 * This is useful for concatenating multiple strings together 560 * without having to repeatedly scan for the end. 561 * 562 * Params: 563 * dest = destination buffer. 564 * src = source string. 565 * 566 * Returns: a pointer to trailing nul byte. 567 */ 568 public static string stpcpy(string dest, string src) 569 { 570 auto retStr = g_stpcpy(Str.toStringz(dest), Str.toStringz(src)); 571 572 scope(exit) Str.freeString(retStr); 573 return Str.toString(retStr); 574 } 575 576 /** 577 * Looks whether the string @str begins with @prefix. 578 * 579 * Params: 580 * str = a nul-terminated string 581 * prefix = the nul-terminated prefix to look for 582 * 583 * Returns: %TRUE if @str begins with @prefix, %FALSE otherwise. 584 * 585 * Since: 2.2 586 */ 587 public static bool hasPrefix(string str, string prefix) 588 { 589 return g_str_has_prefix(Str.toStringz(str), Str.toStringz(prefix)) != 0; 590 } 591 592 /** 593 * Looks whether the string @str ends with @suffix. 594 * 595 * Params: 596 * str = a nul-terminated string 597 * suffix = the nul-terminated suffix to look for 598 * 599 * Returns: %TRUE if @str end with @suffix, %FALSE otherwise. 600 * 601 * Since: 2.2 602 */ 603 public static bool hasSuffix(string str, string suffix) 604 { 605 return g_str_has_suffix(Str.toStringz(str), Str.toStringz(suffix)) != 0; 606 } 607 608 /** 609 * Determines if a string is pure ASCII. A string is pure ASCII if it 610 * contains no bytes with the high bit set. 611 * 612 * Params: 613 * str = a string 614 * 615 * Returns: %TRUE if @str is ASCII 616 * 617 * Since: 2.40 618 */ 619 public static bool isAscii(string str) 620 { 621 return g_str_is_ascii(Str.toStringz(str)) != 0; 622 } 623 624 /** 625 * Checks if a search conducted for @search_term should match 626 * @potential_hit. 627 * 628 * This function calls g_str_tokenize_and_fold() on both 629 * @search_term and @potential_hit. ASCII alternates are never taken 630 * for @search_term but will be taken for @potential_hit according to 631 * the value of @accept_alternates. 632 * 633 * A hit occurs when each folded token in @search_term is a prefix of a 634 * folded token from @potential_hit. 635 * 636 * Depending on how you're performing the search, it will typically be 637 * faster to call g_str_tokenize_and_fold() on each string in 638 * your corpus and build an index on the returned folded tokens, then 639 * call g_str_tokenize_and_fold() on the search term and 640 * perform lookups into that index. 641 * 642 * As some examples, searching for ‘fred’ would match the potential hit 643 * ‘Smith, Fred’ and also ‘Frédéric’. Searching for ‘Fréd’ would match 644 * ‘Frédéric’ but not ‘Frederic’ (due to the one-directional nature of 645 * accent matching). Searching ‘fo’ would match ‘Foo’ and ‘Bar Foo 646 * Baz’, but not ‘SFO’ (because no word has ‘fo’ as a prefix). 647 * 648 * Params: 649 * searchTerm = the search term from the user 650 * potentialHit = the text that may be a hit 651 * acceptAlternates = %TRUE to accept ASCII alternates 652 * 653 * Returns: %TRUE if @potential_hit is a hit 654 * 655 * Since: 2.40 656 */ 657 public static bool matchString(string searchTerm, string potentialHit, bool acceptAlternates) 658 { 659 return g_str_match_string(Str.toStringz(searchTerm), Str.toStringz(potentialHit), acceptAlternates) != 0; 660 } 661 662 /** 663 * Transliterate @str to plain ASCII. 664 * 665 * For best results, @str should be in composed normalised form. 666 * 667 * This function performs a reasonably good set of character 668 * replacements. The particular set of replacements that is done may 669 * change by version or even by runtime environment. 670 * 671 * If the source language of @str is known, it can used to improve the 672 * accuracy of the translation by passing it as @from_locale. It should 673 * be a valid POSIX locale string (of the form 674 * "language[_territory][.codeset][@modifier]"). 675 * 676 * If @from_locale is %NULL then the current locale is used. 677 * 678 * If you want to do translation for no specific locale, and you want it 679 * to be done independently of the currently locale, specify "C" for 680 * @from_locale. 681 * 682 * Params: 683 * str = a string, in UTF-8 684 * fromLocale = the source locale, if known 685 * 686 * Returns: a string in plain ASCII 687 * 688 * Since: 2.40 689 */ 690 public static string toAscii(string str, string fromLocale) 691 { 692 auto retStr = g_str_to_ascii(Str.toStringz(str), Str.toStringz(fromLocale)); 693 694 scope(exit) Str.freeString(retStr); 695 return Str.toString(retStr); 696 } 697 698 /** 699 * Tokenises @string and performs folding on each token. 700 * 701 * A token is a non-empty sequence of alphanumeric characters in the 702 * source string, separated by non-alphanumeric characters. An 703 * "alphanumeric" character for this purpose is one that matches 704 * g_unichar_isalnum() or g_unichar_ismark(). 705 * 706 * Each token is then (Unicode) normalised and case-folded. If 707 * @ascii_alternates is non-%NULL and some of the returned tokens 708 * contain non-ASCII characters, ASCII alternatives will be generated. 709 * 710 * The number of ASCII alternatives that are generated and the method 711 * for doing so is unspecified, but @translit_locale (if specified) may 712 * improve the transliteration if the language of the source string is 713 * known. 714 * 715 * Params: 716 * str = a string 717 * translitLocale = the language code (like 'de' or 718 * 'en_GB') from which @string originates 719 * asciiAlternates = a 720 * return location for ASCII alternates 721 * 722 * Returns: the folded tokens 723 * 724 * Since: 2.40 725 */ 726 public static string[] tokenizeAndFold(string str, string translitLocale, out string[] asciiAlternates) 727 { 728 char** outasciiAlternates = null; 729 730 auto retStr = g_str_tokenize_and_fold(Str.toStringz(str), Str.toStringz(translitLocale), &outasciiAlternates); 731 732 asciiAlternates = Str.toStringArray(outasciiAlternates); 733 734 scope(exit) Str.freeStringArray(retStr); 735 return Str.toStringArray(retStr); 736 } 737 738 /** 739 * For each character in @string, if the character is not in @valid_chars, 740 * replaces the character with @substitutor. Modifies @string in place, 741 * and return @string itself, not a copy. The return value is to allow 742 * nesting such as 743 * |[<!-- language="C" --> 744 * g_ascii_strup (g_strcanon (str, "abc", '?')) 745 * ]| 746 * 747 * Params: 748 * str = a nul-terminated array of bytes 749 * validChars = bytes permitted in @string 750 * substitutor = replacement character for disallowed bytes 751 * 752 * Returns: @string 753 */ 754 public static string strcanon(string str, string validChars, char substitutor) 755 { 756 auto retStr = g_strcanon(Str.toStringz(str), Str.toStringz(validChars), substitutor); 757 758 scope(exit) Str.freeString(retStr); 759 return Str.toString(retStr); 760 } 761 762 /** 763 * A case-insensitive string comparison, corresponding to the standard 764 * strcasecmp() function on platforms which support it. 765 * 766 * Deprecated: See g_strncasecmp() for a discussion of why this 767 * function is deprecated and how to replace it. 768 * 769 * Params: 770 * s1 = a string 771 * s2 = a string to compare with @s1 772 * 773 * Returns: 0 if the strings match, a negative value if @s1 < @s2, 774 * or a positive value if @s1 > @s2. 775 */ 776 public static int strcasecmp(string s1, string s2) 777 { 778 return g_strcasecmp(Str.toStringz(s1), Str.toStringz(s2)); 779 } 780 781 /** 782 * Removes trailing whitespace from a string. 783 * 784 * This function doesn't allocate or reallocate any memory; 785 * it modifies @string in place. Therefore, it cannot be used 786 * on statically allocated strings. 787 * 788 * The pointer to @string is returned to allow the nesting of functions. 789 * 790 * Also see g_strchug() and g_strstrip(). 791 * 792 * Params: 793 * str = a string to remove the trailing whitespace from 794 * 795 * Returns: @string 796 */ 797 public static string strchomp(string str) 798 { 799 auto retStr = g_strchomp(Str.toStringz(str)); 800 801 scope(exit) Str.freeString(retStr); 802 return Str.toString(retStr); 803 } 804 805 /** 806 * Removes leading whitespace from a string, by moving the rest 807 * of the characters forward. 808 * 809 * This function doesn't allocate or reallocate any memory; 810 * it modifies @string in place. Therefore, it cannot be used on 811 * statically allocated strings. 812 * 813 * The pointer to @string is returned to allow the nesting of functions. 814 * 815 * Also see g_strchomp() and g_strstrip(). 816 * 817 * Params: 818 * str = a string to remove the leading whitespace from 819 * 820 * Returns: @string 821 */ 822 public static string strchug(string str) 823 { 824 auto retStr = g_strchug(Str.toStringz(str)); 825 826 scope(exit) Str.freeString(retStr); 827 return Str.toString(retStr); 828 } 829 830 /** 831 * Compares @str1 and @str2 like strcmp(). Handles %NULL 832 * gracefully by sorting it before non-%NULL strings. 833 * Comparing two %NULL pointers returns 0. 834 * 835 * Params: 836 * str1 = a C string or %NULL 837 * str2 = another C string or %NULL 838 * 839 * Returns: an integer less than, equal to, or greater than zero, if @str1 is <, == or > than @str2. 840 * 841 * Since: 2.16 842 */ 843 public static int strcmp0(string str1, string str2) 844 { 845 return g_strcmp0(Str.toStringz(str1), Str.toStringz(str2)); 846 } 847 848 /** 849 * Replaces all escaped characters with their one byte equivalent. 850 * 851 * This function does the reverse conversion of g_strescape(). 852 * 853 * Params: 854 * source = a string to compress 855 * 856 * Returns: a newly-allocated copy of @source with all escaped 857 * character compressed 858 */ 859 public static string strcompress(string source) 860 { 861 auto retStr = g_strcompress(Str.toStringz(source)); 862 863 scope(exit) Str.freeString(retStr); 864 return Str.toString(retStr); 865 } 866 867 /** 868 * Converts any delimiter characters in @string to @new_delimiter. 869 * Any characters in @string which are found in @delimiters are 870 * changed to the @new_delimiter character. Modifies @string in place, 871 * and returns @string itself, not a copy. The return value is to 872 * allow nesting such as 873 * |[<!-- language="C" --> 874 * g_ascii_strup (g_strdelimit (str, "abc", '?')) 875 * ]| 876 * 877 * Params: 878 * str = the string to convert 879 * delimiters = a string containing the current delimiters, 880 * or %NULL to use the standard delimiters defined in #G_STR_DELIMITERS 881 * newDelimiter = the new delimiter character 882 * 883 * Returns: @string 884 */ 885 public static string strdelimit(string str, string delimiters, char newDelimiter) 886 { 887 auto retStr = g_strdelimit(Str.toStringz(str), Str.toStringz(delimiters), newDelimiter); 888 889 scope(exit) Str.freeString(retStr); 890 return Str.toString(retStr); 891 } 892 893 /** 894 * Converts a string to lower case. 895 * 896 * Deprecated: This function is totally broken for the reasons discussed 897 * in the g_strncasecmp() docs - use g_ascii_strdown() or g_utf8_strdown() 898 * instead. 899 * 900 * Params: 901 * str = the string to convert. 902 * 903 * Returns: the string 904 */ 905 public static string strdown(string str) 906 { 907 auto retStr = g_strdown(Str.toStringz(str)); 908 909 scope(exit) Str.freeString(retStr); 910 return Str.toString(retStr); 911 } 912 913 /** 914 * Duplicates a string. If @str is %NULL it returns %NULL. 915 * The returned string should be freed with g_free() 916 * when no longer needed. 917 * 918 * Params: 919 * str = the string to duplicate 920 * 921 * Returns: a newly-allocated copy of @str 922 */ 923 public static string strdup(string str) 924 { 925 auto retStr = g_strdup(Str.toStringz(str)); 926 927 scope(exit) Str.freeString(retStr); 928 return Str.toString(retStr); 929 } 930 931 /** 932 * Similar to the standard C vsprintf() function but safer, since it 933 * calculates the maximum space required and allocates memory to hold 934 * the result. The returned string should be freed with g_free() when 935 * no longer needed. 936 * 937 * See also g_vasprintf(), which offers the same functionality, but 938 * additionally returns the length of the allocated string. 939 * 940 * Params: 941 * format = a standard printf() format string, but notice 942 * [string precision pitfalls][string-precision] 943 * args = the list of parameters to insert into the format string 944 * 945 * Returns: a newly-allocated string holding the result 946 */ 947 public static string strdupVprintf(string format, void* args) 948 { 949 auto retStr = g_strdup_vprintf(Str.toStringz(format), args); 950 951 scope(exit) Str.freeString(retStr); 952 return Str.toString(retStr); 953 } 954 955 /** 956 * Copies %NULL-terminated array of strings. The copy is a deep copy; 957 * the new array should be freed by first freeing each string, then 958 * the array itself. g_strfreev() does this for you. If called 959 * on a %NULL value, g_strdupv() simply returns %NULL. 960 * 961 * Params: 962 * strArray = a %NULL-terminated array of strings 963 * 964 * Returns: a new %NULL-terminated array of strings. 965 */ 966 public static string[] strdupv(string[] strArray) 967 { 968 return Str.toStringArray(g_strdupv(Str.toStringzArray(strArray))); 969 } 970 971 /** 972 * Returns a string corresponding to the given error code, e.g. "no 973 * such process". Unlike strerror(), this always returns a string in 974 * UTF-8 encoding, and the pointer is guaranteed to remain valid for 975 * the lifetime of the process. 976 * 977 * Note that the string may be translated according to the current locale. 978 * 979 * The value of %errno will not be changed by this function. However, it may 980 * be changed by intermediate function calls, so you should save its value 981 * as soon as the call returns: 982 * |[ 983 * int saved_errno; 984 * 985 * ret = read (blah); 986 * saved_errno = errno; 987 * 988 * g_strerror (saved_errno); 989 * ]| 990 * 991 * Params: 992 * errnum = the system error number. See the standard C %errno 993 * documentation 994 * 995 * Returns: a UTF-8 string describing the error code. If the error code 996 * is unknown, it returns a string like "unknown error (<code>)". 997 */ 998 public static string strerror(int errnum) 999 { 1000 return Str.toString(g_strerror(errnum)); 1001 } 1002 1003 /** 1004 * Escapes the special characters '\b', '\f', '\n', '\r', '\t', '\v', '\' 1005 * and '"' in the string @source by inserting a '\' before 1006 * them. Additionally all characters in the range 0x01-0x1F (everything 1007 * below SPACE) and in the range 0x7F-0xFF (all non-ASCII chars) are 1008 * replaced with a '\' followed by their octal representation. 1009 * Characters supplied in @exceptions are not escaped. 1010 * 1011 * g_strcompress() does the reverse conversion. 1012 * 1013 * Params: 1014 * source = a string to escape 1015 * exceptions = a string of characters not to escape in @source 1016 * 1017 * Returns: a newly-allocated copy of @source with certain 1018 * characters escaped. See above. 1019 */ 1020 public static string strescape(string source, string exceptions) 1021 { 1022 auto retStr = g_strescape(Str.toStringz(source), Str.toStringz(exceptions)); 1023 1024 scope(exit) Str.freeString(retStr); 1025 return Str.toString(retStr); 1026 } 1027 1028 /** 1029 * Frees a %NULL-terminated array of strings, as well as each 1030 * string it contains. 1031 * 1032 * If @str_array is %NULL, this function simply returns. 1033 * 1034 * Params: 1035 * strArray = a %NULL-terminated array of strings to free 1036 */ 1037 public static void strfreev(string[] strArray) 1038 { 1039 g_strfreev(Str.toStringzArray(strArray)); 1040 } 1041 1042 /** 1043 * Joins a number of strings together to form one long string, with the 1044 * optional @separator inserted between each of them. The returned string 1045 * should be freed with g_free(). 1046 * 1047 * If @str_array has no items, the return value will be an 1048 * empty string. If @str_array contains a single item, @separator will not 1049 * appear in the resulting string. 1050 * 1051 * Params: 1052 * separator = a string to insert between each of the 1053 * strings, or %NULL 1054 * strArray = a %NULL-terminated array of strings to join 1055 * 1056 * Returns: a newly-allocated string containing all of the strings joined 1057 * together, with @separator between them 1058 */ 1059 public static string strjoinv(string separator, string[] strArray) 1060 { 1061 auto retStr = g_strjoinv(Str.toStringz(separator), Str.toStringzArray(strArray)); 1062 1063 scope(exit) Str.freeString(retStr); 1064 return Str.toString(retStr); 1065 } 1066 1067 /** 1068 * Portability wrapper that calls strlcat() on systems which have it, 1069 * and emulates it otherwise. Appends nul-terminated @src string to @dest, 1070 * guaranteeing nul-termination for @dest. The total size of @dest won't 1071 * exceed @dest_size. 1072 * 1073 * At most @dest_size - 1 characters will be copied. Unlike strncat(), 1074 * @dest_size is the full size of dest, not the space left over. This 1075 * function does not allocate memory. It always nul-terminates (unless 1076 * @dest_size == 0 or there were no nul characters in the @dest_size 1077 * characters of dest to start with). 1078 * 1079 * Caveat: this is supposedly a more secure alternative to strcat() or 1080 * strncat(), but for real security g_strconcat() is harder to mess up. 1081 * 1082 * Params: 1083 * dest = destination buffer, already containing one nul-terminated string 1084 * src = source buffer 1085 * destSize = length of @dest buffer in bytes (not length of existing string 1086 * inside @dest) 1087 * 1088 * Returns: size of attempted result, which is MIN (dest_size, strlen 1089 * (original dest)) + strlen (src), so if retval >= dest_size, 1090 * truncation occurred. 1091 */ 1092 public static size_t strlcat(string dest, string src, size_t destSize) 1093 { 1094 return g_strlcat(Str.toStringz(dest), Str.toStringz(src), destSize); 1095 } 1096 1097 /** 1098 * Portability wrapper that calls strlcpy() on systems which have it, 1099 * and emulates strlcpy() otherwise. Copies @src to @dest; @dest is 1100 * guaranteed to be nul-terminated; @src must be nul-terminated; 1101 * @dest_size is the buffer size, not the number of bytes to copy. 1102 * 1103 * At most @dest_size - 1 characters will be copied. Always nul-terminates 1104 * (unless @dest_size is 0). This function does not allocate memory. Unlike 1105 * strncpy(), this function doesn't pad @dest (so it's often faster). It 1106 * returns the size of the attempted result, strlen (src), so if 1107 * @retval >= @dest_size, truncation occurred. 1108 * 1109 * Caveat: strlcpy() is supposedly more secure than strcpy() or strncpy(), 1110 * but if you really want to avoid screwups, g_strdup() is an even better 1111 * idea. 1112 * 1113 * Params: 1114 * dest = destination buffer 1115 * src = source buffer 1116 * destSize = length of @dest in bytes 1117 * 1118 * Returns: length of @src 1119 */ 1120 public static size_t strlcpy(string dest, string src, size_t destSize) 1121 { 1122 return g_strlcpy(Str.toStringz(dest), Str.toStringz(src), destSize); 1123 } 1124 1125 /** 1126 * A case-insensitive string comparison, corresponding to the standard 1127 * strncasecmp() function on platforms which support it. It is similar 1128 * to g_strcasecmp() except it only compares the first @n characters of 1129 * the strings. 1130 * 1131 * Deprecated: The problem with g_strncasecmp() is that it does 1132 * the comparison by calling toupper()/tolower(). These functions 1133 * are locale-specific and operate on single bytes. However, it is 1134 * impossible to handle things correctly from an internationalization 1135 * standpoint by operating on bytes, since characters may be multibyte. 1136 * Thus g_strncasecmp() is broken if your string is guaranteed to be 1137 * ASCII, since it is locale-sensitive, and it's broken if your string 1138 * is localized, since it doesn't work on many encodings at all, 1139 * including UTF-8, EUC-JP, etc. 1140 * 1141 * There are therefore two replacement techniques: g_ascii_strncasecmp(), 1142 * which only works on ASCII and is not locale-sensitive, and 1143 * g_utf8_casefold() followed by strcmp() on the resulting strings, 1144 * which is good for case-insensitive sorting of UTF-8. 1145 * 1146 * Params: 1147 * s1 = a string 1148 * s2 = a string to compare with @s1 1149 * n = the maximum number of characters to compare 1150 * 1151 * Returns: 0 if the strings match, a negative value if @s1 < @s2, 1152 * or a positive value if @s1 > @s2. 1153 */ 1154 public static int strncasecmp(string s1, string s2, uint n) 1155 { 1156 return g_strncasecmp(Str.toStringz(s1), Str.toStringz(s2), n); 1157 } 1158 1159 /** 1160 * Duplicates the first @n bytes of a string, returning a newly-allocated 1161 * buffer @n + 1 bytes long which will always be nul-terminated. If @str 1162 * is less than @n bytes long the buffer is padded with nuls. If @str is 1163 * %NULL it returns %NULL. The returned value should be freed when no longer 1164 * needed. 1165 * 1166 * To copy a number of characters from a UTF-8 encoded string, 1167 * use g_utf8_strncpy() instead. 1168 * 1169 * Params: 1170 * str = the string to duplicate 1171 * n = the maximum number of bytes to copy from @str 1172 * 1173 * Returns: a newly-allocated buffer containing the first @n bytes 1174 * of @str, nul-terminated 1175 */ 1176 public static string strndup(string str, size_t n) 1177 { 1178 auto retStr = g_strndup(Str.toStringz(str), n); 1179 1180 scope(exit) Str.freeString(retStr); 1181 return Str.toString(retStr); 1182 } 1183 1184 /** 1185 * Creates a new string @length bytes long filled with @fill_char. 1186 * The returned string should be freed when no longer needed. 1187 * 1188 * Params: 1189 * length = the length of the new string 1190 * fillChar = the byte to fill the string with 1191 * 1192 * Returns: a newly-allocated string filled the @fill_char 1193 */ 1194 public static string strnfill(size_t length, char fillChar) 1195 { 1196 auto retStr = g_strnfill(length, fillChar); 1197 1198 scope(exit) Str.freeString(retStr); 1199 return Str.toString(retStr); 1200 } 1201 1202 /** 1203 * Reverses all of the bytes in a string. For example, 1204 * `g_strreverse ("abcdef")` will result in "fedcba". 1205 * 1206 * Note that g_strreverse() doesn't work on UTF-8 strings 1207 * containing multibyte characters. For that purpose, use 1208 * g_utf8_strreverse(). 1209 * 1210 * Params: 1211 * str = the string to reverse 1212 * 1213 * Returns: the same pointer passed in as @string 1214 */ 1215 public static string strreverse(string str) 1216 { 1217 auto retStr = g_strreverse(Str.toStringz(str)); 1218 1219 scope(exit) Str.freeString(retStr); 1220 return Str.toString(retStr); 1221 } 1222 1223 /** 1224 * Searches the string @haystack for the last occurrence 1225 * of the string @needle. 1226 * 1227 * Params: 1228 * haystack = a nul-terminated string 1229 * needle = the nul-terminated string to search for 1230 * 1231 * Returns: a pointer to the found occurrence, or 1232 * %NULL if not found. 1233 */ 1234 public static string strrstr(string haystack, string needle) 1235 { 1236 auto retStr = g_strrstr(Str.toStringz(haystack), Str.toStringz(needle)); 1237 1238 scope(exit) Str.freeString(retStr); 1239 return Str.toString(retStr); 1240 } 1241 1242 /** 1243 * Searches the string @haystack for the last occurrence 1244 * of the string @needle, limiting the length of the search 1245 * to @haystack_len. 1246 * 1247 * Params: 1248 * haystack = a nul-terminated string 1249 * haystackLen = the maximum length of @haystack 1250 * needle = the nul-terminated string to search for 1251 * 1252 * Returns: a pointer to the found occurrence, or 1253 * %NULL if not found. 1254 */ 1255 public static string strrstrLen(string haystack, ptrdiff_t haystackLen, string needle) 1256 { 1257 auto retStr = g_strrstr_len(Str.toStringz(haystack), haystackLen, Str.toStringz(needle)); 1258 1259 scope(exit) Str.freeString(retStr); 1260 return Str.toString(retStr); 1261 } 1262 1263 /** 1264 * Returns a string describing the given signal, e.g. "Segmentation fault". 1265 * You should use this function in preference to strsignal(), because it 1266 * returns a string in UTF-8 encoding, and since not all platforms support 1267 * the strsignal() function. 1268 * 1269 * Params: 1270 * signum = the signal number. See the `signal` documentation 1271 * 1272 * Returns: a UTF-8 string describing the signal. If the signal is unknown, 1273 * it returns "unknown signal (<signum>)". 1274 */ 1275 public static string strsignal(int signum) 1276 { 1277 return Str.toString(g_strsignal(signum)); 1278 } 1279 1280 /** 1281 * Splits a string into a maximum of @max_tokens pieces, using the given 1282 * @delimiter. If @max_tokens is reached, the remainder of @string is 1283 * appended to the last token. 1284 * 1285 * As an example, the result of g_strsplit (":a:bc::d:", ":", -1) is a 1286 * %NULL-terminated vector containing the six strings "", "a", "bc", "", "d" 1287 * and "". 1288 * 1289 * As a special case, the result of splitting the empty string "" is an empty 1290 * vector, not a vector containing a single string. The reason for this 1291 * special case is that being able to represent a empty vector is typically 1292 * more useful than consistent handling of empty elements. If you do need 1293 * to represent empty elements, you'll need to check for the empty string 1294 * before calling g_strsplit(). 1295 * 1296 * Params: 1297 * str = a string to split 1298 * delimiter = a string which specifies the places at which to split 1299 * the string. The delimiter is not included in any of the resulting 1300 * strings, unless @max_tokens is reached. 1301 * maxTokens = the maximum number of pieces to split @string into. 1302 * If this is less than 1, the string is split completely. 1303 * 1304 * Returns: a newly-allocated %NULL-terminated array of strings. Use 1305 * g_strfreev() to free it. 1306 */ 1307 public static string[] strsplit(string str, string delimiter, int maxTokens) 1308 { 1309 return Str.toStringArray(g_strsplit(Str.toStringz(str), Str.toStringz(delimiter), maxTokens)); 1310 } 1311 1312 /** 1313 * Splits @string into a number of tokens not containing any of the characters 1314 * in @delimiter. A token is the (possibly empty) longest string that does not 1315 * contain any of the characters in @delimiters. If @max_tokens is reached, the 1316 * remainder is appended to the last token. 1317 * 1318 * For example the result of g_strsplit_set ("abc:def/ghi", ":/", -1) is a 1319 * %NULL-terminated vector containing the three strings "abc", "def", 1320 * and "ghi". 1321 * 1322 * The result of g_strsplit_set (":def/ghi:", ":/", -1) is a %NULL-terminated 1323 * vector containing the four strings "", "def", "ghi", and "". 1324 * 1325 * As a special case, the result of splitting the empty string "" is an empty 1326 * vector, not a vector containing a single string. The reason for this 1327 * special case is that being able to represent a empty vector is typically 1328 * more useful than consistent handling of empty elements. If you do need 1329 * to represent empty elements, you'll need to check for the empty string 1330 * before calling g_strsplit_set(). 1331 * 1332 * Note that this function works on bytes not characters, so it can't be used 1333 * to delimit UTF-8 strings for anything but ASCII characters. 1334 * 1335 * Params: 1336 * str = The string to be tokenized 1337 * delimiters = A nul-terminated string containing bytes that are used 1338 * to split the string. 1339 * maxTokens = The maximum number of tokens to split @string into. 1340 * If this is less than 1, the string is split completely 1341 * 1342 * Returns: a newly-allocated %NULL-terminated array of strings. Use 1343 * g_strfreev() to free it. 1344 * 1345 * Since: 2.4 1346 */ 1347 public static string[] strsplitSet(string str, string delimiters, int maxTokens) 1348 { 1349 return Str.toStringArray(g_strsplit_set(Str.toStringz(str), Str.toStringz(delimiters), maxTokens)); 1350 } 1351 1352 /** 1353 * Searches the string @haystack for the first occurrence 1354 * of the string @needle, limiting the length of the search 1355 * to @haystack_len. 1356 * 1357 * Params: 1358 * haystack = a string 1359 * haystackLen = the maximum length of @haystack. Note that -1 is 1360 * a valid length, if @haystack is nul-terminated, meaning it will 1361 * search through the whole string. 1362 * needle = the string to search for 1363 * 1364 * Returns: a pointer to the found occurrence, or 1365 * %NULL if not found. 1366 */ 1367 public static string strstrLen(string haystack, ptrdiff_t haystackLen, string needle) 1368 { 1369 auto retStr = g_strstr_len(Str.toStringz(haystack), haystackLen, Str.toStringz(needle)); 1370 1371 scope(exit) Str.freeString(retStr); 1372 return Str.toString(retStr); 1373 } 1374 1375 /** 1376 * Converts a string to a #gdouble value. 1377 * It calls the standard strtod() function to handle the conversion, but 1378 * if the string is not completely converted it attempts the conversion 1379 * again with g_ascii_strtod(), and returns the best match. 1380 * 1381 * This function should seldom be used. The normal situation when reading 1382 * numbers not for human consumption is to use g_ascii_strtod(). Only when 1383 * you know that you must expect both locale formatted and C formatted numbers 1384 * should you use this. Make sure that you don't pass strings such as comma 1385 * separated lists of values, since the commas may be interpreted as a decimal 1386 * point in some locales, causing unexpected results. 1387 * 1388 * Params: 1389 * nptr = the string to convert to a numeric value. 1390 * endptr = if non-%NULL, it returns the 1391 * character after the last character used in the conversion. 1392 * 1393 * Returns: the #gdouble value. 1394 */ 1395 public static double strtod(string nptr, out string endptr) 1396 { 1397 char* outendptr = null; 1398 1399 auto p = g_strtod(Str.toStringz(nptr), &outendptr); 1400 1401 endptr = Str.toString(outendptr); 1402 1403 return p; 1404 } 1405 1406 /** 1407 * Converts a string to upper case. 1408 * 1409 * Deprecated: This function is totally broken for the reasons 1410 * discussed in the g_strncasecmp() docs - use g_ascii_strup() 1411 * or g_utf8_strup() instead. 1412 * 1413 * Params: 1414 * str = the string to convert 1415 * 1416 * Returns: the string 1417 */ 1418 public static string strup(string str) 1419 { 1420 auto retStr = g_strup(Str.toStringz(str)); 1421 1422 scope(exit) Str.freeString(retStr); 1423 return Str.toString(retStr); 1424 } 1425 1426 /** */ 1427 public static GType strvGetType() 1428 { 1429 return g_strv_get_type(); 1430 } 1431 1432 /** 1433 * Returns the length of the given %NULL-terminated 1434 * string array @str_array. 1435 * 1436 * Params: 1437 * strArray = a %NULL-terminated array of strings 1438 * 1439 * Returns: length of @str_array. 1440 * 1441 * Since: 2.6 1442 */ 1443 public static uint strvLength(string[] strArray) 1444 { 1445 return g_strv_length(Str.toStringzArray(strArray)); 1446 } 1447 1448 /** 1449 * Checks if @strv contains @str. @strv must not be %NULL. 1450 * 1451 * Params: 1452 * strv = a %NULL-terminated array of strings 1453 * str = a string 1454 * 1455 * Returns: %TRUE if @str is an element of @strv, according to g_str_equal(). 1456 * 1457 * Since: 2.44 1458 */ 1459 public static bool strvContains(string strv, string str) 1460 { 1461 return g_strv_contains(Str.toStringz(strv), Str.toStringz(str)) != 0; 1462 } 1463 1464 /** 1465 * An implementation of the GNU vasprintf() function which supports 1466 * positional parameters, as specified in the Single Unix Specification. 1467 * This function is similar to g_vsprintf(), except that it allocates a 1468 * string to hold the output, instead of putting the output in a buffer 1469 * you allocate in advance. 1470 * 1471 * `glib/gprintf.h` must be explicitly included in order to use this function. 1472 * 1473 * Params: 1474 * str = the return location for the newly-allocated string. 1475 * format = a standard printf() format string, but notice 1476 * [string precision pitfalls][string-precision] 1477 * args = the list of arguments to insert in the output. 1478 * 1479 * Returns: the number of bytes printed. 1480 * 1481 * Since: 2.4 1482 */ 1483 public static int vasprintf(string[] str, string format, void* args) 1484 { 1485 return g_vasprintf(Str.toStringzArray(str), Str.toStringz(format), args); 1486 } 1487 1488 /** 1489 * An implementation of the standard vprintf() function which supports 1490 * positional parameters, as specified in the Single Unix Specification. 1491 * 1492 * `glib/gprintf.h` must be explicitly included in order to use this function. 1493 * 1494 * Params: 1495 * format = a standard printf() format string, but notice 1496 * [string precision pitfalls][string-precision] 1497 * args = the list of arguments to insert in the output. 1498 * 1499 * Returns: the number of bytes printed. 1500 * 1501 * Since: 2.2 1502 */ 1503 public static int vprintf(string format, void* args) 1504 { 1505 return g_vprintf(Str.toStringz(format), args); 1506 } 1507 1508 /** 1509 * A safer form of the standard vsprintf() function. The output is guaranteed 1510 * to not exceed @n characters (including the terminating nul character), so 1511 * it is easy to ensure that a buffer overflow cannot occur. 1512 * 1513 * See also g_strdup_vprintf(). 1514 * 1515 * In versions of GLib prior to 1.2.3, this function may return -1 if the 1516 * output was truncated, and the truncated string may not be nul-terminated. 1517 * In versions prior to 1.3.12, this function returns the length of the output 1518 * string. 1519 * 1520 * The return value of g_vsnprintf() conforms to the vsnprintf() function 1521 * as standardized in ISO C99. Note that this is different from traditional 1522 * vsnprintf(), which returns the length of the output string. 1523 * 1524 * The format string may contain positional parameters, as specified in 1525 * the Single Unix Specification. 1526 * 1527 * Params: 1528 * str = the buffer to hold the output. 1529 * n = the maximum number of bytes to produce (including the 1530 * terminating nul character). 1531 * format = a standard printf() format string, but notice 1532 * string precision pitfalls][string-precision] 1533 * args = the list of arguments to insert in the output. 1534 * 1535 * Returns: the number of bytes which would be produced if the buffer 1536 * was large enough. 1537 */ 1538 public static int vsnprintf(string str, gulong n, string format, void* args) 1539 { 1540 return g_vsnprintf(Str.toStringz(str), n, Str.toStringz(format), args); 1541 } 1542 1543 /** 1544 * An implementation of the standard vsprintf() function which supports 1545 * positional parameters, as specified in the Single Unix Specification. 1546 * 1547 * `glib/gprintf.h` must be explicitly included in order to use this function. 1548 * 1549 * Params: 1550 * str = the buffer to hold the output. 1551 * format = a standard printf() format string, but notice 1552 * [string precision pitfalls][string-precision] 1553 * args = the list of arguments to insert in the output. 1554 * 1555 * Returns: the number of bytes printed. 1556 * 1557 * Since: 2.2 1558 */ 1559 public static int vsprintf(string str, string format, void* args) 1560 { 1561 return g_vsprintf(Str.toStringz(str), Str.toStringz(format), args); 1562 } 1563 1564 /** 1565 * An implementation of the standard fprintf() function which supports 1566 * positional parameters, as specified in the Single Unix Specification. 1567 * 1568 * `glib/gprintf.h` must be explicitly included in order to use this function. 1569 * 1570 * Params: 1571 * file = the stream to write to. 1572 * format = a standard printf() format string, but notice 1573 * [string precision pitfalls][string-precision] 1574 * args = the list of arguments to insert in the output. 1575 * 1576 * Returns: the number of bytes printed. 1577 * 1578 * Since: 2.2 1579 */ 1580 public static int vfprintf(FILE* file, string format, void* args) 1581 { 1582 return g_vfprintf(file, Str.toStringz(format), args); 1583 } 1584 1585 /** 1586 * A convenience function for converting a string to a signed number. 1587 * 1588 * This function assumes that @str contains only a number of the given 1589 * @base that is within inclusive bounds limited by @min and @max. If 1590 * this is true, then the converted number is stored in @out_num. An 1591 * empty string is not a valid input. A string with leading or 1592 * trailing whitespace is also an invalid input. 1593 * 1594 * @base can be between 2 and 36 inclusive. Hexadecimal numbers must 1595 * not be prefixed with "0x" or "0X". Such a problem does not exist 1596 * for octal numbers, since they were usually prefixed with a zero 1597 * which does not change the value of the parsed number. 1598 * 1599 * Parsing failures result in an error with the %G_NUMBER_PARSER_ERROR 1600 * domain. If the input is invalid, the error code will be 1601 * %G_NUMBER_PARSER_ERROR_INVALID. If the parsed number is out of 1602 * bounds - %G_NUMBER_PARSER_ERROR_OUT_OF_BOUNDS. 1603 * 1604 * See g_ascii_strtoll() if you have more complex needs such as 1605 * parsing a string which starts with a number, but then has other 1606 * characters. 1607 * 1608 * Params: 1609 * str = a string 1610 * base = base of a parsed number 1611 * min = a lower bound (inclusive) 1612 * max = an upper bound (inclusive) 1613 * outNum = a return location for a number 1614 * 1615 * Returns: %TRUE if @str was a number, otherwise %FALSE. 1616 * 1617 * Since: 2.54 1618 * 1619 * Throws: GException on failure. 1620 */ 1621 public static bool asciiStringToSigned(string str, uint base, long min, long max, out long outNum) 1622 { 1623 GError* err = null; 1624 1625 auto p = g_ascii_string_to_signed(Str.toStringz(str), base, min, max, &outNum, &err) != 0; 1626 1627 if (err !is null) 1628 { 1629 throw new GException( new ErrorG(err) ); 1630 } 1631 1632 return p; 1633 } 1634 1635 /** 1636 * A convenience function for converting a string to an unsigned number. 1637 * 1638 * This function assumes that @str contains only a number of the given 1639 * @base that is within inclusive bounds limited by @min and @max. If 1640 * this is true, then the converted number is stored in @out_num. An 1641 * empty string is not a valid input. A string with leading or 1642 * trailing whitespace is also an invalid input. 1643 * 1644 * @base can be between 2 and 36 inclusive. Hexadecimal numbers must 1645 * not be prefixed with "0x" or "0X". Such a problem does not exist 1646 * for octal numbers, since they were usually prefixed with a zero 1647 * which does not change the value of the parsed number. 1648 * 1649 * Parsing failures result in an error with the %G_NUMBER_PARSER_ERROR 1650 * domain. If the input is invalid, the error code will be 1651 * %G_NUMBER_PARSER_ERROR_INVALID. If the parsed number is out of 1652 * bounds - %G_NUMBER_PARSER_ERROR_OUT_OF_BOUNDS. 1653 * 1654 * See g_ascii_strtoull() if you have more complex needs such as 1655 * parsing a string which starts with a number, but then has other 1656 * characters. 1657 * 1658 * Params: 1659 * str = a string 1660 * base = base of a parsed number 1661 * min = a lower bound (inclusive) 1662 * max = an upper bound (inclusive) 1663 * outNum = a return location for a number 1664 * 1665 * Returns: %TRUE if @str was a number, otherwise %FALSE. 1666 * 1667 * Since: 2.54 1668 * 1669 * Throws: GException on failure. 1670 */ 1671 public static bool asciiStringToUnsigned(string str, uint base, ulong min, ulong max, out ulong outNum) 1672 { 1673 GError* err = null; 1674 1675 auto p = g_ascii_string_to_unsigned(Str.toStringz(str), base, min, max, &outNum, &err) != 0; 1676 1677 if (err !is null) 1678 { 1679 throw new GException( new ErrorG(err) ); 1680 } 1681 1682 return p; 1683 } 1684 }