From e6310bbc5a3bce1033c0e579341e115be9df6fe9 Mon Sep 17 00:00:00 2001
From: =?utf8?q?V=C3=A1clav=20Slav=C3=ADk?= <vslavik@fastmail.fm>
Date: Thu, 28 Jun 2007 12:36:54 +0000
Subject: [PATCH] added wxString::FromAscii(char*,size_t) for consistency with
 FromUTF8()

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@46995 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
---
 docs/latex/wx/wxstring.tex | 10 ++++++----
 include/wx/string.h        |  3 +++
 src/common/string.cpp      | 33 +++++++++++++++++----------------
 3 files changed, 26 insertions(+), 20 deletions(-)

diff --git a/docs/latex/wx/wxstring.tex b/docs/latex/wx/wxstring.tex
index 6b175c1455..36d7b3f57d 100644
--- a/docs/latex/wx/wxstring.tex
+++ b/docs/latex/wx/wxstring.tex
@@ -770,6 +770,8 @@ This is a convenience method useful when storing binary data in wxString.
 
 \func{static wxString }{FromAscii}{\param{const char*}{ s}}
 
+\func{static wxString }{FromAscii}{\param{const char*}{ s}, \param{size\_t}{ len}}
+
 \func{static wxString }{FromAscii}{\param{const char}{ c}}
 
 Converts the string or character from an ASCII, 7-bit form
@@ -1371,10 +1373,10 @@ The macro wxWX2WCbuf is defined as the correct return type (without const).
 \constfunc{wxWritableWCharBuffer}{wchar\_str}{\void}
 
 Returns an object with string data that is implicitly convertible to
-{\tt char*} pointer. Note that any change to the returned buffer is lost and so
-this function is only usable for passing strings to legacy libraries that
-don't have const-correct API. Use \helpref{wxStringBuffer}{wxstringbuffer} if
-you want to modify the string.
+{\tt char*} pointer. Note that changes to the returned buffer may or may
+not be lost (depending on the build) and so this function is only usable for
+passing strings to legacy libraries that don't have const-correct API. Use
+\helpref{wxStringBuffer}{wxstringbuffer} if you want to modify the string.
 
 \wxheading{See also}
 
diff --git a/include/wx/string.h b/include/wx/string.h
index 7cccebcad3..fa5552b9f2 100644
--- a/include/wx/string.h
+++ b/include/wx/string.h
@@ -1215,11 +1215,14 @@ public:
     // the behaviour of these functions with the strings containing anything
     // else than 7 bit ASCII characters is undefined, use at your own risk.
 #if wxUSE_UNICODE
+    static wxString FromAscii(const char *ascii, size_t len);  // string
     static wxString FromAscii(const char *ascii);  // string
     static wxString FromAscii(const char ascii);   // char
     const wxCharBuffer ToAscii() const;
 #else // ANSI
     static wxString FromAscii(const char *ascii) { return wxString( ascii ); }
+    static wxString FromAscii(const char *ascii, size_t len)
+        { return wxString( ascii, len ); }
     static wxString FromAscii(const char ascii) { return wxString( ascii ); }
     const char *ToAscii() const { return c_str(); }
 #endif // Unicode/!Unicode
diff --git a/src/common/string.cpp b/src/common/string.cpp
index b9456ecee4..7b9f77b28b 100644
--- a/src/common/string.cpp
+++ b/src/common/string.cpp
@@ -973,35 +973,36 @@ int wxString::CmpNoCase(const wxString& s) const
 #endif
 #endif
 
-wxString wxString::FromAscii(const char *ascii)
+wxString wxString::FromAscii(const char *ascii, size_t len)
 {
-    if (!ascii)
+    if (!ascii || len == 0)
        return wxEmptyString;
 
-    size_t len = strlen(ascii);
     wxString res;
 
-    if ( len )
-    {
-        wxImplStringBuffer buf(res, len);
-        wxStringCharType *dest = buf;
+    wxImplStringBuffer buf(res, len);
+    wxStringCharType *dest = buf;
 
-        for ( ;; )
-        {
-            unsigned char c = (unsigned char)*ascii++;
-            wxASSERT_MSG( c < 0x80,
-                          _T("Non-ASCII value passed to FromAscii().") );
+    for ( ;; )
+    {
+        unsigned char c = (unsigned char)*ascii++;
+        wxASSERT_MSG( c < 0x80,
+                      _T("Non-ASCII value passed to FromAscii().") );
 
-            *dest++ = (wchar_t)c;
+        *dest++ = (wchar_t)c;
 
-            if ( c == '\0' )
-                break;
-        }
+        if ( c == '\0' )
+            break;
     }
 
     return res;
 }
 
+wxString wxString::FromAscii(const char *ascii)
+{
+    return FromAscii(ascii, strlen(ascii));
+}
+
 wxString wxString::FromAscii(const char ascii)
 {
     // What do we do with '\0' ?
-- 
2.50.0

