content: /*
   * Copyright (C) 2018 Light Team Software
   *
   * This file is part of ModPE IDE.
   *
   * ModPE IDE is free software: you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation, either version 3 of the License, or
   * (at your option) any later version.
   *
   * ModPE IDE 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 General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
   */
  
  package com.github.ahmadaghazadeh.editor.processor.utils.text;
  
  import android.widget.MultiAutoCompleteTextView;
  
  /**
   * @author Trần Lê Duy
   */
  public class SymbolsTokenizer implements MultiAutoCompleteTextView.Tokenizer {
  
      private static final String TOKEN = "!@#$%^&*()_+-={}|[]:;'<>/<.? \r\n\t";
  
      @Override
      public int findTokenStart(CharSequence text, int cursor) {
          int i = cursor;
          while (i > 0 && !TOKEN.contains(Character.toString(text.charAt(i - 1)))) {
              i--;
          }
          while (i < cursor && text.charAt(i) == ' ') {
              i++;
          }
          return i;
      }
  
      @Override
      public int findTokenEnd(CharSequence text, int cursor) {
          int i = cursor;
          int len = text.length();
          while (i < len) {
              if (TOKEN.contains(Character.toString(text.charAt(i - 1)))) {
                  return i;
              } else {
                  i++;
              }
          }
          return len;
      }
  
      @Override
      public CharSequence terminateToken(CharSequence text) {
          int i = text.length();
          while (i > 0 && text.charAt(i - 1) == ' ') {
              i--;
          }
          return text;
      }
  }
  