From 747a26458ea1235ef601ed056e38063e228e33d0 Mon Sep 17 00:00:00 2001 From: cxl Date: Fri, 20 May 2016 11:25:28 +0000 Subject: [PATCH] Core: String: TrimLast, TrimStart, TrimEnd git-svn-id: svn://ultimatepp.org/upp/trunk@9847 f0d560ea-af0d-0410-9eb7-867de7ffcac7 --- uppsrc/Core/String.h | 12 +++++- uppsrc/Core/src.tpp/String$en-us.tpp | 59 ++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 1 deletion(-) diff --git a/uppsrc/Core/String.h b/uppsrc/Core/String.h index 6dcf8fd89..b309474ef 100644 --- a/uppsrc/Core/String.h +++ b/uppsrc/Core/String.h @@ -57,6 +57,8 @@ public: void Insert(int pos, const tchar *s, int count) { B::Insert(pos, count, s); } void Insert(int pos, const String& s) { Insert(pos, s, s.GetCount()); } void Insert(int pos, const char *s); + + void TrimLast(int count = 1) { B::Trim(B::GetCount() - count); } void Cat(int c) { B::Cat(c); } void Cat(const tchar *s, int len) { B::Cat(s, len); } @@ -107,9 +109,17 @@ public: bool StartsWith(const tchar *s) const; bool StartsWith(const String& s) const { return StartsWith(~s, s.GetLength()); } + bool TrimStart(const tchar *s, int len) { if(!StartsWith(s, len)) return false; Remove(0, len); return true; } + bool TrimStart(const tchar *s) { return TrimStart(s, strlen__(s)); } + bool TrimStart(const String& s) { return TrimStart(~s, s.GetLength()); } + bool EndsWith(const tchar *s, int len) const; bool EndsWith(const tchar *s) const; bool EndsWith(const String& s) const { return EndsWith(~s, s.GetLength()); } + + bool TrimEnd(const tchar *s, int len) { if(!EndsWith(s, len)) return false; TrimLast(len); return true; } + bool TrimEnd(const tchar *s) { return TrimEnd(s, strlen__(s)); } + bool TrimEnd(const String& s) { return TrimEnd(~s, s.GetLength()); } int FindFirstOf(int len, const tchar *s, int from) const; int FindFirstOf(const tchar *s, int from = 0) const; @@ -147,7 +157,7 @@ public: friend String operator+(const tchar *a, const String& b) { String c(a); c += b; return c; } friend String operator+(tchar a, const String& b) { String c(a, 1); c += b; return c; } -// Avoid common error of adding offset to String (NoCopy to produce compiler error) +// Avoid common error of adding offset to String (Fail__ to produce compiler error) friend Fail__ operator+(int, const String&) { return Fail__(); } friend Fail__ operator+(const String&, int) { return Fail__();} friend Fail__ operator+(size_t, const String&) { return Fail__(); } diff --git a/uppsrc/Core/src.tpp/String$en-us.tpp b/uppsrc/Core/src.tpp/String$en-us.tpp index 5ef8f90ef..8da76d9b4 100644 --- a/uppsrc/Core/src.tpp/String$en-us.tpp +++ b/uppsrc/Core/src.tpp/String$en-us.tpp @@ -520,6 +520,12 @@ rt]([@(0.0.255) int]_[*@3 pos], [@(0.0.255) const]_[_^AString`:`:tchar^ tchar]_` times).&] [s3; &] [s4;%- &] +[s5;:Upp`:`:AString`:`:TrimLast`(int`):%- [@(0.0.255) void]_[* TrimLast]([@(0.0.255) int]_[*@3 c +ount]_`=_[@3 1])&] +[s2; Removes [%-*@3 count] characters from the end of String. Same +as Remove(GetCount() `- count).&] +[s3; &] +[s4;%- &] [s5;:AString`:`:Mid`(int`,int`)const:%- [_^AString`:`:String^ String]_[* Mid]([@(0.0.255) i nt]_[*@3 pos], [@(0.0.255) int]_[*@3 length])_[@(0.0.255) const]&] [s2; Returns a substring that begins from [%-*@3 pos] and with [%-*@3 length @@ -564,6 +570,10 @@ substring found. In other words, if not found returns `-1, if found, returns Find([%-*@3 s][%- , ][%-*@3 from]) `+ strlen([%-*@3 s]).&] [s3; &] [s4;%- &] +[s5;:Upp`:`:AString`:`:Replace`(const Upp`:`:AString`:`:tchar`*`,int`,const Upp`:`:AString`:`:tchar`*`,int`):%- [@(0.0.255) v +oid]_[* Replace]([@(0.0.255) const]_[_^Upp`:`:AString`:`:tchar^ tchar]_`*[*@3 find], +[@(0.0.255) int]_[*@3 findlen], [@(0.0.255) const]_[_^Upp`:`:AString`:`:tchar^ tchar]_`*[*@3 r +eplace], [@(0.0.255) int]_[*@3 replacelen])&] [s5;:AString`:`:Replace`(const AString`:`:String`&`,const AString`:`:String`&`):%- [@(0.0.255) v oid]_[* Replace]([@(0.0.255) const]_[_^AString`:`:String^ String][@(0.0.255) `&]_[*@3 find], [@(0.0.255) const]_[_^AString`:`:String^ String][@(0.0.255) `&]_[*@3 replace])&] @@ -578,4 +588,53 @@ oid]_[* Replace]([@(0.0.255) const]_[_^AString`:`:tchar^ tchar]_`*[*@3 find], [@(0.0.255) const]_[_^AString`:`:String^ String][@(0.0.255) `&]_[*@3 replace])&] [s2; Replaces substring [%-*@3 find] with [%-*@3 replace ]string for all times [%-*@3 find] string appears.&] +[s3; &] +[s4;%- &] +[s5;:Upp`:`:AString`:`:StartsWith`(const Upp`:`:AString`:`:tchar`*`,int`)const:%- [@(0.0.255) b +ool]_[* StartsWith]([@(0.0.255) const]_[_^Upp`:`:AString`:`:tchar^ tchar]_`*[*@3 s], +[@(0.0.255) int]_[*@3 len])_[@(0.0.255) const]&] +[s5;:Upp`:`:AString`:`:StartsWith`(const Upp`:`:AString`:`:tchar`*`)const:%- [@(0.0.255) b +ool]_[* StartsWith]([@(0.0.255) const]_[_^Upp`:`:AString`:`:tchar^ tchar]_`*[*@3 s])_[@(0.0.255) c +onst]&] +[s5;:Upp`:`:AString`:`:StartsWith`(const Upp`:`:AString`:`:String`&`)const:%- [@(0.0.255) b +ool]_[* StartsWith]([@(0.0.255) const]_[_^Upp`:`:AString`:`:String^ String][@(0.0.255) `& +]_[*@3 s])_[@(0.0.255) const]&] +[s2; Returns true if string starts with [%-*@3 s].&] +[s3; &] +[s4;%- &] +[s5;:Upp`:`:AString`:`:TrimStart`(const Upp`:`:AString`:`:tchar`*`,int`):%- [@(0.0.255) b +ool]_[* TrimStart]([@(0.0.255) const]_[_^Upp`:`:AString`:`:tchar^ tchar]_`*[*@3 s], +[@(0.0.255) int]_[*@3 len])&] +[s5;:Upp`:`:AString`:`:TrimStart`(const Upp`:`:AString`:`:tchar`*`):%- [@(0.0.255) bool +]_[* TrimStart]([@(0.0.255) const]_[_^Upp`:`:AString`:`:tchar^ tchar]_`*[*@3 s])&] +[s5;:Upp`:`:AString`:`:TrimStart`(const Upp`:`:AString`:`:String`&`):%- [@(0.0.255) boo +l]_[* TrimStart]([@(0.0.255) const]_[_^Upp`:`:AString`:`:String^ String][@(0.0.255) `&]_[*@3 s +])&] +[s2; If string ends with [%-*@3 s]., trims the end to remove it and +returns true.&] +[s3; &] +[s4;%- &] +[s5;:Upp`:`:AString`:`:EndsWith`(const Upp`:`:AString`:`:tchar`*`,int`)const:%- [@(0.0.255) b +ool]_[* EndsWith]([@(0.0.255) const]_[_^Upp`:`:AString`:`:tchar^ tchar]_`*[*@3 s], +[@(0.0.255) int]_[*@3 len])_[@(0.0.255) const]&] +[s5;:Upp`:`:AString`:`:EndsWith`(const Upp`:`:AString`:`:tchar`*`)const:%- [@(0.0.255) b +ool]_[* EndsWith]([@(0.0.255) const]_[_^Upp`:`:AString`:`:tchar^ tchar]_`*[*@3 s])_[@(0.0.255) c +onst]&] +[s5;:Upp`:`:AString`:`:EndsWith`(const Upp`:`:AString`:`:String`&`)const:%- [@(0.0.255) b +ool]_[* EndsWith]([@(0.0.255) const]_[_^Upp`:`:AString`:`:String^ String][@(0.0.255) `&]_ +[*@3 s])_[@(0.0.255) const]&] +[s2; Returns true if string ends with [%-*@3 s].&] +[s3; &] +[s4;%- &] +[s5;:Upp`:`:AString`:`:TrimEnd`(const Upp`:`:AString`:`:tchar`*`,int`):%- [@(0.0.255) b +ool]_[* TrimEnd]([@(0.0.255) const]_[_^Upp`:`:AString`:`:tchar^ tchar]_`*[*@3 s], +[@(0.0.255) int]_[*@3 len])&] +[s5;:Upp`:`:AString`:`:TrimEnd`(const Upp`:`:AString`:`:tchar`*`):%- [@(0.0.255) bool]_ +[* TrimEnd]([@(0.0.255) const]_[_^Upp`:`:AString`:`:tchar^ tchar]_`*[*@3 s])&] +[s5;:Upp`:`:AString`:`:TrimEnd`(const Upp`:`:AString`:`:String`&`):%- [@(0.0.255) bool]_ +[* TrimEnd]([@(0.0.255) const]_[_^Upp`:`:AString`:`:String^ String][@(0.0.255) `&]_[*@3 s]) +&] +[s2; If string ends with [%-*@3 s]., trims the end to remove it and +returns true.&] +[s3; &] [s3; ]] \ No newline at end of file