Skip to content

Commit c7242d0

Browse files
committed
🔧 Refactor string extension methods for improved performance and consistency. Changes include optimizing character counting, reversing strings, and joining words. Updated tests to cover edge cases and fixed bugs related to character occurrences and string formatting.
1 parent f6a2c17 commit c7242d0

3 files changed

Lines changed: 211 additions & 131 deletions

File tree

‎lib/src/nullable_string_extensions.dart‎

Lines changed: 37 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -578,12 +578,14 @@ extension MiscExtensionsNullable on String? {
578578
count++;
579579
if (i == letters.length - 1) {
580580
occurrences.add({checkingLetter: count});
581-
checkingLetter = letters[i];
582581
}
583582
} else {
584583
occurrences.add({checkingLetter: count});
585584
checkingLetter = letters[i];
586585
count = 1;
586+
if (i == letters.length - 1) {
587+
occurrences.add({checkingLetter: count});
588+
}
587589
}
588590
}
589591
return occurrences;
@@ -662,8 +664,7 @@ extension MiscExtensionsNullable on String? {
662664
return this;
663665
}
664666

665-
var letters = this!.split('').toList().reversed;
666-
return letters.reduce((current, next) => current + next);
667+
return this!.split('').reversed.join();
667668
}
668669

669670
/// Returns the first [n] characters of the `String`.
@@ -735,19 +736,11 @@ extension MiscExtensionsNullable on String? {
735736
}
736737

737738
var words = this!.trim().split(RegExp(r'(\s+)'));
738-
var slugWord = '';
739739

740740
if (this!.length == 1) {
741741
return this;
742742
}
743-
for (var i = 0; i <= words.length - 1; i++) {
744-
if (i == words.length - 1) {
745-
slugWord += words[i];
746-
} else {
747-
slugWord += words[i] + '_';
748-
}
749-
}
750-
return slugWord;
743+
return words.join('_');
751744
}
752745

753746
/// Returns the `String` to snake_case.
@@ -763,19 +756,11 @@ extension MiscExtensionsNullable on String? {
763756
}
764757

765758
var words = this!.toLowerCase().trim().split(RegExp(r'(\s+)'));
766-
var snakeWord = '';
767759

768760
if (this!.length == 1) {
769761
return this;
770762
}
771-
for (var i = 0; i <= words.length - 1; i++) {
772-
if (i == words.length - 1) {
773-
snakeWord += words[i];
774-
} else {
775-
snakeWord += words[i] + '_';
776-
}
777-
}
778-
return snakeWord;
763+
return words.join('_');
779764
}
780765

781766
/// Returns the `String` in camelcase.
@@ -944,16 +929,16 @@ extension MiscExtensionsNullable on String? {
944929
/// ```
945930
String? get replaceGreek {
946931
if (this.isBlank) return this;
947-
var normalizedWord = '';
932+
var sb = StringBuffer();
948933
for (var i = 0; i < this!.length; i++) {
949934
var character = this![i];
950935
if (StringHelpers.greekToLatin.containsKey(character)) {
951-
normalizedWord += StringHelpers.greekToLatin[character]!;
936+
sb.write(StringHelpers.greekToLatin[character]!);
952937
} else {
953-
normalizedWord += character;
938+
sb.write(character);
954939
}
955940
}
956-
return normalizedWord;
941+
return sb.toString();
957942
}
958943

959944
/// Adds a [replacement] character at [index] of the `String`.
@@ -1088,11 +1073,11 @@ extension MiscExtensionsNullable on String? {
10881073
if (this.isBlank || count <= 0) {
10891074
return this;
10901075
}
1091-
var repeated = this!;
1092-
for (var i = 0; i < count - 1; i++) {
1093-
repeated += this!;
1076+
var sb = StringBuffer();
1077+
for (var i = 0; i < count; i++) {
1078+
sb.write(this!);
10941079
}
1095-
return repeated;
1080+
return sb.toString();
10961081
}
10971082

10981083
/// Squeezes the `String` by removing repeats of a given character.
@@ -1107,15 +1092,15 @@ extension MiscExtensionsNullable on String? {
11071092
return this;
11081093
}
11091094

1110-
var sb = '';
1095+
var sb = StringBuffer();
11111096
for (var i = 0; i < this!.length; i++) {
11121097
if (i == 0 ||
11131098
this![i - 1] != this![i] ||
11141099
(this![i - 1] == this![i] && this![i] != char)) {
1115-
sb += this![i];
1100+
sb.write(this![i]);
11161101
}
11171102
}
1118-
return sb;
1103+
return sb.toString();
11191104
}
11201105

11211106
/// Checks if the `String` is consisted of same characters (ignores cases).
@@ -1218,21 +1203,20 @@ extension MiscExtensionsNullable on String? {
12181203
return this;
12191204
}
12201205

1221-
//var buffer = StringBuffer();
12221206
var maskChars = mask.toArray;
12231207
var index = 0;
1224-
var out = '';
1208+
var sb = StringBuffer();
12251209
for (var m in maskChars) {
12261210
if (m == specialChar) {
12271211
if (index < this!.length) {
1228-
out += this![index];
1212+
sb.write(this![index]);
12291213
index++;
12301214
}
12311215
} else {
1232-
out += m;
1216+
sb.write(m);
12331217
}
12341218
}
1235-
return out;
1219+
return sb.toString();
12361220
}
12371221

