Skip to content

Commit

Permalink
Move the private category addition to a static method. (#395)
Browse files Browse the repository at this point in the history
Also fix up some doc comment strings.
  • Loading branch information
thomasvl committed Mar 17, 2023
1 parent 4624d22 commit 864f37a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 30 deletions.
25 changes: 11 additions & 14 deletions Foundation/GTMNSString+HTML.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,21 @@
@interface NSString (GTMNSStringHTMLAdditions)

/// Get a string where internal characters that need escaping for HTML are escaped
//
///
/// For example, '&' become '&'. This will only cover characters from table
/// A.2.2 of http://www.w3.org/TR/xhtml1/dtds.html#a_dtd_Special_characters
/// which is what you want for a unicode encoded webpage. If you have a ascii
/// or non-encoded webpage, please use stringByEscapingAsciiHTML which will
/// encode all characters.
///
/// For obvious reasons this call is only safe once.
//
// Returns:
// Autoreleased NSString
//
///
/// Returns:
/// Autoreleased NSString
- (NSString *)gtm_stringByEscapingForHTML;

/// Get a string where internal characters that need escaping for HTML are escaped
//
///
/// For example, '&' become '&'
/// All non-mapped characters (unicode that don't have a &keyword; mapping)
/// will be converted to the appropriate &#xxx; value. If your webpage is
Expand All @@ -47,20 +46,18 @@
/// are using a unicode compliant HTML reader).
///
/// For obvious reasons this call is only safe once.
//
// Returns:
// Autoreleased NSString
//
///
/// Returns:
/// Autoreleased NSString
- (NSString *)gtm_stringByEscapingForAsciiHTML;

/// Get a string where internal characters that are escaped for HTML are unescaped
//
///
/// For example, '&' becomes '&'
/// Handles   and 2 cases as well
///
// Returns:
// Autoreleased NSString
//
/// Returns:
/// Autoreleased NSString
- (NSString *)gtm_stringByUnescapingFromHTML;

@end
35 changes: 19 additions & 16 deletions Foundation/GTMNSString+HTML.m
Original file line number Diff line number Diff line change
Expand Up @@ -370,22 +370,21 @@ static int EscapeMapCompare(const void *ucharVoid, const void *mapVoid) {
return val;
}

@implementation NSString (GTMNSStringHTMLAdditions)

- (NSString *)gtm_stringByEscapingHTMLUsingTable:(HTMLEscapeMap*)table
ofSize:(NSUInteger)size
escapingUnicode:(BOOL)escapeUnicode {
NSUInteger length = [self length];
static NSString *StringByEscapingHTMLUsingTable(NSString *src,
HTMLEscapeMap* table,
NSUInteger tableSize,
BOOL escapeUnicode) {
NSUInteger length = [src length];
if (!length) {
return self;
return src;
}

NSMutableString *finalString = [NSMutableString string];
NSMutableData *data2 = [NSMutableData dataWithCapacity:sizeof(unichar) * length];

// this block is common between GTMNSString+HTML and GTMNSString+XML but
// it's so short that it isn't really worth trying to share.
const unichar *buffer = CFStringGetCharactersPtr((CFStringRef)self);
const unichar *buffer = CFStringGetCharactersPtr((CFStringRef)src);
if (!buffer) {
// We want this buffer to be autoreleased.
NSMutableData *data = [NSMutableData dataWithLength:length * sizeof(UniChar)];
Expand All @@ -395,7 +394,7 @@ - (NSString *)gtm_stringByEscapingHTMLUsingTable:(HTMLEscapeMap*)table
return nil;
// COV_NF_END
}
[self getCharacters:[data mutableBytes]];
[src getCharacters:[data mutableBytes]];
buffer = [data bytes];
}

Expand All @@ -412,7 +411,7 @@ - (NSString *)gtm_stringByEscapingHTMLUsingTable:(HTMLEscapeMap*)table

for (NSUInteger i = 0; i < length; ++i) {
HTMLEscapeMap *val = bsearch(&buffer[i], table,
size / sizeof(HTMLEscapeMap),
tableSize / sizeof(HTMLEscapeMap),
sizeof(HTMLEscapeMap), EscapeMapCompare);
if (val || (escapeUnicode && buffer[i] > 127)) {
if (buffer2Length) {
Expand Down Expand Up @@ -441,16 +440,20 @@ - (NSString *)gtm_stringByEscapingHTMLUsingTable:(HTMLEscapeMap*)table
return finalString;
}

@implementation NSString (GTMNSStringHTMLAdditions)

- (NSString *)gtm_stringByEscapingForHTML {
return [self gtm_stringByEscapingHTMLUsingTable:gUnicodeHTMLEscapeMap
ofSize:sizeof(gUnicodeHTMLEscapeMap)
escapingUnicode:NO];
return StringByEscapingHTMLUsingTable(self,
gUnicodeHTMLEscapeMap,
sizeof(gUnicodeHTMLEscapeMap),
/*escapingUnicode=*/NO);
} // gtm_stringByEscapingHTML

- (NSString *)gtm_stringByEscapingForAsciiHTML {
return [self gtm_stringByEscapingHTMLUsingTable:gAsciiHTMLEscapeMap
ofSize:sizeof(gAsciiHTMLEscapeMap)
escapingUnicode:YES];
return StringByEscapingHTMLUsingTable(self,
gAsciiHTMLEscapeMap,
sizeof(gAsciiHTMLEscapeMap),
/*escapingUnicode=*/YES);
} // gtm_stringByEscapingAsciiHTML

- (NSString *)gtm_stringByUnescapingFromHTML {
Expand Down

0 comments on commit 864f37a

Please sign in to comment.