Skip to content

Commit

Permalink
Fix for bugzilla 2718
Browse files Browse the repository at this point in the history
  • Loading branch information
andralex committed Sep 26, 2010
1 parent 5248dc6 commit 460c844
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 24 deletions.
14 changes: 7 additions & 7 deletions std/stream.d
Expand Up @@ -298,8 +298,8 @@ interface OutputStream {
* and should only be used in conjunction with read.
* Throw WriteException on error.
*/
void write(char[] s);
void write(const(wchar)[] s); /// ditto
void write(const(char)[] s);
void write(const(wchar)[] s); /// ditto

/***
* Write a line of text,
Expand Down Expand Up @@ -337,8 +337,8 @@ interface OutputStream {
* Print a formatted string into the stream using printf-style syntax,
* returning the number of bytes written.
*/
size_t vprintf(char[] format, va_list args);
size_t printf(char[] format, ...); /// ditto
size_t vprintf(const(char)[] format, va_list args);
size_t printf(const(char)[] format, ...); /// ditto

/***
* Print a formatted string into the stream using writef-style syntax.
Expand Down Expand Up @@ -1077,7 +1077,7 @@ class Stream : InputStream, OutputStream {
void write(dchar x) { writeExact(&x, x.sizeof); }

// writes a string, together with its length
void write(char[] s) {
void write(const(char)[] s) {
write(s.length);
writeString(s);
}
Expand Down Expand Up @@ -1122,7 +1122,7 @@ class Stream : InputStream, OutputStream {

// writes data to stream using vprintf() syntax,
// returns number of bytes written
size_t vprintf(char[] format, va_list args) {
size_t vprintf(const(char)[] format, va_list args) {
// shamelessly stolen from OutBuffer,
// by Walter's permission
char[1024] buffer;
Expand Down Expand Up @@ -1155,7 +1155,7 @@ class Stream : InputStream, OutputStream {

// writes data to stream using printf() syntax,
// returns number of bytes written
size_t printf(char[] format, ...) {
size_t printf(const(char)[] format, ...) {
va_list ap;
ap = cast(va_list) &format;
ap += format.sizeof;
Expand Down
41 changes: 24 additions & 17 deletions std/string.d
Expand Up @@ -215,7 +215,7 @@ unittest
* $(D s[]) must not contain embedded 0's.
*/

const(char)* toStringz(const(char)[] s)
immutable(char)* toStringz(const(char)[] s)
in
{
// The assert below contradicts the unittests!
Expand Down Expand Up @@ -255,22 +255,29 @@ body
copy = new char[s.length + 1];
copy[0..s.length] = s;
copy[s.length] = 0;
return copy.ptr;
}

// /// Ditto
// const(char)* toStringz(immutable(char)[] s)
// {
// /* Peek past end of s[], if it's 0, no conversion necessary.
// * Note that the compiler will put a 0 past the end of static
// * strings, and the storage allocator will put a 0 past the end
// * of newly allocated char[]'s.
// */
// immutable p = &s[0] + s.length;
// if (*p == 0)
// return s.ptr;
// return toStringz(cast(const char[]) s);
// }

return assumeUnique(copy).ptr;
}

/// Ditto
immutable(char)* toStringz(string s)
{
if (!s) return null;
/* Peek past end of s[], if it's 0, no conversion necessary.
* Note that the compiler will put a 0 past the end of static
* strings, and the storage allocator will put a 0 past the end
* of newly allocated char[]'s.
*/
immutable p = s.ptr + s.length;
// Is p dereferenceable? A simple test: if the p points to an
// address multiple of 4, then conservatively assume the pointer
// might be pointing to a new block of memory, which might be
// unreadable. Otherwise, it's definitely pointing to valid
// memory.
if ((cast(size_t) p & 3) && *p == 0)
return s.ptr;
return toStringz(cast(const char[]) s);
}

unittest
{
Expand Down
8 changes: 8 additions & 0 deletions std/utf.d
Expand Up @@ -919,6 +919,14 @@ string toUTF8(string s)

/** ditto */

string toUTF8(in char[] s)
{
validate(s);
return s.idup;
}

/** ditto */

string toUTF8(const(wchar)[] s)
{
char[] r;
Expand Down

0 comments on commit 460c844

Please sign in to comment.