public
Description: The Nu programming language.
Homepage: http://programming.nu
Clone URL: git://github.com/timburks/nu.git
Better printing of strings in lists.

Strings are now displayed in a quoted and escaped form when
they are displayed as a part of a list. So now we have:

% (list "a" "b" "c")
("a" "b" "c")

where previously:

% (list "a" "b" "c")
(a b c)

I also fixed the order of arguments in some test assertion
macro invocations. The desired result should always be first.
timburks (author)
Fri May 02 14:22:25 -0700 2008
commit  134ad119134c33139262b55b437140a70f50a144
tree    417404cbab7517b6facb7fa227a958fe3fedce07
parent  c38c2414b8bf50eff8d8f61898fc190a63ffede4
...
175
176
177
178
 
 
 
 
 
 
179
180
181
...
175
176
177
 
178
179
180
181
182
183
184
185
186
0
@@ -175,7 +175,12 @@ limitations under the License.
0
             [result appendString:[mycar stringValue]];
0
         }
0
         else if (mycar && (mycar != Nu__null)) {
0
- [result appendString:[mycar description]];
0
+ if ([mycar respondsToSelector:@selector(escapedStringRepresentation)]) {
0
+ [result appendString:[mycar escapedStringRepresentation]];
0
+ }
0
+ else {
0
+ [result appendString:[mycar description]];
0
+ }
0
         }
0
         else {
0
             [result appendString:@"()"];
...
138
139
140
 
 
 
141
142
143
...
138
139
140
141
142
143
144
145
146
0
@@ -138,6 +138,9 @@ limitations under the License.
0
 /*! Convert a string into a symbol. */
0
 - (id) symbolValue;
0
 
0
+/*! Get a representation of the string that can be used in Nu source code. */
0
+- (NSString *) escapedStringRepresentation;
0
+
0
 /*! Split a string into lines. */
0
 - (NSArray *) lines;
0
 
...
243
244
245
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
246
247
248
...
356
357
358
359
 
360
361
362
...
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
...
390
391
392
 
393
394
395
396
0
@@ -243,6 +243,40 @@ extern id Nu__null;
0
     return self;
0
 }
0
 
0
+- (NSString *) escapedStringRepresentation
0
+{
0
+ NSMutableString *result = [NSMutableString stringWithString:@"\""];
0
+ int length = [self length];
0
+ for (int i = 0; i < length; i++) {
0
+ unichar c = [self characterAtIndex:i];
0
+ if (c < 32) {
0
+ switch (c)
0
+ {
0
+ case 0x07: [result appendString:@"\\a"]; break;
0
+ case 0x08: [result appendString:@"\\b"]; break;
0
+ case 0x09: [result appendString:@"\\t"]; break;
0
+ case 0x0a: [result appendString:@"\\n"]; break;
0
+ case 0x0c: [result appendString:@"\\f"]; break;
0
+ case 0x0d: [result appendString:@"\\r"]; break;
0
+ case 0x1b: [result appendString:@"\\e"]; break;
0
+ default:
0
+ [result appendFormat:@"\\x%02x", c];
0
+ }
0
+ }
0
+ else if (c < 127) {
0
+ [result appendCharacter:c];
0
+ }
0
+ else if (c < 256) {
0
+ [result appendFormat:@"\\x%02x", c];
0
+ }
0
+ else {
0
+ [result appendFormat:@"\\u%04x", c];
0
+ }
0
+ }
0
+ [result appendString:@"\""];
0
+ return result;
0
+}
0
+
0
 - (id) evalWithContext:(NSMutableDictionary *) context
