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