12381222
/// Removes the first [n] characters from the `String`.
@@ -1344,7 +1328,7 @@ extension MiscExtensionsNullable on String? {
13441328
if (index < 0) {
13451329
return null;
13461330
}
1347-
return this!.split('')[index];
1331+
return this![index];
13481332
}
13491333

13501334
/// Appends a [suffix] to the `String`.
@@ -1575,7 +1559,7 @@ extension MiscExtensionsNullable on String? {
15751559

15761560
int index = this!.indexOf(char);
15771561
if (index == -1) {
1578-
return null;
1562+
return '';
15791563
}
15801564

15811565
return this!.substring(0, index);
@@ -1721,23 +1705,13 @@ extension MiscExtensionsNullable on String? {
17211705
return this;
17221706
}
17231707

1724-
if (!this!.contains(pattern)) {
1725-
return defaultToBlank ? '' : this;
1726-
}
1727-
1728-
List<String> patternWords = pattern.split(' ');
1729-
1730-
if (patternWords.isEmpty) {
1731-
return defaultToBlank ? '' : this;
1732-
}
1733-
int indexOfLastPatternWord = this!.indexOf(patternWords.last);
1708+
int index = this!.indexOf(pattern);
17341709

1735-
if (patternWords.last.length == 0) {
1710+
if (index == -1) {
17361711
return defaultToBlank ? '' : this;
17371712
}
17381713

1739-
return this!.substring(
1740-
indexOfLastPatternWord + patternWords.last.length, this!.length);
1714+
return this!.substring(index + pattern.length);
17411715
}
17421716

17431717
/// Returns the `String` before a specific character
@@ -1757,25 +1731,13 @@ extension MiscExtensionsNullable on String? {
17571731
return this;
17581732
}
17591733

1760-
if (!this!.contains(pattern)) {
1761-
return defaultToBlank ? '' : this;
1762-
}
1763-
1764-
List<String> patternWords = pattern.split(' ');
1765-
1766-
if (patternWords.isEmpty) {
1767-
return defaultToBlank ? '' : this;
1768-
}
1769-
int indexOfFirstPatternWord = this!.indexOf(patternWords.first);
1734+
int index = this!.indexOf(pattern);
17701735

1771-
if (patternWords.last.length == 0) {
1736+
if (index == -1) {
17721737
return defaultToBlank ? '' : this;
17731738
}
17741739

1775-
return this!.substring(
1776-
0,
1777-
indexOfFirstPatternWord,
1778-
);
1740+
return this!.substring(0, index);
17791741
}
17801742

17811743
/// The Jaro distance is a measure of edit distance between two strings
@@ -2090,8 +2052,7 @@ extension MiscExtensionsNullable on String? {
20902052
return '';
20912053
}
20922054

2093-
final startIndex = pattern.length == 1 ? index + pattern.length : index;
2094-
return this!.substring(startIndex);
2055+
return this!.substring(index);
20952056
}
20962057

20972058
/// Adds a `String` after the first match of the [pattern]. The [pattern] should not be `null`.
@@ -2223,11 +2184,11 @@ extension MiscExtensionsNullable on String? {
22232184
return this;
22242185
}
22252186

2226-
List<String> suffix = ["bytes", "KB", "MB", "GB"];
2187+
List<String> suffix = ["bytes", "KB", "MB", "GB", "TB"];
22272188

22282189
int j = 0;
22292190

2230-
while (number! >= 1024 && j < 4) {
2191+
while (number! >= 1024 && j < suffix.length - 1) {
22312192
number = (number / 1024).floor();
22322193
j++;
22332194
}
@@ -2428,18 +2389,16 @@ extension MiscExtensionsNullable on String? {
24282389
return this;
24292390
}
24302391

2431-
List<String> letters = this!.toArray;
2432-
2433-
String swapped = '';
2392+
var sb = StringBuffer();
24342393

2435-
for (final l in letters) {
2394+
for (final l in this!.toArray) {
24362395
if (l.isUpperCase) {
2437-
swapped += l.toLowerCase();
2396+
sb.write(l.toLowerCase());
24382397
} else {
2439-
swapped += l.toUpperCase();
2398+
sb.write(l.toUpperCase());
24402399
}
24412400
}
2442-
return swapped;
2401+
return sb.toString();
24432402
}
24442403

24452404
/// Checks whether the provided `String` is a valid Swift code.

0 commit comments

Comments
 (0)