From 27e875bf09d1a2334a6ae763bc6bbcbcd8faad8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A7=E7=9F=B3=E5=A4=B4?= Date: Tue, 22 Aug 2017 00:43:53 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9B=E5=AD=97=E7=AC=A6=E4=B8=B2?= =?UTF-8?q?=E9=95=BF=E5=BA=A6=E8=AE=A1=E7=AE=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Core/String.cpp | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/Core/String.cpp b/Core/String.cpp index 8a316090..bc55acc9 100644 --- a/Core/String.cpp +++ b/Core/String.cpp @@ -140,10 +140,15 @@ String::String(char* str, int length) : Array(str, length) _Capacity = length - 1; // 计算外部字符串长度 - int len = strlen(str); - if (len >= length) len = length - 1; + //int len = strlen(str); + //if (len >= length) len = length - 1; + + auto p = str; + int len = 0; + while (*p++ != '\0' && len < _Capacity) len++; + _Length = len; - _Arr[_Length] = '\0'; + _Arr[len] = '\0'; } // 外部传入缓冲区供内部使用,内部计算字符串长度,注意长度减去零结束符