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
3 // found in the LICENSE file.
5 #include "ios/chrome/common/string_util.h"
7 #import <UIKit/UIKit.h>
9 #include "base/logging.h"
10 #include "base/mac/scoped_block.h"
11 #include "base/mac/scoped_nsobject.h"
12 #include "base/strings/stringprintf.h"
13 #include "base/strings/sys_string_conversions.h"
16 typedef BOOL (^ArrayFilterProcedure)(id object, NSUInteger index, BOOL* stop);
17 typedef NSString* (^SubstringExtractionProcedure)(NSUInteger);
20 NSString* ParseStringWithLink(NSString* text, NSRange* out_link_range) {
21 // Find the range within |text| and create a substring without the link tags.
22 NSRange begin_range = [text rangeOfString:@"BEGIN_LINK[ \t]*"
23 options:NSRegularExpressionSearch];
24 NSRange link_text_range = NSMakeRange(NSNotFound, 0);
25 if (begin_range.length == 0) {
27 *out_link_range = link_text_range;
31 NSUInteger after_begin_link = NSMaxRange(begin_range);
32 NSRange range_to_search_for_end_link =
33 NSMakeRange(after_begin_link, text.length - after_begin_link);
34 NSRange end_range = [text rangeOfString:@"[ \t]*END_LINK"
35 options:NSRegularExpressionSearch
36 range:range_to_search_for_end_link];
37 if (end_range.length == 0) {
39 *out_link_range = link_text_range;
43 link_text_range.location = after_begin_link;
44 link_text_range.length = end_range.location - link_text_range.location;
45 base::scoped_nsobject<NSMutableString> out_text(
46 [[NSMutableString alloc] init]);
47 // First part - before the link.
48 if (begin_range.location > 0)
49 [out_text appendString:[text substringToIndex:begin_range.location]];
52 [out_text appendString:[text substringWithRange:link_text_range]];
54 // Last part - after the link.
55 NSUInteger after_end_link = NSMaxRange(end_range);
56 if (after_end_link < [text length]) {
57 [out_text appendString:[text substringFromIndex:after_end_link]];
60 link_text_range.location = begin_range.location;
62 *out_link_range = link_text_range;
63 return [NSString stringWithString:out_text];
66 // Ranges of unicode codepage containing drawing characters.
68 // 2200—22FF Mathematical Operators
69 // 2300—23FF Miscellaneous Technical
70 // 2400—243F Control Pictures
71 // 2440—245F Optical Character Recognition
72 // 2460—24FF Enclosed Alphanumerics
73 // 2500—257F Box Drawing
74 // 2580—259F Block Elements
75 // 25A0—25FF Geometric Shapes
76 // 2600—26FF Miscellaneous Symbols
78 // 27C0—27EF Miscellaneous Mathematical Symbols-A
79 // 27F0—27FF Supplemental Arrows-A
80 // 2900—297F Supplemental Arrows-B
81 // 2980—29FF Miscellaneous Mathematical Symbols-B
82 // 2A00—2AFF Supplemental Mathematical Operators
83 // 2B00—2BFF Miscellaneous Symbols and Arrows
84 // The section 2800—28FF Braille Patterns must be preserved.
85 // The list of characters that must be deleted from the selection.
86 NSCharacterSet* GraphicCharactersSet() {
87 static NSMutableCharacterSet* graphicalCharsSet;
88 static dispatch_once_t dispatch_once_token;
89 dispatch_once(&dispatch_once_token, ^{
90 graphicalCharsSet = [[NSMutableCharacterSet alloc] init];
91 NSRange graphicalCharsFirstRange = NSMakeRange(0x2190, 0x2800 - 0x2190);
92 NSRange graphicalCharsSecondRange = NSMakeRange(0x2900, 0x2c00 - 0x2900);
93 [graphicalCharsSet addCharactersInRange:graphicalCharsFirstRange];
94 [graphicalCharsSet addCharactersInRange:graphicalCharsSecondRange];
96 return graphicalCharsSet;
99 // TODO(marq): Add unit tests for this function.
100 NSString* CleanNSStringForDisplay(NSString* dirty,
101 BOOL removeGraphicChars,
103 NSCharacterSet* wspace = [NSCharacterSet whitespaceAndNewlineCharacterSet];
104 NSString* cleanString = dirty;
105 if (removeGraphicChars) {
106 cleanString = [[cleanString
107 componentsSeparatedByCharactersInSet:GraphicCharactersSet()]
108 componentsJoinedByString:@" "];
110 base::scoped_nsobject<NSMutableArray> spaceSeparatedCompoments(
111 [[cleanString componentsSeparatedByCharactersInSet:wspace] mutableCopy]);
112 ArrayFilterProcedure filter = ^(id object, NSUInteger index, BOOL* stop) {
113 return [object isEqualToString:@""];
115 [spaceSeparatedCompoments
116 removeObjectsAtIndexes:[spaceSeparatedCompoments
117 indexesOfObjectsPassingTest:filter]];
118 cleanString = [spaceSeparatedCompoments componentsJoinedByString:@" "];
119 return trim ? [cleanString stringByTrimmingCharactersInSet:wspace]
123 std::string CleanStringForDisplay(std::string dirty,
124 BOOL removeGraphicChars,
126 return base::SysNSStringToUTF8(CleanNSStringForDisplay(
127 base::SysUTF8ToNSString(dirty), removeGraphicChars, trim));
130 // TODO(marq): Add unit tests for this function.
131 NSString* SubstringOfWidth(NSString* string,
132 NSDictionary* attributes,
135 if (![string length])
138 UIFont* font = [attributes objectForKey:NSFontAttributeName];
141 // Function to get the correct substring while insulating against
142 // length overrun/underrun.
143 base::mac::ScopedBlock<SubstringExtractionProcedure> getSubstring;
145 getSubstring.reset([^NSString*(NSUInteger chars) {
146 NSUInteger length = [string length];
147 return [string substringFromIndex:length - MIN(length, chars)];
150 getSubstring.reset([^NSString*(NSUInteger chars) {
151 return [string substringToIndex:MIN(chars, [string length])];
155 // Guess at the number of characters that will fit, assuming
156 // the font's x-height is about 25% wider than an average character (25%
157 // value was determined experimentally).
158 NSUInteger characters =
159 MIN(targetWidth / (font.xHeight * 0.8), [string length]);
160 NSInteger increment = 1;
161 NSString* substring = getSubstring.get()(characters);
162 CGFloat prevWidth = [substring sizeWithAttributes:attributes].width;
164 characters += increment;
165 substring = getSubstring.get()(characters);
166 CGFloat thisWidth = [substring sizeWithAttributes:attributes].width;
167 if (prevWidth > targetWidth) {
168 if (thisWidth < targetWidth)
169 break; // Shrinking the string, found the right size.
171 increment = -1; // Shrink the string
172 } else if (prevWidth < targetWidth) {
173 if (thisWidth < targetWidth)
174 increment = 1; // Grow the string
176 substring = getSubstring.get()(characters - increment);
177 break; // Growing the string, found the right size.
180 prevWidth = thisWidth;
181 } while (characters > 0 && characters < [string length]);