From f9f7bd82071acba247b696a8fc40f6d1b29c44f2 Mon Sep 17 00:00:00 2001 From: houzh Date: Wed, 1 Nov 2023 10:34:36 +0800 Subject: [PATCH] fix TextUtils::replace,Assets::getString --- scripts/idgen.py | 2 +- src/gui/core/assets.cc | 11 +++++------ src/gui/core/assets.h | 2 +- src/gui/core/context.h | 2 +- src/gui/core/textutils.cc | 14 +++++++------- src/gui/core/textutils.h | 2 +- src/gui/widget/textview.cc | 11 +++-------- 7 files changed, 19 insertions(+), 25 deletions(-) diff --git a/scripts/idgen.py b/scripts/idgen.py index 36d4e68f..3ac40a4d 100755 --- a/scripts/idgen.py +++ b/scripts/idgen.py @@ -42,7 +42,7 @@ class CDROIDHandler( xml.sax.ContentHandler ): if self.isMyNS(value): self.addID(value) if 'string/' in value: - value = self.normalizeXMLString(value) + #value = self.normalizeXMLString(value),this is done in cdroid Assets::getString self.addString(value) def addID(self,name): diff --git a/src/gui/core/assets.cc b/src/gui/core/assets.cc index 85bfe9d8..ce91db23 100755 --- a/src/gui/core/assets.cc +++ b/src/gui/core/assets.cc @@ -301,21 +301,20 @@ int Assets::getNextAutofillId(){ return mNextAutofillViewId++; } -const std::string& Assets::getString(const std::string& resid,const std::string&lan) { +const std::string Assets::getString(const std::string& resid,const std::string&lan) { if((!lan.empty())&&(mLanguage!=lan)) { loadStrings(lan); } + std::string str = resid; std::string pkg,name=resid; parseResource(resid,&name,&pkg); name = AttributeSet::normalize(pkg,resid); auto itr = mStrings.find(name); if(itr != mStrings.end()) { - std::string str=itr->second; - TextUtils::replace("\\n","\n"); - TextUtils::replace("\\r","\r"); - return str; + str = itr->second; } - return resid; + TextUtils::replace(str,"\\n","\n"); + return str; } int Assets::getArray(const std::string&resid,std::vector&out) { diff --git a/src/gui/core/assets.h b/src/gui/core/assets.h index e34b92c5..3d1db68f 100755 --- a/src/gui/core/assets.h +++ b/src/gui/core/assets.h @@ -45,7 +45,7 @@ public: const DisplayMetrics&getDisplayMetrics()const override; int getId(const std::string&)const override; int getNextAutofillId()override; - const std::string& getString(const std::string&id,const std::string&lan="")override; + const std::string getString(const std::string&id,const std::string&lan="")override; Cairo::RefPtr getImage(const std::string&resname)override; std::vector getStringArray(const std::string&resname,const std::string&arrayname)const; std::unique_ptr getInputStream(const std::string&resname,std::string*outpkg=nullptr)override; diff --git a/src/gui/core/context.h b/src/gui/core/context.h index ebb28759..e145b10e 100755 --- a/src/gui/core/context.h +++ b/src/gui/core/context.h @@ -24,7 +24,7 @@ public: virtual const DisplayMetrics&getDisplayMetrics()const=0; virtual int getId(const std::string&)const=0; virtual int getNextAutofillId()=0; - virtual const std::string& getString(const std::string&id,const std::string&lan="")=0; + virtual const std::string getString(const std::string&id,const std::string&lan="")=0; static Cairo::RefPtr loadImage( std::istream&istream ){ return Cairo::ImageSurface::create_from_stream(istream); } diff --git a/src/gui/core/textutils.cc b/src/gui/core/textutils.cc index 65dc4b94..216caee6 100644 --- a/src/gui/core/textutils.cc +++ b/src/gui/core/textutils.cc @@ -137,14 +137,14 @@ std::string& TextUtils::trim(std::string&s){ return s; } -int TextUtils::replace(std::string&str,const std::string&sfind,const std::string&sreplace){ - size_t pos; - int count=0; - while((pos=str.find_first_of(sfind))!=std::string::npos){ - str.replace(pos,pos+sfind.length(),sreplace); - count++; +std::string& TextUtils::replace(std::string&src,const std::string&old_value,const std::string&new_value){ + for (std::string::size_type pos(0); pos != std::string::npos; pos += new_value.length()) { + if ((pos = src.find(old_value, pos)) != std::string::npos) { + src.replace(pos, old_value.length(), new_value); + } + else break; } - return count; + return src; } long TextUtils::strtol(const std::string&value){ diff --git a/src/gui/core/textutils.h b/src/gui/core/textutils.h index 1b3a6d70..87300fbc 100644 --- a/src/gui/core/textutils.h +++ b/src/gui/core/textutils.h @@ -15,7 +15,7 @@ public: static bool startWith(const std::string&str,const std::string&head); static bool endWith(const std::string&str,const std::string&tail); static std::string& trim(std::string&); - static int replace(std::string&str,const std::string&sfind,const std::string&sreplace); + static std::string& replace(std::string&str,const std::string&sfind,const std::string&sreplace); static long strtol(const std::string&value); static std::vector split(const std::string& s,const std::string& delim); static int UCS2UTF(wchar_t wc,char*oututf,int outlen); diff --git a/src/gui/widget/textview.cc b/src/gui/widget/textview.cc index fbf5e609..7a9b6c34 100755 --- a/src/gui/widget/textview.cc +++ b/src/gui/widget/textview.cc @@ -366,14 +366,9 @@ void TextAppearanceAttributes::readTextAppearance(Context*ctx,const AttributeSet TextView::TextView(Context*ctx,const AttributeSet& attrs) :View(ctx,attrs){ initView(); - std::string text = ctx->getString(attrs.getString("text")); - TextUtils::replace(text,"\\n","\n"); - TextUtils::replace(text,"\\r","\r"); - setText(text); - text = ctx->getString(attrs.getString("hint")); - TextUtils::replace(text,"\\n","\n"); - TextUtils::replace(text,"\\r","\r"); - setHint(text); + + setText(ctx->getString(attrs.getString("text"))); + setHint(ctx->getString(attrs.getString("hint"))); Drawable* left = attrs.getDrawable("drawableLeft"); Drawable*right = attrs.getDrawable("drawableRight");