-
-
Notifications
You must be signed in to change notification settings - Fork 584
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
30 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 16 additions & 10 deletions
26
app/src/main/java/fr/neamar/kiss/normalizer/PhoneNormalizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,25 @@ | ||
package fr.neamar.kiss.normalizer; | ||
|
||
public class PhoneNormalizer { | ||
public static String simplifyPhoneNumber(String phoneNumber) { | ||
public static StringNormalizer.Result simplifyPhoneNumber(String phoneNumber) { | ||
// This is done manually for performance reason, | ||
// But the algorithm is just a regexp replacement of "[-.():/ ]" with "" | ||
StringBuilder sb = new StringBuilder(); | ||
int phoneLength = phoneNumber.length(); | ||
for(int i = 0; i < phoneLength; i++) | ||
{ | ||
char c = phoneNumber.charAt(i); | ||
if(c == ' ' || c == '-' || c == '.' || c == '(' || c == ')' || c == ':' || c == '/') { | ||
continue; | ||
|
||
int numCodePoints = Character.codePointCount(phoneNumber, 0, phoneNumber.length()); | ||
IntSequenceBuilder codePoints = new IntSequenceBuilder(numCodePoints); | ||
IntSequenceBuilder resultMap = new IntSequenceBuilder(numCodePoints); | ||
|
||
int i = 0; | ||
for (int iterCodePoint = 0; iterCodePoint < numCodePoints; iterCodePoint += 1) { | ||
int c = Character.codePointAt(phoneNumber, i); | ||
|
||
if (c != ' ' && c != '-' && c != '.' && c != '(' && c != ')' && c != ':' && c != '/') { | ||
codePoints.add(c); | ||
resultMap.add(i); | ||
} | ||
sb.append(c); | ||
i += Character.charCount(c); | ||
} | ||
return sb.toString(); | ||
|
||
return new StringNormalizer.Result(phoneNumber.length(), codePoints.toArray(), resultMap.toArray()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters