// Gets a formatting template which can be used to efficiently format a partial number where // digits are added one by one. private String getFormattingTemplate(String numberPattern, String numberFormat) { // Creates a phone number consisting only of the digit 9 that matches the // numberPattern by applying the pattern to the longestPhoneNumber string. String longestPhoneNumber = "999999999999999"; Matcher m = regexCache.getPatternForRegex(numberPattern).matcher(longestPhoneNumber); m.find(); // this will always succeed String aPhoneNumber = m.group(); // No formatting template can be created if the number of digits entered so far is longer than // the maximum the current formatting rule can accommodate. if (aPhoneNumber.length() < nationalNumber.length()) { return(""); } // Formats the number according to numberFormat String template = aPhoneNumber.replaceAll(numberPattern, numberFormat); // Replaces each digit with character DIGIT_PLACEHOLDER template = template.replaceAll("9", DIGIT_PLACEHOLDER); return(template); }
// Appends the specified string builder to this sequence. internal StringBuilder append(StringBuilder sb) { if (sb == null) { return(append(new String("null"))); } int len = sb.length(); int newcount = count + len; if (newcount > value.Length) { expandCapacity(newcount); } sb.getChars(0, len, value, count); count = newcount; return(this); }
internal static boolean allNumberGroupsRemainGrouped(PhoneNumberUtil util, PhoneNumber number, StringBuilder normalizedCandidate, String[] formattedNumberGroups) { int fromIndex = 0; // Check each group of consecutive digits are not broken into separate groupings in the // {@code normalizedCandidate} string. for (int i = 0; i < formattedNumberGroups.length(); i++) { // Fails if the substring of {@code normalizedCandidate} starting from {@code fromIndex} // doesn't contain the consecutive digits in formattedNumberGroups[i]. fromIndex = normalizedCandidate.indexOf(formattedNumberGroups[i], fromIndex); if (fromIndex < 0) { return(false); } // Moves {@code fromIndex} forward. fromIndex += formattedNumberGroups[i].length(); if (i == 0 && fromIndex < normalizedCandidate.length()) { // We are at the position right after the NDC. We get the region used for formatting // information based on the country code in the phone number, rather than the number itself, // as we do not need to distinguish between different countries with the same country // calling code and this is faster. String region = util.getRegionCodeForCountryCode(number.getCountryCode()); if (util.getNddPrefixForRegion(region, true) != null && Character.isDigit(normalizedCandidate.charAt(fromIndex))) { // This means there is no formatting symbol after the NDC. In this case, we only // accept the number if there is no formatting symbol at all in the number, except // for extensions. This is only important for countries with national prefixes. String nationalSignificantNumber = util.getNationalSignificantNumber(number); return(normalizedCandidate.substring(fromIndex - formattedNumberGroups[i].length()) .startsWith(nationalSignificantNumber)); } } } // The check here makes sure that we haven't mistakenly already used the extension to // match the last group of the subscriber number. Note the extension cannot have // formatting in-between digits. return(normalizedCandidate.substring(fromIndex).contains(number.getExtension())); }
/** * Combines the national number with any prefix (IDD/+ and country code or national prefix) that * was collected. A space will be inserted between them if the current formatting template * indicates this to be suitable. */ private String appendNationalNumber(String nationalNumber) { int prefixBeforeNationalNumberLength = prefixBeforeNationalNumber.length(); if (shouldAddSpaceAfterNationalPrefix && prefixBeforeNationalNumberLength > 0 && prefixBeforeNationalNumber.charAt(prefixBeforeNationalNumberLength - 1) != SEPARATOR_BEFORE_NATIONAL_NUMBER) { // We want to add a space after the national prefix if the national prefix formatting rule // indicates that this would normally be done, with the exception of the case where we already // appended a space because the NDD was surprisingly long. return(new String(prefixBeforeNationalNumber) + SEPARATOR_BEFORE_NATIONAL_NUMBER + nationalNumber); } else { return(prefixBeforeNationalNumber + nationalNumber); } }
public Matcher appendReplacement(StringBuffer sb, String replacement) { // If no match, return error if (first < 0) throw new InvalidOperationException("No match available"); // Process substitution string to replace group references with groups int cursor = 0; StringBuilder result = new StringBuilder(); while (cursor < replacement.length()) { char nextChar = replacement.charAt(cursor); if (nextChar == '\\') { cursor++; nextChar = replacement.charAt(cursor); result.append(nextChar); cursor++; } else if (nextChar == '$') { // Skip past $ cursor++; // A StringIndexOutOfBoundsException is thrown if // this "$" is the last character in replacement // string in current implementation, a IAE might be // more appropriate. nextChar = replacement.charAt(cursor); int refNum = -1; if (nextChar == '{') { cursor++; StringBuilder gsb = new StringBuilder(); while (cursor < replacement.length()) { nextChar = replacement.charAt(cursor); if (ASCII.isLower(nextChar) || ASCII.isUpper(nextChar) || ASCII.isDigit(nextChar)) { gsb.append(nextChar); cursor++; } else { break; } } if (gsb.length() == 0) throw new IllegalArgumentException( "named capturing group has 0 length name"); if (nextChar != '}') throw new IllegalArgumentException( "named capturing group is missing trailing '}'"); String gname = gsb.toString(); if (ASCII.isDigit(gname.charAt(0))) throw new IllegalArgumentException( "capturing group name {" + gname + "} starts with digit character"); if (!parentPattern.namedGroups().containsKey(gname)) throw new IllegalArgumentException( "No group with name {" + gname + "}"); refNum = parentPattern.namedGroups().get(gname); cursor++; } else { // The first number is always a group refNum = (int)nextChar - '0'; if ((refNum < 0)||(refNum > 9)) throw new IllegalArgumentException( "Illegal group reference"); cursor++; // Capture the largest legal group string boolean done = false; while (!done) { if (cursor >= replacement.length()) { break; } int nextDigit = replacement.charAt(cursor) - '0'; if ((nextDigit < 0)||(nextDigit > 9)) { // not a number break; } int newRefNum = (refNum * 10) + nextDigit; if (groupCount() < newRefNum) { done = true; } else { refNum = newRefNum; cursor++; } } } // Append group if (start(refNum) != -1 && end(refNum) != -1) result.append(text, start(refNum), end(refNum)); } else { result.append(nextChar); cursor++; } } // Append the intervening text sb.append(text, lastAppendPosition, first); // Append the match substitution sb.append(result); lastAppendPosition = last; return this; }
private String inputDigitWithOptionToRememberPosition(char nextChar, boolean rememberPosition) { accruedInput.append(nextChar); if (rememberPosition) { originalPosition = accruedInput.length(); } // We do formatting on-the-fly only when each character entered is either a digit, or a plus // sign (accepted at the start of the number only). if (!isDigitOrLeadingPlusSign(nextChar)) { ableToFormat = false; inputHasFormatting = true; } else { nextChar = normalizeAndAccrueDigitsAndPlusSign(nextChar, rememberPosition); } if (!ableToFormat) { // When we are unable to format because of reasons other than that formatting chars have been // entered, it can be due to really long IDDs or NDDs. If that is the case, we might be able // to do formatting again after extracting them. if (inputHasFormatting) { return(accruedInput.toString()); } else if (attemptToExtractIdd()) { if (attemptToExtractCountryCallingCode()) { return(attemptToChoosePatternWithPrefixExtracted()); } } else if (ableToExtractLongerNdd()) { // Add an additional space to separate long NDD and national significant number for // readability. We don't set shouldAddSpaceAfterNationalPrefix to true, since we don't want // this to change later when we choose formatting templates. prefixBeforeNationalNumber.append(SEPARATOR_BEFORE_NATIONAL_NUMBER); return(attemptToChoosePatternWithPrefixExtracted()); } return(accruedInput.toString()); } // We start to attempt to format only when at least MIN_LEADING_DIGITS_LENGTH digits (the plus // sign is counted as a digit as well for this purpose) have been entered. switch (accruedInputWithoutFormatting.length()) { case 0: case 1: case 2: return(accruedInput.toString()); default: if (accruedInputWithoutFormatting.length() == 3) { if (attemptToExtractIdd()) { isExpectingCountryCallingCode = true; } else // No IDD or plus sign is found, might be entering in national format. { nationalPrefixExtracted = removeNationalPrefixFromNationalNumber(); return(attemptToChooseFormattingPattern()); } } if (isExpectingCountryCallingCode) { if (attemptToExtractCountryCallingCode()) { isExpectingCountryCallingCode = false; } return(prefixBeforeNationalNumber + nationalNumber.toString()); } if (possibleFormats.size() > 0) // The formatting pattern is already chosen. { String tempNationalNumber = inputDigitHelper(nextChar); // See if the accrued digits can be formatted properly already. If not, use the results // from inputDigitHelper, which does formatting based on the formatting pattern chosen. String formattedNumber = attemptToFormatAccruedDigits(); if (formattedNumber.length() > 0) { return(formattedNumber); } narrowDownPossibleFormats(nationalNumber.toString()); if (maybeCreateNewTemplate()) { return(inputAccruedNationalNumber()); } return(ableToFormat ? appendNationalNumber(tempNationalNumber) : accruedInput.toString()); } else { return(attemptToChooseFormattingPattern()); } } }
public CompilerResults compileFromFiles(CompilerParameters parameters, File[] files) { var results = new CompilerResults(); this.context = new CompilerContext(parameters, results); this.statementValidator = new StatementValidator(this.context); this.expressionValidator = new ExpressionValidator(this.context); this.statementValidator.ExpressionValidator = this.expressionValidator; this.expressionValidator.StatementValidator = this.statementValidator; this.reachabilityChecker = new ReachabilityChecker(context); this.assignmentChecker = new AssignmentChecker(context); this.bytecodeGenerator = new BytecodeGenerator(context); bool tragicError = false; var buffer = new char[4096]; var sb = new StringBuilder(); var parser = new Parser(); foreach (var file in files) { sb.setLength(0); InputStreamReader reader = null; try { reader = new InputStreamReader(new FileInputStream(file), Charset.forName("UTF-8")); int read; while ((read = reader.read(buffer)) != -1) { sb.append(buffer, 0, read); } var text = new char[sb.length()]; sb.getChars(0, sizeof(text), text, 0); if (sizeof(text) > 0) { if (text[sizeof(text) - 1] == '\u001a') { text[sizeof(text) - 1] = ' '; } } var preprocessor = new Preprocessor(results.codeErrorManager, text); preprocessor.Filename = file.getAbsolutePath(); preprocessor.Symbols.addAll(parameters.Symbols); var scanner = new PreprocessedTextScanner(results.codeErrorManager, preprocessor.preprocess()); scanner.Filename = file.getAbsolutePath(); var compilationUnit = parser.parseCompilationUnit(scanner); if (compilationUnit != null) { compilationUnit.Symbols = preprocessor.Symbols; context.CompilationUnits.add(compilationUnit); } } catch (CodeErrorException) { } catch (Exception e) { e.printStackTrace(); tragicError = true; break; } finally { if (reader != null) { try { reader.close(); } catch (IOException) { } } } } if (!tragicError) { if (!context.HasErrors) { if (parameters.ProgressTracker != null) { parameters.ProgressTracker.compilationStageFinished(CompilationStage.Parsing); } doCompile(); } } this.context = null; this.statementValidator = null; this.expressionValidator = null; this.reachabilityChecker = null; this.assignmentChecker = null; this.queryTranslator = null; this.documentationBuilder = null; if (parameters.ProgressTracker != null) { parameters.ProgressTracker.compilationFinished(); } return results; }
// Appends the specified string builder to this sequence. internal StringBuilder append(StringBuilder sb) { if (sb == null) return append(new String("null")); int len = sb.length(); int newcount = count + len; if (newcount > value.Length) expandCapacity(newcount); sb.getChars(0, len, value, count); count = newcount; return this; }
public StringBuilder(StringBuilder str) : base(str.length() + 16) { append(str.toString()); }
private int normalizeCharClass(StringBuilder newPattern, int i) { StringBuilder charClass = new StringBuilder(); StringBuilder eq = null; int lastCodePoint = -1; String result; i++; charClass.append("["); while(true) { int c = normalizedPattern.codePointAt(i); StringBuilder sequenceBuffer; if (c == ']' && lastCodePoint != '\\') { charClass.append((char)c); break; } else if (Character.getType(c) == Character.NON_SPACING_MARK) { sequenceBuffer = new StringBuilder(); sequenceBuffer.appendCodePoint(lastCodePoint); while(Character.getType(c) == Character.NON_SPACING_MARK) { sequenceBuffer.appendCodePoint(c); i += Character.charCount(c); if (i >= normalizedPattern.length()) break; c = normalizedPattern.codePointAt(i); } String ea = produceEquivalentAlternation( sequenceBuffer.toString()); charClass.setLength(charClass.length()-Character.charCount(lastCodePoint)); if (eq == null) eq = new StringBuilder(); eq.append('|'); eq.append(ea); } else { charClass.appendCodePoint(c); i++; } if (i == normalizedPattern.length()) throw error(new String("Unclosed character class")); lastCodePoint = c; } if (eq != null) { result = new String("(?:"+charClass.toString()+eq.toString()+")"); } else { result = charClass.toString(); } newPattern.append(result); return i; }
private void normalize() { boolean inCharClass = false; int lastCodePoint = -1; // Convert pattern into normalizedD form normalizedPattern = Normalizer.normalize(pattern, Normalizer.Form.NFD); patternLength = normalizedPattern.length(); // Modify pattern to match canonical equivalences StringBuilder newPattern = new StringBuilder(patternLength); for(int i=0; i<patternLength; ) { int c = normalizedPattern.codePointAt(i); StringBuilder sequenceBuffer; if ((Character.getType(c) == Character.NON_SPACING_MARK) && (lastCodePoint != -1)) { sequenceBuffer = new StringBuilder(); sequenceBuffer.appendCodePoint(lastCodePoint); sequenceBuffer.appendCodePoint(c); while(Character.getType(c) == Character.NON_SPACING_MARK) { i += Character.charCount(c); if (i >= patternLength) break; c = normalizedPattern.codePointAt(i); sequenceBuffer.appendCodePoint(c); } String ea = produceEquivalentAlternation( sequenceBuffer.toString()); newPattern.setLength(newPattern.length()-Character.charCount(lastCodePoint)); newPattern.append("(?:").append(ea).append(")"); } else if (c == '[' && lastCodePoint != '\\') { i = normalizeCharClass(newPattern, i); } else { newPattern.appendCodePoint(c); } lastCodePoint = c; i += Character.charCount(c); } normalizedPattern = newPattern.toString(); }
private String groupname(int ch) { StringBuilder sb = new StringBuilder(); sb.append(Character.toChars(ch)); while (ASCII.isLower(ch=read()) || ASCII.isUpper(ch) || ASCII.isDigit(ch)) { sb.append(Character.toChars(ch)); } if (sb.length() == 0) throw error(new String("named capturing group has 0 length name")); if (ch != '>') throw error(new String("named capturing group is missing trailing '>'")); return sb.toString(); }
private char[] getText(IFile file) { char[] text; if (parameters.AllFiles.getProjectRelativeName(file).equals(parameters.EditedFileName)) { text = parameters.EditedFileText; } else { using (var reader = new InputStreamReader(file.getContents(), file.getCharset())) { var sb = new StringBuilder(); int read; while ((read = reader.read(buffer)) != -1) { sb.append(buffer, 0, read); } text = new char[sb.length()]; sb.getChars(0, sizeof(text), text, 0); } } if (sizeof(text) > 0) { if (text[sizeof(text) - 1] == '\u001a') { text[sizeof(text) - 1] = ' '; } } return text; }
public Matcher appendReplacement(StringBuffer sb, String replacement) { // If no match, return error if (first < 0) { throw new InvalidOperationException("No match available"); } // Process substitution string to replace group references with groups int cursor = 0; StringBuilder result = new StringBuilder(); while (cursor < replacement.length()) { char nextChar = replacement.charAt(cursor); if (nextChar == '\\') { cursor++; nextChar = replacement.charAt(cursor); result.append(nextChar); cursor++; } else if (nextChar == '$') { // Skip past $ cursor++; // A StringIndexOutOfBoundsException is thrown if // this "$" is the last character in replacement // string in current implementation, a IAE might be // more appropriate. nextChar = replacement.charAt(cursor); int refNum = -1; if (nextChar == '{') { cursor++; StringBuilder gsb = new StringBuilder(); while (cursor < replacement.length()) { nextChar = replacement.charAt(cursor); if (ASCII.isLower(nextChar) || ASCII.isUpper(nextChar) || ASCII.isDigit(nextChar)) { gsb.append(nextChar); cursor++; } else { break; } } if (gsb.length() == 0) { throw new IllegalArgumentException( "named capturing group has 0 length name"); } if (nextChar != '}') { throw new IllegalArgumentException( "named capturing group is missing trailing '}'"); } String gname = gsb.toString(); if (ASCII.isDigit(gname.charAt(0))) { throw new IllegalArgumentException( "capturing group name {" + gname + "} starts with digit character"); } if (!parentPattern.namedGroups().containsKey(gname)) { throw new IllegalArgumentException( "No group with name {" + gname + "}"); } refNum = parentPattern.namedGroups().get(gname); cursor++; } else { // The first number is always a group refNum = (int)nextChar - '0'; if ((refNum < 0) || (refNum > 9)) { throw new IllegalArgumentException( "Illegal group reference"); } cursor++; // Capture the largest legal group string boolean done = false; while (!done) { if (cursor >= replacement.length()) { break; } int nextDigit = replacement.charAt(cursor) - '0'; if ((nextDigit < 0) || (nextDigit > 9)) // not a number { break; } int newRefNum = (refNum * 10) + nextDigit; if (groupCount() < newRefNum) { done = true; } else { refNum = newRefNum; cursor++; } } } // Append group if (start(refNum) != -1 && end(refNum) != -1) { result.append(text, start(refNum), end(refNum)); } } else { result.append(nextChar); cursor++; } } // Append the intervening text sb.append(text, lastAppendPosition, first); // Append the match substitution sb.append(result); lastAppendPosition = last; return(this); }
public StringBuilder(StringBuilder str) : base(str.length() + 16) { append(str.toString()); }