mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 03:32:45 +00:00 
			
		
		
		
	AK: Add FlyString::equals_ignoring_case(StringView)
And share the code with String by moving the logic to StringUtils. :^)
This commit is contained in:
		
							parent
							
								
									0efa47b7ef
								
							
						
					
					
						commit
						26bc3d4ea0
					
				
					 6 changed files with 31 additions and 16 deletions
				
			
		|  | @ -88,4 +88,9 @@ int FlyString::to_int(bool& ok) const | ||||||
|     return StringUtils::convert_to_int(view(), ok); |     return StringUtils::convert_to_int(view(), ok); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | bool FlyString::equals_ignoring_case(const StringView& other) const | ||||||
|  | { | ||||||
|  |     return StringUtils::equals_ignoring_case(view(), other); | ||||||
|  | } | ||||||
|  | 
 | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -48,6 +48,8 @@ public: | ||||||
| 
 | 
 | ||||||
|     int to_int(bool& ok) const; |     int to_int(bool& ok) const; | ||||||
| 
 | 
 | ||||||
|  |     bool equals_ignoring_case(const StringView&) const; | ||||||
|  | 
 | ||||||
|     static void did_destroy_impl(Badge<StringImpl>, StringImpl&); |     static void did_destroy_impl(Badge<StringImpl>, StringImpl&); | ||||||
| 
 | 
 | ||||||
| private: | private: | ||||||
|  |  | ||||||
|  | @ -38,13 +38,6 @@ | ||||||
| extern "C" char* strstr(const char* haystack, const char* needle); | extern "C" char* strstr(const char* haystack, const char* needle); | ||||||
| #endif | #endif | ||||||
| 
 | 
 | ||||||
| static inline char to_lowercase(char c) |  | ||||||
| { |  | ||||||
|     if (c >= 'A' && c <= 'Z') |  | ||||||
|         return c | 0x20; |  | ||||||
|     return c; |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| namespace AK { | namespace AK { | ||||||
| 
 | 
 | ||||||
| bool String::operator==(const String& other) const | bool String::operator==(const String& other) const | ||||||
|  | @ -309,15 +302,7 @@ bool String::contains(const String& needle) const | ||||||
| 
 | 
 | ||||||
| bool String::equals_ignoring_case(const StringView& other) const | bool String::equals_ignoring_case(const StringView& other) const | ||||||
| { | { | ||||||
|     if (other.m_impl == impl()) |     return StringUtils::equals_ignoring_case(view(), other); | ||||||
|         return true; |  | ||||||
|     if (length() != other.length()) |  | ||||||
|         return false; |  | ||||||
|     for (size_t i = 0; i < length(); ++i) { |  | ||||||
|         if (::to_lowercase(characters()[i]) != ::to_lowercase(other.characters_without_null_termination()[i])) |  | ||||||
|             return false; |  | ||||||
|     } |  | ||||||
|     return true; |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| String escape_html_entities(const StringView& html) | String escape_html_entities(const StringView& html) | ||||||
|  |  | ||||||
|  | @ -143,6 +143,26 @@ unsigned convert_to_uint(const StringView& str, bool& ok) | ||||||
|     return value; |     return value; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | static inline char to_lowercase(char c) | ||||||
|  | { | ||||||
|  |     if (c >= 'A' && c <= 'Z') | ||||||
|  |         return c | 0x20; | ||||||
|  |     return c; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | bool equals_ignoring_case(const StringView& a, const StringView& b) | ||||||
|  | { | ||||||
|  |     if (a.impl() && a.impl() == b.impl()) | ||||||
|  |         return true; | ||||||
|  |     if (a.length() != b.length()) | ||||||
|  |         return false; | ||||||
|  |     for (size_t i = 0; i < a.length(); ++i) { | ||||||
|  |         if (to_lowercase(a.characters_without_null_termination()[i]) != to_lowercase(b.characters_without_null_termination()[i])) | ||||||
|  |             return false; | ||||||
|  |     } | ||||||
|  |     return true; | ||||||
|  | } | ||||||
|  | 
 | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -41,6 +41,7 @@ namespace StringUtils { | ||||||
| bool matches(const StringView& str, const StringView& mask, CaseSensitivity = CaseSensitivity::CaseInsensitive); | bool matches(const StringView& str, const StringView& mask, CaseSensitivity = CaseSensitivity::CaseInsensitive); | ||||||
| int convert_to_int(const StringView&, bool& ok); | int convert_to_int(const StringView&, bool& ok); | ||||||
| unsigned convert_to_uint(const StringView&, bool& ok); | unsigned convert_to_uint(const StringView&, bool& ok); | ||||||
|  | bool equals_ignoring_case(const StringView&, const StringView&); | ||||||
| 
 | 
 | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -139,6 +139,8 @@ public: | ||||||
|         return !(*this == other); |         return !(*this == other); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     const StringImpl* impl() const { return m_impl; } | ||||||
|  | 
 | ||||||
| private: | private: | ||||||
|     friend class String; |     friend class String; | ||||||
|     const StringImpl* m_impl { nullptr }; |     const StringImpl* m_impl { nullptr }; | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Andreas Kling
						Andreas Kling