0
 {
0
     NSMutableString *result;
0
@@ -356,7 +390,7 @@ extern id Nu__null;
0
 - (NSString *) replaceString:(NSString *) target withString:(NSString *) replacement
0
 {
0
     NSMutableString *s = [NSMutableString stringWithString:self];
0
- [s replaceOccurrencesOfString:target withString:replacement options:nil range:NSMakeRange(0, [self length])];
0
+ [s replaceOccurrencesOfString:target withString:replacement options:0 range:NSMakeRange(0, [self length])];
0
     return s;
0
 }
0
 
...
906
907
908
909
 
910
911
912
913
 
 
 
914
915
916
917
 
918
919
920
921
922
923
 
 
 
 
 
924
925
 
926
927
 
928
929
930
931
932
 
933
934
 
935
936
937
...
906
907
908
 
909
910
 
 
 
911
912
913
914
915
916
 
917
918
 
 
 
 
 
919
920
921
922
923
924
 
925
926
 
927
928
929
930
931
 
932
933
 
934
935
936
937
0
@@ -906,32 +906,32 @@ static int nu_parse_escape_sequences(NSString *string, int i, int imax, NSMutabl
0
                     if ([cursor car] != [NSNull null]) {
0
                         id expression = [cursor car];
0
                         //printf("evaluating %s\n", [[expression stringValue] cStringUsingEncoding:NSUTF8StringEncoding]);
0
-#ifdef DARWIN
0
+ #ifdef DARWIN
0
                         @try
0
-#else
0
- NS_DURING
0
-#endif
0
+ #else
0
+ NS_DURING
0
+ #endif
0
                         {
0
                             id result = [expression evalWithContext:context];
0
                             if (result)
0
- printf("%s\n", [[result stringValue] cStringUsingEncoding:NSUTF8StringEncoding]);
0
+ printf("%s\n", [[result stringValue] cStringUsingEncoding:NSUTF8StringEncoding]);
0
                         }
0
-#ifdef DARWIN
0
-@catch (id exception)
0
-#else
0
- NS_HANDLER
0
-#endif
0
+ #ifdef DARWIN
0
+ @catch (id exception)
0
+ #else
0
+ NS_HANDLER
0
+ #endif
0
                         {
0
-#ifndef DARWIN
0
+ #ifndef DARWIN
0
                             id exception = localException;
0
-#endif
0
+ #endif
0
                             printf("%s: %s\n",
0
                                 [[exception name] cStringUsingEncoding:NSUTF8StringEncoding],
0
                                 [[exception reason] cStringUsingEncoding:NSUTF8StringEncoding]);
0
                         }
0
-#ifndef DARWIN
0
+ #ifndef DARWIN
0
                         NS_ENDHANDLER
0
-#endif
0
+ #endif
0
                     }
0
                     cursor = [cursor cdr];
0
                 }
...
37
38
39
40
41
 
 
42
43
44
45
46
 
47
48
49
...
54
55
56
57
 
58
59
60
61
62
 
 
63
64
65
66
 
67
68
69
...
37
38
39
 
 
40
41
42
43
44
45
 
46
47
48
49
...
54
55
56
 
57
58
59
60
 
 
61
62
63
64
 
 
65
66
67
68
0
@@ -37,13 +37,13 @@
0
 
0
 (class TestClassMethodMissing is NuTestCase
0
      (- (id) testCustomSubclassClassMethodMissing is
0
- (assert_equal (MySampleClass hello) "Handling message (hello)")
0
- (assert_equal (MySampleClass nu:"rocks") "Handling message (nu: rocks)"))
0
+ (assert_equal "Handling message (hello)" (MySampleClass hello) )
0
+ (assert_equal "Handling message (nu: \"rocks\")" (MySampleClass nu:"rocks") ))
0
      
0
      (if (eq (uname) "Darwin")
0
          (- (id) testNSWorkspaceSingletonRemoval is
0
             ;; I'm almost positive that all Nubies on Macs will have Xcode installed.
0
- (assert_not_equal (NSWorkspace fullPathForApplication:"Xcode") nil)
0
+ (assert_not_equal nil (NSWorkspace fullPathForApplication:"Xcode") )
0
             (assert_equal ((NSWorkspace sharedWorkspace) notificationCenter) (NSWorkspace notificationCenter))
0
             (assert_equal ((NSWorkspace sharedWorkspace) activeApplication) (NSWorkspace activeApplication)))
0
          
0
@@ -54,16 +54,15 @@
0
             (assert_throws "NuUnknownMessage" (do () (NSObject shouldFail:2))))
0
          
0
          (- (id) testInheritedClassMethodMissing is
0
- (assert_equal (YetAnotherClass hello) "Handling message (hello)"))
0
+ (assert_equal "Handling message (hello)" (YetAnotherClass hello)))
0
          
0
          (- (id) testTwoUnknownMessageHandlers is
0
             (set two (TwoHandle new))
0
- (assert_equal (two hello) "Instance-based unknown message: (hello)")
0
- (assert_equal (TwoHandle goodbye) "Class-based unknown message: (goodbye)"))
0
+ (assert_equal "Instance-based unknown message: (hello)" (two hello))
0
+ (assert_equal "Class-based unknown message: (goodbye)" (TwoHandle goodbye)))
0
          
0
          (- (id) testOverridingClassBasedMethodMissing is
0
- (assert_equal (Overrider hello) "Overrode successfully with message (hello)"))))
0
-
0
+ (assert_equal "Overrode successfully with message (hello)" (Overrider hello)))))
0
 
0
 ;; helpers for method missing tests
0
 
...
103
104
105
106
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107
...
103
104
105
 
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
0
@@ -103,5 +103,20 @@ Line 2
0
 END)
0
           (set lines (x lines))
0
           (assert_equal 3 (lines count))
0
- (assert_equal "Line 1" (lines objectAtIndex:1))))
0
+ (assert_equal "Line 1" (lines objectAtIndex:1)))
0
+
0
+ (imethod (id) testEscapedRepresentations is
0
+ ;; verify the named characters
0
+ (assert_equal "(\"\\a\\b\\t\\n\\f\\r\\e\")" ('("\a\b\t\n\f\r\e") stringValue))
0
+ ;; verify escaping of low-valued characters
0
+ (assert_equal "\"\\x01\\x02\"" ("\x01\x02" escapedStringRepresentation))
0
+ ;;verify that 0x1f is the highest character escaped, 0x20 is kept as-is
0
+ (assert_equal "\"\\x1f \"" ("\x1f\x20" escapedStringRepresentation))
0
+ ;; verify that 0x7e is not escaped but 0x7f is
0
+ (assert_equal "\"~\\x7f\"" ("\x7e\x7f" escapedStringRepresentation))
0
+ ;; verify escaping of higher-valued one-byte characters
0
+ (assert_equal "\"\\xe0\\xf0\"" ("\xE0\xf0" escapedStringRepresentation))
0
+ ;; verify escaping of unicode characters
0
+ (assert_equal "\"\\u0100\\uffff\"" ("\u0100\uffFF" escapedStringRepresentation))))
0
+
0
 

Comments

    No one has commented yet.