Re: Small patch for handling non-breaking spaces.

Robert Menteer <[email protected]> Sun, 13 Mar 2005 23:45:03 -0500
Newsgroups gmane.network.fire.devel
Message-ID <[email protected]>
On Mar 9, 2005, at 11:04 PM,  
fire-development-request-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org wrote:
> From: Jason Townsend <[email protected]>
> Subject: Re: Small patch for handling non-breaking spaces.
> Date: Tue, 8 Mar 2005 20:47:57 -0800
> To: fire-development-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org
> Reply-To: fire-development-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org
>
>
> --Apple-Mail-5-1039975715
> Content-Transfer-Encoding: 7bit
> Content-Type: text/plain;
> 	charset=US-ASCII;
> 	format=flowed
>
> On Mar 8, 2005, at 8:10 PM, Mark Rowe wrote:
>> On Mar 9, 2005, at 4:03 PM, Robert Menteer wrote:
>>> Here is a small patch for handling non-breaking spaces.
>>>
>> [snip]
>>
>> The patch seems logical, but I don't understand the use of \xCA's
>> throughout it.  My understanding is that Unicode's NO-BREAK SPACE is
>> at codepoint U+00A0, which is \xc2\xa0 using UTF-8.  What am I
>> missing?
>
> I think this would be better to do with something like this:
>
> [NSString stringWithFormat:@"%C",0x00A0] (assuming that Mark is right
> about the Unicode code point). The problem is I'm not sure there's any
> way to guarantee what encoding is used when you put a hex escape into
> an NSString constant. The %C always means provide a unichar, which is
> unambiguous.
>
> I'm guessing 0xCA is the MacRoman encoding of this character.
>
> -Jason

Let's try this one more time this time using Unicode characters.

Index: NSStringAdditions.m
===================================================================
RCS file: /cvsroot/fire/fire/NSStringAdditions.m,v
retrieving revision 1.40
diff -u -r1.40 NSStringAdditions.m
--- NSStringAdditions.m	31 Dec 2004 20:55:09 -0000	1.40
+++ NSStringAdditions.m	14 Mar 2005 04:33:55 -0000
@@ -101,6 +101,16 @@
          if (rng.length > 0)
              [out replaceCharactersInRange: rng withString: @"\""];
      } while (rng.length > 0);
+    do {
+				//non breaking space is a normal character whose glyph looks like  
a space
+        rng = [out rangeOfString: @"&nbsp;"];
+        if (rng.length > 0){
+			NSString *nbsp = [[[NSString alloc] initWithBytes:"\xA0" length:1
+						encoding: 
CFStringConvertEncodingToNSStringEncoding(NSUTF8StringEncoding)]
+						autorelease];
+            [out replaceCharactersInRange: rng withString: nbsp];
+		}
+    } while (rng.length > 0);
  //This is a bit more tricky
      rng.location = -1;
      do {
Index: NSAttributedString+AIMLanguage.m
===================================================================
RCS file: /cvsroot/fire/fire/NSAttributedString+AIMLanguage.m,v
retrieving revision 1.55
diff -u -r1.55 NSAttributedString+AIMLanguage.m
--- NSAttributedString+AIMLanguage.m	11 Mar 2005 03:24:50 -0000	1.55
+++ NSAttributedString+AIMLanguage.m	14 Mar 2005 04:34:33 -0000
@@ -1,4 +1,4 @@
-/*
+		/*
   * NSAttributedString+AIMLanguage.m
   * Fire
   *
@@ -70,9 +70,14 @@
              [out replaceCharactersInRange: rng withString: @"\""];
      } while (rng.length > 0);
      do {
-        rng = [out rangeOfString: @"&nbsp;"];  //non breaking space  
(don't know how to do the non-breaking part, but got the space easy
-        if (rng.length > 0)
-            [out replaceCharactersInRange: rng withString: @" "];
+				//non breaking space is a normal character whose glyph looks like  
a space
+        rng = [out rangeOfString: @"&nbsp;"];
+        if (rng.length > 0){
+						NSString *nbsp = [[[NSString alloc] initWithBytes:"\xA0" length:1
+												encoding: 
CFStringConvertEncodingToNSStringEncoding(NSUTF8StringEncoding)]
+												autorelease];
+            [out replaceCharactersInRange: rng withString: nbsp];
+				}
      } while (rng.length > 0);
  //This is a bit more tricky
      rng.location = -1;
@@ -811,6 +816,9 @@
              case '&':
                  [escapedstr appendString:@"&amp;"];
                  break;
+						case '\x00a0':
+								[escapedstr appendString:@"&nbsp;"];
+								break;
              default:
                  [escapedstr appendString:
                      [NSString stringWithCharacters:&aChar length:1]];
Index: Rendezvous/NSAttributedString+RendezvousHTML.m
===================================================================
RCS file:  
/cvsroot/fire/fire/Rendezvous/NSAttributedString+RendezvousHTML.m,v
retrieving revision 1.13
diff -u -r1.13 NSAttributedString+RendezvousHTML.m
--- Rendezvous/NSAttributedString+RendezvousHTML.m	12 Jan 2005 23:55:47  
-0000	1.13
+++ Rendezvous/NSAttributedString+RendezvousHTML.m	14 Mar 2005 04:35:26  
-0000
@@ -74,9 +74,14 @@
              [out replaceCharactersInRange: rng withString: @"'"];
      } while (rng.length > 0);
      do {
-        rng = [out rangeOfString: @"&nbsp;"];  //non breaking space  
(don't know how to do the non-breaking part, but got the space easy
-        if (rng.length > 0)
-            [out replaceCharactersInRange: rng withString: @" "];
+		//non breaking space is a normal character whose glyph looks like a  
space
+        rng = [out rangeOfString: @"&nbsp;"];
+        if (rng.length > 0){
+			NSString *nbsp = [[[NSString alloc] initWithBytes:"\xA0" length:1
+						encoding: 
CFStringConvertEncodingToNSStringEncoding(NSUTF8StringEncoding)]
+						autorelease];
+            [out replaceCharactersInRange: rng withString: nbsp];
+		}
      } while (rng.length > 0);
  //This is a bit more tricky
      rng.location = -1;
@@ -453,6 +458,9 @@
              case '"':
                  [escapedstr appendString:@"&amp;quot;"];
                  break;
+            case '\x00a0':
+                [escapedstr appendString:@"&amp;nbsp;"];
+                break;
              default:
                  [escapedstr appendString:
                      [NSString stringWithCharacters:&aChar length:1]];
@@ -641,6 +649,9 @@
                  case '"':
                      [escapedstr appendString:@"&quot;"];
                      break;
+                case '\x00a0':
+                    [escapedstr appendString:@"&nbsp;"];
+                    break;
                  default:
                      [escapedstr appendString:
                          [NSString stringWithCharacters:&aChar  
length:1]];

With knowledge it's possible.
With the right tools it's trivial.