| 663 | } |
| 664 | |
| 665 | inline char *c_ptr() |
| 666 | { |
| 667 | if (unlikely(!Ptr)) |
| 668 | return (char*) ""; |
| 669 | /* |
| 670 | Here we assume that any buffer used to initialize String has |
| 671 | an end \0 or have at least an accessable character at end. |
| 672 | This is to handle the case of String("Hello",5) and |
| 673 | String("hello",5) efficiently. |
| 674 | |
| 675 | We have two options here. To test for !Alloced_length or !alloced. |
| 676 | Using "Alloced_length" is slightly safer so that we do not read |
| 677 | from potentially uninitialized memory (normally not dangerous but |
| 678 | may give warnings in valgrind), but "alloced" is safer as there |
| 679 | are less change to get memory loss from code that is using |
| 680 | String((char*), length) or String.set((char*), length) and does |
| 681 | not free things properly (and there is several places in the code |
| 682 | where this happens and it is hard to find out if any of these will call |
| 683 | c_ptr(). |
| 684 | */ |
| 685 | if (unlikely(!alloced && !Ptr[str_length])) |
| 686 | return Ptr; |
| 687 | if (str_length < Alloced_length) |
| 688 | { |
| 689 | Ptr[str_length]=0; |
| 690 | return Ptr; |
| 691 | } |
| 692 | (void) realloc(str_length); /* This will add end \0 */ |
| 693 | return Ptr; |
| 694 | } |
| 695 | /* |
| 696 | One should use c_ptr() instead for most cases. This will be deleted soon, |
| 697 | kept for compatibility. |
no outgoing calls