Encode NSString for XML/HTML

后端 未结 14 1408
误落风尘
误落风尘 2020-11-28 04:25

Is there a way to HTML encode a string (NSString) in Objective-C, something along the lines of Server.HtmlEncode in .NET?

14条回答
  •  北荒
    北荒 (楼主)
    2020-11-28 04:49

    Here is a more efficient implementation of this xml escape logic.

    + (NSString*) xmlSimpleEscape:(NSString*)unescapedStr
    {
      if (unescapedStr == nil || [unescapedStr length] == 0) {
        return unescapedStr;
      }
    
      const int len = [unescapedStr length];
      int longer = ((int) (len * 0.10));
      if (longer < 5) {
        longer = 5;
      }
      longer = len + longer;
      NSMutableString *mStr = [NSMutableString stringWithCapacity:longer];
    
      NSRange subrange;
      subrange.location = 0;
      subrange.length = 0;
    
      for (int i = 0; i < len; i++) {
        char c = [unescapedStr characterAtIndex:i];
        NSString *replaceWithStr = nil;
    
        if (c == '\"')
        {
          replaceWithStr = @""";
        }
        else if (c == '\'')
        {
          replaceWithStr = @"'";
        }
        else if (c == '<')
        {
          replaceWithStr = @"<";
        }
        else if (c == '>')
        {
          replaceWithStr = @">";
        }
        else if (c == '&')
        {
          replaceWithStr = @"&";
        }
    
        if (replaceWithStr == nil) {
          // The current character is not an XML escape character, increase subrange length
    
          subrange.length += 1;
        } else {
          // The current character will be replaced, but append any pending substring first
    
          if (subrange.length > 0) {
            NSString *substring = [unescapedStr substringWithRange:subrange];
            [mStr appendString:substring];
          }
    
          [mStr appendString:replaceWithStr];
    
          subrange.location = i + 1;
          subrange.length = 0;
        }
      }
    
      // Got to end of unescapedStr so append any pending substring, in the
      // case of no escape characters this will append the whole string.
    
      if (subrange.length > 0) {
        if (subrange.location == 0) {
          [mStr appendString:unescapedStr];      
        } else {
          NSString *substring = [unescapedStr substringWithRange:subrange];
          [mStr appendString:substring];
        }
      }
    
      return [NSString stringWithString:mStr];
    }
    
    + (NSString*) formatSimpleNode:(NSString*)tagname value:(NSString*)value
    {
      NSAssert(tagname != nil, @"tagname is nil");
      NSAssert([tagname length] > 0, @"tagname is the empty string");
    
      if (value == nil || [value length] == 0) {
        // Certain XML parsers don't like empty nodes like "", use "" instead
        return [NSString stringWithFormat:@"<%@ />", tagname];
      } else {
        NSString *escapedValue = [self xmlSimpleEscape:value];
        return [NSString stringWithFormat:@"<%@>%@", tagname, escapedValue, tagname];    
      }
    }
    

提交回复
热议问题