gt:                 String rest = str.substring(accessCode.length());
context: // Copyright (c) 2012, Rick Maddy
  // All rights reserved.
  //
  // Redistribution and use in source and binary forms, with or without
  // modification, are permitted provided that the following conditions are met:
  //
  // * Redistributions of source code must retain the above copyright notice, this
  //   list of conditions and the following disclaimer.
  //
  // * Redistributions in binary form must reproduce the above copyright notice,
  //   this list of conditions and the following disclaimer in the documentation
  //   and/or other materials provided with the distribution.
  //
  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  // OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  
  package ir.nikagram.PhoneFormat;
  
  import ir.nikagram.messenger.ApplicationLoader;
  import ir.nikagram.messenger.FileLog;
  
  import java.io.ByteArrayOutputStream;
  import java.io.InputStream;
  import java.nio.ByteBuffer;
  import java.nio.ByteOrder;
  import java.util.ArrayList;
  import java.util.HashMap;
  import java.util.Locale;
  
  public class PhoneFormat {
      public byte[] data;
      private boolean initialzed = false;
      public ByteBuffer buffer;
      public String defaultCountry;
      public String defaultCallingCode;
      public HashMap<String, Integer> callingCodeOffsets;
      public HashMap<String, ArrayList<String>> callingCodeCountries;
      public HashMap<String, CallingCodeInfo> callingCodeData;
      public HashMap<String, String> countryCallingCode;
  
      private static volatile PhoneFormat Instance = null;
      public static PhoneFormat getInstance() {
          PhoneFormat localInstance = Instance;
          if (localInstance == null) {
              synchronized (PhoneFormat.class) {
                  localInstance = Instance;
                  if (localInstance == null) {
                      Instance = localInstance = new PhoneFormat();
                  }
              }
          }
          return localInstance;
      }
  
      public static String strip(String str) {
          StringBuilder res = new StringBuilder(str);
          String phoneChars = "0123456789+*#";
          for (int i = res.length() - 1; i >= 0; i--) {
              if (!phoneChars.contains(res.substring(i, i + 1))) {
                  res.deleteCharAt(i);
              }
          }
          return res.toString();
      }
  
      public static String stripExceptNumbers(String str, boolean includePlus) {
          StringBuilder res = new StringBuilder(str);
          String phoneChars = "0123456789";
          if (includePlus) {
              phoneChars += "+";
          }
          for (int i = res.length() - 1; i >= 0; i--) {
              if (!phoneChars.contains(res.substring(i, i + 1))) {
                  res.deleteCharAt(i);
              }
          }
          return res.toString();
      }
  
      public static String stripExceptNumbers(String str) {
          return stripExceptNumbers(str, false);
      }
  
      public PhoneFormat() {
          init(null);
      }
  
      public PhoneFormat(String countryCode) {
          init(countryCode);
      }
  
      public void init(String countryCode) {
          InputStream stream = null;
          ByteArrayOutputStream bos = null;
          try {
              stream = ApplicationLoader.applicationContext.getAssets().open("PhoneFormats.dat");
              bos = new ByteArrayOutputStream();
              byte[] buf = new byte[1024];
              int len;
              while ((len = stream.read(buf, 0, 1024)) != -1) {
                  bos.write(buf, 0, len);
              }
              data = bos.toByteArray();
              buffer = ByteBuffer.wrap(data);
              buffer.order(ByteOrder.LITTLE_ENDIAN);
          } catch (Exception e) {
              e.printStackTrace();
              return;
          } finally {
              try {
                  if (bos != null) {
                      bos.close();
                  }
              } catch (Exception e) {
                  FileLog.e("tmessages", e);
              }
              try {
                  if (stream != null) {
                      stream.close();
                  }
              } catch (Exception e) {
                  FileLog.e("tmessages", e);
              }
          }
  
          if (countryCode != null && countryCode.length() != 0) {
              defaultCountry = countryCode;
          } else {
              Locale loc = Locale.getDefault();
              defaultCountry = loc.getCountry().toLowerCase();
          }
          callingCodeOffsets = new HashMap<>(255);
          callingCodeCountries = new HashMap<>(255);
          callingCodeData = new HashMap<>(10);
          countryCallingCode = new HashMap<>(255);
  
          parseDataHeader();
          initialzed = true;
      }
  
      public String defaultCallingCode() {
          return callingCodeForCountryCode(defaultCountry);
      }
  
      public String callingCodeForCountryCode(String countryCode) {
          return countryCallingCode.get(countryCode.toLowerCase());
      }
  
      public ArrayList countriesForCallingCode(String callingCode) {
          if (callingCode.startsWith("+")) {
              callingCode = callingCode.substring(1);
          }
  
          return callingCodeCountries.get(callingCode);
      }
  
      public CallingCodeInfo findCallingCodeInfo(String str) {
          CallingCodeInfo res = null;
          for (int i = 0; i < 3; i++) {
              if (i < str.length()) {
                  res = callingCodeInfo(str.substring(0, i + 1));
                  if (res != null) {
                      break;
                  }
              } else {
                  break;
              }
          }
  
          return res;
      }
  
      public String format(String orig) {
          if (!initialzed) {
              return orig;
          }
          try {
              String str = strip(orig);
  
              if (str.startsWith("+")) {
                  String rest = str.substring(1);
                  CallingCodeInfo info = findCallingCodeInfo(rest);
                  if (info != null) {
                      String phone = info.format(rest);
                      return "+" +  phone;
                  } else {
                      return orig;
                  }
              } else {
                  CallingCodeInfo info = callingCodeInfo(defaultCallingCode);
                  if (info == null) {
                      return orig;
                  }
  
                  String accessCode = info.matchingAccessCode(str);
                  if (accessCode != null) {
                      String rest = str.substring(accessCode.length());
                      String phone = rest;
                      CallingCodeInfo info2 = findCallingCodeInfo(rest);
                      if (info2 != null) {
                          phone = info2.format(rest);
                      }
  
                      if (phone.length() == 0) {
                          return accessCode;
                      } else {
                          return String.format("%s %s", accessCode, phone);
                      }
                  } else {
                      return info.format(str);
                  }
              }
          } catch (Exception e) {
              FileLog.e("tmessages", e);
              return orig;
          }
  
      }
  
      public boolean isPhoneNumberValid(String phoneNumber) {
          if (!initialzed) {
              return true;
          }
          String str = strip(phoneNumber);
  
          if (str.startsWith("+")) {
              String rest = str.substring(1);
              CallingCodeInfo info = findCallingCodeInfo(rest);
              return info != null && info.isValidPhoneNumber(rest);
          } else {
              CallingCodeInfo info = callingCodeInfo(defaultCallingCode);
              if (info == null) {
                  return false;
              }
  
              String accessCode = info.matchingAccessCode(str);
              if (accessCode != null) {
  