Russian not fixed completely #1

Open
DirectX opened this Issue Feb 10, 2011 · 1 comment

Comments

Projects
None yet
2 participants
@DirectX

DirectX commented Feb 10, 2011

NSString+Hyphenate.m
...
111: char const* tokenChars = [[token lowercaseString] UTF8String];
112: wordLength = strlen(tokenChars)]; // <-- Doubles length on multybyte words!
...

NSMakeRange(loc, wordLength - loc) below crashes due to
'*** -[NSCFString substringWithRange:]: Range or index out of bounds'.

Correcly: wordLength = [token length];

@xfyre

This comment has been minimized.

Show comment Hide comment
@xfyre

xfyre Aug 13, 2011

Here's the patch to make hyphenation work correctly for Russian and get rid of NSRangeException:

--- NSString+Hyphenate.m        2011-01-19 17:17:51.000000000 +0300
+++ /Users/xfire/Documents/works/dvp/iphone/rbc/Classes/NSString+Hyphenate/NSString+Hyphenate.m 2011-08-13 19:12:55.000000000 +0400
@@ -11,7 +11,7 @@

 @implementation NSString (Hyphenate)

-- (NSString*)stringByHyphenatingWithLocale:(NSLocale*)locale {
+- (NSString *) stringByHyphenatingWithLocale:(NSLocale*)locale {
     static HyphenDict* dict = NULL;
     static NSString* localeIdentifier = nil;
     static NSBundle* bundle = nil;
@@ -123,7 +123,7 @@

             NSUInteger loc = 0;
             NSUInteger len = 0;
-            for (i = 0; i < wordLength; i++) {
+            for (i = 0; i < tokenRange.length; i++) {
                 if (hyphens[i] & 1) {
                     len = i - loc + 1;
                     [result appendString:
@@ -134,7 +134,7 @@
             }
             if (loc < wordLength) {
                 [result appendString:
-                 [token substringWithRange:NSMakeRange(loc, wordLength - loc)]];
+                 [token substringWithRange:NSMakeRange(loc, tokenRange.length - loc)]];
             }

             // Clean up

Enjoy!

xfyre commented Aug 13, 2011

Here's the patch to make hyphenation work correctly for Russian and get rid of NSRangeException:

--- NSString+Hyphenate.m        2011-01-19 17:17:51.000000000 +0300
+++ /Users/xfire/Documents/works/dvp/iphone/rbc/Classes/NSString+Hyphenate/NSString+Hyphenate.m 2011-08-13 19:12:55.000000000 +0400
@@ -11,7 +11,7 @@

 @implementation NSString (Hyphenate)

-- (NSString*)stringByHyphenatingWithLocale:(NSLocale*)locale {
+- (NSString *) stringByHyphenatingWithLocale:(NSLocale*)locale {
     static HyphenDict* dict = NULL;
     static NSString* localeIdentifier = nil;
     static NSBundle* bundle = nil;
@@ -123,7 +123,7 @@

             NSUInteger loc = 0;
             NSUInteger len = 0;
-            for (i = 0; i < wordLength; i++) {
+            for (i = 0; i < tokenRange.length; i++) {
                 if (hyphens[i] & 1) {
                     len = i - loc + 1;
                     [result appendString:
@@ -134,7 +134,7 @@
             }
             if (loc < wordLength) {
                 [result appendString:
-                 [token substringWithRange:NSMakeRange(loc, wordLength - loc)]];
+                 [token substringWithRange:NSMakeRange(loc, tokenRange.length - loc)]];
             }

             // Clean up

Enjoy!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment