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