OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ios/chrome/common/string_util.h" | 5 #include "ios/chrome/common/string_util.h" |
6 | 6 |
7 #import <UIKit/UIKit.h> | 7 #import <UIKit/UIKit.h> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/mac/scoped_block.h" | 10 #include "base/mac/scoped_block.h" |
11 #include "base/mac/scoped_nsobject.h" | |
12 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
13 #include "base/strings/sys_string_conversions.h" | 12 #include "base/strings/sys_string_conversions.h" |
14 | 13 |
| 14 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 15 #error "This file requires ARC support." |
| 16 #endif |
| 17 |
15 namespace { | 18 namespace { |
16 typedef BOOL (^ArrayFilterProcedure)(id object, NSUInteger index, BOOL* stop); | 19 typedef BOOL (^ArrayFilterProcedure)(id object, NSUInteger index, BOOL* stop); |
17 typedef NSString* (^SubstringExtractionProcedure)(NSUInteger); | 20 typedef NSString* (^SubstringExtractionProcedure)(NSUInteger); |
18 } | 21 } |
19 | 22 |
20 NSString* ParseStringWithLink(NSString* text, NSRange* out_link_range) { | 23 NSString* ParseStringWithLink(NSString* text, NSRange* out_link_range) { |
21 return ParseStringWithTag(text, out_link_range, @"BEGIN_LINK[ \t]*", | 24 return ParseStringWithTag(text, out_link_range, @"BEGIN_LINK[ \t]*", |
22 @"[ \t]*END_LINK"); | 25 @"[ \t]*END_LINK"); |
23 } | 26 } |
24 | 27 |
(...skipping 18 matching lines...) Expand all Loading... |
43 options:NSRegularExpressionSearch | 46 options:NSRegularExpressionSearch |
44 range:range_to_search_for_end_tag]; | 47 range:range_to_search_for_end_tag]; |
45 if (end_range.length == 0) { | 48 if (end_range.length == 0) { |
46 if (out_tag_range) | 49 if (out_tag_range) |
47 *out_tag_range = tag_text_range; | 50 *out_tag_range = tag_text_range; |
48 return text; | 51 return text; |
49 } | 52 } |
50 | 53 |
51 tag_text_range.location = after_begin_tag; | 54 tag_text_range.location = after_begin_tag; |
52 tag_text_range.length = end_range.location - tag_text_range.location; | 55 tag_text_range.length = end_range.location - tag_text_range.location; |
53 base::scoped_nsobject<NSMutableString> out_text( | 56 NSMutableString* out_text = [[NSMutableString alloc] init]; |
54 [[NSMutableString alloc] init]); | |
55 // First part - before the tag. | 57 // First part - before the tag. |
56 if (begin_range.location > 0) | 58 if (begin_range.location > 0) |
57 [out_text appendString:[text substringToIndex:begin_range.location]]; | 59 [out_text appendString:[text substringToIndex:begin_range.location]]; |
58 | 60 |
59 // Tag part. | 61 // Tag part. |
60 [out_text appendString:[text substringWithRange:tag_text_range]]; | 62 [out_text appendString:[text substringWithRange:tag_text_range]]; |
61 | 63 |
62 // Last part - after the tag. | 64 // Last part - after the tag. |
63 NSUInteger after_end_tag = NSMaxRange(end_range); | 65 NSUInteger after_end_tag = NSMaxRange(end_range); |
64 if (after_end_tag < [text length]) { | 66 if (after_end_tag < [text length]) { |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 } | 107 } |
106 | 108 |
107 NSString* CleanNSStringForDisplay(NSString* dirty, BOOL removeGraphicChars) { | 109 NSString* CleanNSStringForDisplay(NSString* dirty, BOOL removeGraphicChars) { |
108 NSCharacterSet* wspace = [NSCharacterSet whitespaceAndNewlineCharacterSet]; | 110 NSCharacterSet* wspace = [NSCharacterSet whitespaceAndNewlineCharacterSet]; |
109 NSString* cleanString = dirty; | 111 NSString* cleanString = dirty; |
110 if (removeGraphicChars) { | 112 if (removeGraphicChars) { |
111 cleanString = [[cleanString | 113 cleanString = [[cleanString |
112 componentsSeparatedByCharactersInSet:GraphicCharactersSet()] | 114 componentsSeparatedByCharactersInSet:GraphicCharactersSet()] |
113 componentsJoinedByString:@" "]; | 115 componentsJoinedByString:@" "]; |
114 } | 116 } |
115 base::scoped_nsobject<NSMutableArray> spaceSeparatedCompoments( | 117 NSMutableArray* spaceSeparatedCompoments = |
116 [[cleanString componentsSeparatedByCharactersInSet:wspace] mutableCopy]); | 118 [[cleanString componentsSeparatedByCharactersInSet:wspace] mutableCopy]; |
117 ArrayFilterProcedure filter = ^(id object, NSUInteger index, BOOL* stop) { | 119 ArrayFilterProcedure filter = ^(id object, NSUInteger index, BOOL* stop) { |
118 return [object isEqualToString:@""]; | 120 return [object isEqualToString:@""]; |
119 }; | 121 }; |
120 [spaceSeparatedCompoments | 122 [spaceSeparatedCompoments |
121 removeObjectsAtIndexes:[spaceSeparatedCompoments | 123 removeObjectsAtIndexes:[spaceSeparatedCompoments |
122 indexesOfObjectsPassingTest:filter]]; | 124 indexesOfObjectsPassingTest:filter]]; |
123 cleanString = [spaceSeparatedCompoments componentsJoinedByString:@" "]; | 125 cleanString = [spaceSeparatedCompoments componentsJoinedByString:@" "]; |
124 return cleanString; | 126 return cleanString; |
125 } | 127 } |
126 | 128 |
127 std::string CleanStringForDisplay(const std::string& dirty, | 129 std::string CleanStringForDisplay(const std::string& dirty, |
128 BOOL removeGraphicChars) { | 130 BOOL removeGraphicChars) { |
129 return base::SysNSStringToUTF8(CleanNSStringForDisplay( | 131 return base::SysNSStringToUTF8(CleanNSStringForDisplay( |
130 base::SysUTF8ToNSString(dirty), removeGraphicChars)); | 132 base::SysUTF8ToNSString(dirty), removeGraphicChars)); |
131 } | 133 } |
132 | 134 |
133 NSString* SubstringOfWidth(NSString* string, | 135 NSString* SubstringOfWidth(NSString* string, |
134 NSDictionary* attributes, | 136 NSDictionary* attributes, |
135 CGFloat targetWidth, | 137 CGFloat targetWidth, |
136 BOOL trailing) { | 138 BOOL trailing) { |
137 if (![string length]) | 139 if (![string length]) |
138 return nil; | 140 return nil; |
139 | 141 |
140 UIFont* font = [attributes objectForKey:NSFontAttributeName]; | 142 UIFont* font = [attributes objectForKey:NSFontAttributeName]; |
141 DCHECK(font); | 143 DCHECK(font); |
142 | 144 |
143 // Function to get the correct substring while insulating against | 145 // Function to get the correct substring while insulating against |
144 // length overrun/underrun. | 146 // length overrun/underrun. |
145 base::mac::ScopedBlock<SubstringExtractionProcedure> getSubstring; | 147 SubstringExtractionProcedure getSubstring; |
146 if (trailing) { | 148 if (trailing) { |
147 getSubstring.reset([^NSString*(NSUInteger chars) { | 149 getSubstring = [^NSString*(NSUInteger chars) { |
148 NSUInteger length = [string length]; | 150 NSUInteger length = [string length]; |
149 return [string substringFromIndex:length - MIN(length, chars)]; | 151 return [string substringFromIndex:length - MIN(length, chars)]; |
150 } copy]); | 152 } copy]; |
151 } else { | 153 } else { |
152 getSubstring.reset([^NSString*(NSUInteger chars) { | 154 getSubstring = [^NSString*(NSUInteger chars) { |
153 return [string substringToIndex:MIN(chars, [string length])]; | 155 return [string substringToIndex:MIN(chars, [string length])]; |
154 } copy]); | 156 } copy]; |
155 } | 157 } |
156 | 158 |
157 // Guess at the number of characters that will fit, assuming | 159 // Guess at the number of characters that will fit, assuming |
158 // the font's x-height is about 25% wider than an average character (25% | 160 // the font's x-height is about 25% wider than an average character (25% |
159 // value was determined experimentally). | 161 // value was determined experimentally). |
160 NSUInteger characters = | 162 NSUInteger characters = |
161 MIN(targetWidth / (font.xHeight * 0.8), [string length]); | 163 MIN(targetWidth / (font.xHeight * 0.8), [string length]); |
162 NSInteger increment = 1; | 164 NSInteger increment = 1; |
163 NSString* substring = getSubstring.get()(characters); | 165 NSString* substring = getSubstring(characters); |
164 CGFloat prevWidth = [substring sizeWithAttributes:attributes].width; | 166 CGFloat prevWidth = [substring sizeWithAttributes:attributes].width; |
165 do { | 167 do { |
166 characters += increment; | 168 characters += increment; |
167 substring = getSubstring.get()(characters); | 169 substring = getSubstring(characters); |
168 CGFloat thisWidth = [substring sizeWithAttributes:attributes].width; | 170 CGFloat thisWidth = [substring sizeWithAttributes:attributes].width; |
169 if (prevWidth > targetWidth) { | 171 if (prevWidth > targetWidth) { |
170 if (thisWidth <= targetWidth) | 172 if (thisWidth <= targetWidth) |
171 break; // Shrinking the string, found the right size. | 173 break; // Shrinking the string, found the right size. |
172 else | 174 else |
173 increment = -1; // Shrink the string | 175 increment = -1; // Shrink the string |
174 } else if (prevWidth < targetWidth) { | 176 } else if (prevWidth < targetWidth) { |
175 if (thisWidth < targetWidth) | 177 if (thisWidth < targetWidth) |
176 increment = 1; // Grow the string | 178 increment = 1; // Grow the string |
177 else { | 179 else { |
178 substring = getSubstring.get()(characters - increment); | 180 substring = getSubstring(characters - increment); |
179 break; // Growing the string, found the right size. | 181 break; // Growing the string, found the right size. |
180 } | 182 } |
181 } | 183 } |
182 prevWidth = thisWidth; | 184 prevWidth = thisWidth; |
183 } while (characters > 0 && characters < [string length]); | 185 } while (characters > 0 && characters < [string length]); |
184 | 186 |
185 return substring; | 187 return substring; |
186 } | 188 } |
OLD | NEW |