forked from cappuccino/cappuccino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCPString.j
849 lines (725 loc) · 22.6 KB
/
CPString.j
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
/*
* CPString.j
* Foundation
*
* Created by Francisco Tolmasky.
* Copyright 2008, 280 North, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
@import "CPException.j"
@import "CPObject.j"
@import "CPObjJRuntime.j"
@import "CPRange.j"
@import "CPSortDescriptor.j"
@import "CPURL.j"
@import "CPValue.j"
#define _CPMaxRange(aRange) ((aRange).location + (aRange).length)
/*!
A case insensitive search
@global
@class CPString
*/
CPCaseInsensitiveSearch = 1;
/*!
Exact character match
@global
@class CPString
*/
CPLiteralSearch = 2;
/*!
Start searching from the end of the string
@global
@class CPString
*/
CPBackwardsSearch = 4;
/*!
@global
@class CPString
*/
CPAnchoredSearch = 8;
/*!
Numbers in the string are compared as numbers instead of strings
@global
@class CPString
*/
CPNumericSearch = 64;
/*!
Search ignores diacritic marks.
@global
@class CPString
*/
CPDiacriticInsensitiveSearch = 128;
var CPStringUIDs = new CFMutableDictionary();
var CPStringRegexSpecialCharacters = [
'/', '.', '*', '+', '?', '|', '$', '^',
'(', ')', '[', ']', '{', '}', '\\'
],
CPStringRegexEscapeExpression = new RegExp("(\\" + CPStringRegexSpecialCharacters.join("|\\") + ")", 'g'),
CPStringRegexTrimWhitespace = new RegExp("(^\\s+|\\s+$)", 'g');
/*!
@class CPString
@ingroup foundation
@brief An immutable string (collection of characters).
CPString is an object that allows management of strings. Because CPString is
based on the JavaScript \c String object, CPStrings are immutable, although the
class does have methods that create new CPStrings generated from modifications to the
receiving instance.
A handy feature of CPString instances is that they can be used wherever a JavaScript is
required, and vice versa.
*/
@implementation CPString : CPObject
/*
@ignore
*/
+ (id)alloc
{
if ([self class] !== CPString)
return [super alloc];
return new String;
}
/*!
Returns a new string
*/
+ (id)string
{
return [[self alloc] init];
}
/*!
Returns a CPString containing the specified hash.
@param aHash the hash to represent as a string
*/
+ (id)stringWithHash:(unsigned)aHash
{
var hashString = parseInt(aHash, 10).toString(16);
return "000000".substring(0, MAX(6 - hashString.length, 0)) + hashString;
}
/*!
Returns a copy of the specified string.
@param aString a non-\c nil string to copy
@throws CPInvalidArgumentException if \c aString is \c nil
@return the new CPString
*/
+ (id)stringWithString:(CPString)aString
{
if (!aString)
[CPException raise:CPInvalidArgumentException
reason:"stringWithString: the string can't be 'nil'"];
return [[self alloc] initWithString:aString];
}
/*!
Initializes the string with data from the specified string.
@param aString the string to copy data from
@return the initialized CPString
*/
- (id)initWithString:(CPString)aString
{
if ([self class] === CPString)
return String(aString);
var result = new String(aString);
result.isa = [self class];
return result;
}
/*!
Initializes a string using C printf-style formatting. First argument should be a constant format string, like ' "float val = %f" ', remaining arguments should be the variables to print the values of, comma-separated.
@param format the format to be used, printf-style
@return the initialized CPString
*/
- (id)initWithFormat:(CPString)format, ...
{
if (!format)
[CPException raise:CPInvalidArgumentException
reason:"initWithFormat: the format can't be 'nil'"];
self = ObjectiveJ.sprintf.apply(this, Array.prototype.slice.call(arguments, 2));
return self;
}
/*!
Creates a new string using C printf-style formatting. First argument should be a constant format string,
like ' "float val = %f" ', remaining arguments should be the variables to print the values of, comma-separated.
@param format the format to be used, printf-style
@return the initialized CPString
*/
+ (id)stringWithFormat:(CPString)format, ...
{
if (!format)
[CPException raise:CPInvalidArgumentException
reason:"initWithFormat: the format can't be 'nil'"];
return ObjectiveJ.sprintf.apply(this, Array.prototype.slice.call(arguments, 2));
}
/*!
Returns a description of this CPString object.
*/
- (CPString)description
{
return self;
}
/*!
Returns the number of UTF-8 characters in the string.
*/
- (int)length
{
return length;
}
/*!
Returns the character at the specified index.
@param anIndex the index of the desired character
*/
- (CPString)characterAtIndex:(unsigned)anIndex
{
return charAt(anIndex);
}
// Combining strings
/*!
Returns a string made by appending to the receiver a string constructed from a given format
string and the following arguments
@param format the format string in printf-style.
@return the initialized CPString
*/
- (CPString)stringByAppendingFormat:(CPString)format, ...
{
if (!format)
[CPException raise:CPInvalidArgumentException reason:"initWithFormat: the format can't be 'nil'"];
return self + ObjectiveJ.sprintf.apply(this, Array.prototype.slice.call(arguments, 2));
}
/*!
Creates a new CPString from the concatenation of the receiver and the specified string.
@param aString the string to append to the receiver
@return the new string
*/
- (CPString)stringByAppendingString:(CPString)aString
{
return self + aString;
}
/*!
Returns a new string formed by padding characters or removing them.
If the padding length is shorter than the receiver's length, the
new string will be trimmed down to the padding length size.
If the padding length is longer than the receiver's length, then the
new string is repeatedly padded with the characters from the
specified string starting at the specified index.
@param aLength the desired length of the new CPString
@param aString the padding string to use (if necessary)
@param anIndex the index of the padding string to start from (if necessary to use)
@return the new padded string
*/
- (CPString)stringByPaddingToLength:(unsigned)aLength withString:(CPString)aString startingAtIndex:(unsigned)anIndex
{
if (length == aLength)
return self;
if (aLength < length)
return substr(0, aLength);
var string = self,
substring = aString.substring(anIndex),
difference = aLength - length;
while ((difference -= substring.length) >= 0)
string += substring;
if (-difference < substring.length)
string += substring.substring(0, -difference);
return string;
}
//Dividing Strings
/*!
Tokenizes the receiver string using the specified
delimiter. For example, if the receiver is:
\c "arash.francisco.ross.tom"
and the delimiter is:
\c "."
the returned array would contain:
<pre> ["arash", "francisco", "ross", "tom"] </pre>
@param the delimiter
@return the array of tokens
*/
- (CPArray)componentsSeparatedByString:(CPString)aString
{
return split(aString);
}
/*!
Returns a substring starting from the specified index to the end of the receiver.
@param anIndex the starting string (inclusive)
@return the substring
*/
- (CPString)substringFromIndex:(unsigned)anIndex
{
return substr(anIndex);
}
/*!
Returns a substring starting from the specified range \c location to the range \c length.
@param the range of the substring
@return the substring
*/
- (CPString)substringWithRange:(CPRange)aRange
{
if (aRange.location < 0 || _CPMaxRange(aRange) > length)
[CPException raise:CPRangeException reason:"aRange out of bounds"];
return substr(aRange.location, aRange.length);
}
/*!
Creates a substring from the beginning of the receiver to the specified index.
@param anIndex the last index of the receiver to use for the substring (inclusive)
@return the substring
*/
- (CPString)substringToIndex:(unsigned)anIndex
{
return substring(0, anIndex);
}
// Finding characters and substrings
/*!
Finds the range of characters in the receiver where the specified string exists. If the string
does not exist in the receiver, the range \c length will be 0.
@param aString the string to search for in the receiver
@return the range of characters in the receiver
*/
- (CPRange)rangeOfString:(CPString)aString
{
return [self rangeOfString:aString options:0];
}
/*!
Finds the range of characters in the receiver
where the specified string exists. The search
is subject to the options specified in the
specified mask which can be a combination of:
<pre>
CPCaseInsensitiveSearch
CPLiteralSearch
CPBackwardsSearch
CPAnchoredSearch
CPNumericSearch
</pre>
@param aString the string to search for
@param aMask the options to use in the search
@return the range of characters in the receiver. If the string was not found,
the \c length of the range will be 0.
*/
- (CPRange)rangeOfString:(CPString)aString options:(int)aMask
{
return [self rangeOfString:aString options:aMask range:nil];
}
/*!
Finds the range of characters in the receiver where the specified string
exists in the given range of the receiver.The search is subject to the
options specified in the specified mask which can be a combination of:
<pre>
CPCaseInsensitiveSearch
CPLiteralSearch
CPBackwardsSearch
CPAnchoredSearch
CPNumericSearch
</pre>
@param aString the string to search for
@param aMask the options to use in the search
@param aRange the range of the receiver in which to search for
@return the range of characters in the receiver. The range is relative to
the start of the full string and not the passed-in range. If the
string was not found, or if it was @"", the range will be
{CPNotFound, 0}.
*/
- (CPRange)rangeOfString:(CPString)aString options:(int)aMask range:(CPrange)aRange
{
// Searching for @"" always returns CPNotFound.
if (!aString)
return CPMakeRange(CPNotFound, 0);
var string = (aRange == nil) ? self : [self substringWithRange:aRange],
location = CPNotFound;
if (aMask & CPCaseInsensitiveSearch)
{
string = string.toLowerCase();
aString = aString.toLowerCase();
}
if (aMask & CPBackwardsSearch)
{
location = string.lastIndexOf(aString);
if (aMask & CPAnchoredSearch && location + aString.length != string.length)
location = CPNotFound;
}
else if (aMask & CPAnchoredSearch)
location = string.substr(0, aString.length).indexOf(aString) != CPNotFound ? 0 : CPNotFound;
else
location = string.indexOf(aString);
if (location == CPNotFound)
return CPMakeRange(CPNotFound, 0);
return CPMakeRange(location + (aRange ? aRange.location : 0), aString.length);
}
//Replacing Substrings
- (CPString)stringByEscapingRegexControlCharacters
{
return self.replace(CPStringRegexEscapeExpression, "\\$1");
}
/*!
Returns a new string in which all occurrences of a target string in the receiver are replaced by
another given string.
@param target The string to replace.
@param replacement the string with which to replace the \c target
*/
- (CPString)stringByReplacingOccurrencesOfString:(CPString)target withString:(CPString)replacement
{
return self.replace(new RegExp([target stringByEscapingRegexControlCharacters], "g"), replacement);
}
/*
Returns a new string in which all occurrences of a target string in a specified range of the receiver
are replaced by another given string.
@param target The string to replace
@param replacement the string with which to replace the \c target.
@param options A mask of options to use when comparing \c target with the receiver. Pass 0 to specify no options
@param searchRange The range in the receiver in which to search for \c target.
*/
- (CPString)stringByReplacingOccurrencesOfString:(CPString)target withString:(CPString)replacement options:(int)options range:(CPRange)searchRange
{
var start = substring(0, searchRange.location),
stringSegmentToSearch = substr(searchRange.location, searchRange.length),
end = substring(searchRange.location + searchRange.length, self.length),
target = [target stringByEscapingRegexControlCharacters],
regExp;
if (options & CPCaseInsensitiveSearch)
regExp = new RegExp(target, "gi");
else
regExp = new RegExp(target, "g");
return start + '' + stringSegmentToSearch.replace(regExp, replacement) + '' + end;
}
/*
Returns a new string in which the characters in a specified range of the receiver
are replaced by a given string.
@param range A range of characters in the receiver.
@param replacement The string with which to replace the characters in \c range.
*/
- (CPString)stringByReplacingCharactersInRange:(CPRange)range withString:(CPString)replacement
{
return '' + substring(0, range.location) + replacement + substring(range.location + range.length, self.length);
}
/*!
Returns a new string with leading and trailing whitespace trimmed
*/
- (CPString)stringByTrimmingWhitespace
{
return self.replace(CPStringRegexTrimWhitespace, "");
}
// Identifying and comparing strings
/*!
Compares the receiver to the specified string.
@param aString the string with which to compare
@return the result of the comparison
*/
- (CPComparisonResult)compare:(CPString)aString
{
return [self compare:aString options:nil];
}
/*
Compares the receiver to the specified string.
@param aString the string with which to compare
@return the result of the comparison
*/
- (CPComparisonResult)caseInsensitiveCompare:(CPString)aString
{
return [self compare:aString options:CPCaseInsensitiveSearch];
}
/*!
Compares the receiver to the specified string, using options.
@param aString the string with which to compare
@param aMask the options to use for the comparison
@return the result of the comparison
*/
- (CPComparisonResult)compare:(CPString)aString options:(int)aMask
{
var lhs = self,
rhs = aString;
if (aMask & CPCaseInsensitiveSearch)
{
lhs = lhs.toLowerCase();
rhs = rhs.toLowerCase();
}
if (aMask & CPDiacriticInsensitiveSearch)
{
lhs = lhs.stripDiacritics();
rhs = rhs.stripDiacritics();
}
if (lhs < rhs)
return CPOrderedAscending;
if (lhs > rhs)
return CPOrderedDescending;
return CPOrderedSame;
}
/*!
Compares the receiver to the specified string, using options in range.
@param aString the string with which to compare the range of the receiver specified by range.
@param aMask the options to use for the comparison
@param range the range of the receiver over which to perform the comparison. The range must not exceed the bounds of the receiver.
@return the result of the comparison
*/
- (CPComparisonResult)compare:(CPString)aString options:(int)aMask range:(CPRange)range
{
var lhs = [self substringWithRange:range],
rhs = aString;
return [lhs compare:rhs options:aMask];
}
/*!
Returns \c YES if the receiver starts
with the specified string. If \c aString
is empty, the method will return \c NO.
*/
- (BOOL)hasPrefix:(CPString)aString
{
return aString && aString != "" && indexOf(aString) == 0;
}
/*!
Returns \c NO if the receiver ends
with the specified string. If \c aString
is empty, the method will return \c NO.
*/
- (BOOL)hasSuffix:(CPString)aString
{
return aString && aString != "" && length >= aString.length && lastIndexOf(aString) == (length - aString.length);
}
- (BOOL)isEqual:(id)anObject
{
if (self === anObject)
return YES;
if (!anObject || ![anObject isKindOfClass:[CPString class]])
return NO;
return [self isEqualToString:anObject];
}
/*!
Returns \c YES if the specified string contains the same characters as the receiver.
*/
- (BOOL)isEqualToString:(CPString)aString
{
return self == String(aString);
}
/*!
Returns a hash of the string instance.
*/
- (unsigned)UID
{
var UID = CPStringUIDs.valueForKey(self);
if (!UID)
{
UID = objj_generateObjectUID();
CPStringUIDs.setValueForKey(self, UID);
}
return UID + "";
}
/*!
Returns a string containing characters the receiver and a given string have in common, starting from
the beginning of each up to the first characters that aren't equivalent.
@param aString the string with which to compare the receiver
*/
- (CPString)commonPrefixWithString:(CPString)aString
{
return [self commonPrefixWithString: aString options: 0];
}
/*!
Returns a string containing characters the receiver and a given string have in common, starting from
the beginning of each up to the first characters that aren't equivalent.
@param aString the string with which to compare the receiver
@param aMask options for comparison
*/
- (CPString)commonPrefixWithString:(CPString)aString options:(int)aMask
{
var len = 0, // length of common prefix
lhs = self,
rhs = aString,
min = MIN([lhs length], [rhs length]);
if (aMask & CPCaseInsensitiveSearch)
{
lhs = [lhs lowercaseString];
rhs = [rhs lowercaseString];
}
for (; len < min; len++ )
{
if ( [lhs characterAtIndex:len] !== [rhs characterAtIndex:len] )
break;
}
return [self substringToIndex:len];
}
/*!
Returns a copy of the receiver with all the first letters of words capitalized.
*/
- (CPString)capitalizedString
{
var parts = self.split(/\b/g), // split on word boundaries
i = 0,
count = parts.length;
for (; i < count; i++)
{
if (i == 0 || (/\s$/).test(parts[i - 1])) // only capitalize if previous token was whitespace
parts[i] = parts[i].substring(0, 1).toUpperCase() + parts[i].substring(1).toLowerCase();
else
parts[i] = parts[i].toLowerCase();
}
return parts.join("");
}
/*!
Returns a copy of the string with all its characters made lower case.
*/
- (CPString)lowercaseString
{
return toLowerCase();
}
/*!
Returns a copy of the string with all its characters made upper case.
*/
- (CPString)uppercaseString
{
return toUpperCase();
}
/*!
Returns the text as a floating point value.
*/
- (double)doubleValue
{
return parseFloat(self, 10);
}
/*!
Returns \c YES on encountering one of "Y", "y", "T", "t", or
a digit 1-9. Returns \c NO otherwise. This method skips the initial
whitespace characters, +,- followed by Zeroes.
*/
- (BOOL)boolValue
{
var replaceRegExp = new RegExp("^\\s*[\\+,\\-]?0*");
return RegExp("^[Y,y,t,T,1-9]").test(self.replace(replaceRegExp, ''));
}
/*!
Returns the text as a float point value.
*/
- (float)floatValue
{
return parseFloat(self, 10);
}
/*!
Returns the text as an integer
*/
- (int)intValue
{
return parseInt(self, 10);
}
/*!
Returns an the path components of this string. This
method assumes that the string's content is a '/'
separated file system path.
*/
- (CPArray)pathComponents
{
var result = split('/');
if (result[0] === "")
result[0] = "/";
if (result[result.length - 1] === "")
result.pop();
return result;
}
/*!
Returns the extension of the file denoted by this string.
The '.' is not a part of the extension. This method assumes
that the string's contents is the path to a file or just a filename.
*/
- (CPString)pathExtension
{
if (lastIndexOf('.') === CPNotFound)
return "";
return substr(lastIndexOf('.') + 1);
}
/*!
Returns the last component of this string.
This method assumes that the string's content is a '/'
separated file system path.
*/
- (CPString)lastPathComponent
{
var components = [self pathComponents];
return components[components.length - 1];
}
/*!
Deletes the last path component of a string.
This method assumes that the string's content is a '/'
separated file system path.
*/
- (CPString)stringByDeletingLastPathComponent
{
var path = self,
start = length - 1;
while (path.charAt(start) === '/')
start--;
path = path.substr(0, path.lastIndexOf('/', start));
if (path === "" && charAt(0) === '/')
return '/';
return path;
}
/*!
Deletes the extension of a string.
*/
- (CPString)stringByDeletingPathExtension
{
var extension = [self pathExtension];
if (extension === "")
return self;
if (lastIndexOf('.') < 1)
return self;
return substr(0, [self length] - (extension.length + 1));
}
- (CPString)stringByStandardizingPath
{
// FIXME: Expand tildes etc. in CommonJS?
return [[CPURL URLWithString:self] absoluteString];
}
@end
@implementation CPString (JSON)
/*!
Returns a string representing the supplied JavaScript object encoded as JSON.
*/
+ (CPString)JSONFromObject:(JSObject)anObject
{
return JSON.stringify(anObject);
}
/*!
Returns a JavaScript object decoded from the string's JSON representation.
*/
- (JSObject)objectFromJSON
{
return JSON.parse(self);
}
@end
@implementation CPString (UUID)
/*!
Returns a randomly generated Universally Unique Identifier.
*/
+ (CPString)UUID
{
var g = @"",
i = 0;
for (; i < 32; i++)
g += FLOOR(RAND() * 0xF).toString(0xF);
return g;
}
@end
var diacritics = [[192,198],[224,230],[231,231],[232,235],[236,239],[242,246],[249,252]], // Basic Latin ; Latin-1 Supplement.
normalized = [65,97,99,101,105,111,117];
String.prototype.stripDiacritics = function()
{
var output = "";
for (var indexSource = 0; indexSource < this.length; indexSource++)
{
var code = this.charCodeAt(indexSource);
for (var i = 0; i < diacritics.length; i++)
{
var drange = diacritics[i];
if (code >= drange[0] && code <= drange[drange.length - 1])
{
code = normalized[i];
break;
}
}
output += String.fromCharCode(code);
}
return output;
}
String.prototype.isa = CPString;