Java Code Examples for com.intellij.util.text.CharArrayUtil#fromSequenceWithoutCopying()

The following examples show how to use com.intellij.util.text.CharArrayUtil#fromSequenceWithoutCopying() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LeafElement.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public boolean textContains(char c) {
  final CharSequence text = myText;
  final int len = text.length();

  if (len > TEXT_MATCHES_THRESHOLD) {
    char[] chars = CharArrayUtil.fromSequenceWithoutCopying(text);
    if (chars != null) {
      for (char aChar : chars) {
        if (aChar == c) return true;
      }
      return false;
    }
  }

  for (int i = 0; i < len; ++i) {
    if (c == text.charAt(i)) return true;
  }

  return false;
}
 
Example 2
Source File: FindManagerImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
private FindResult findStringLoop(CharSequence text, int offset, FindModel model, VirtualFile file, @Nullable Predicate<FindResult> filter) {
  final char[] textArray = CharArrayUtil.fromSequenceWithoutCopying(text);
  while (true) {
    FindResult result = doFindString(text, textArray, offset, model, file);
    if (filter == null || filter.test(result)) {
      if (!model.isWholeWordsOnly()) {
        return result;
      }
      if (!result.isStringFound()) {
        return result;
      }
      if (isWholeWord(text, result.getStartOffset(), result.getEndOffset())) {
        return result;
      }
    }

    offset = model.isForward() ? result.getStartOffset() + 1 : result.getEndOffset() - 1;
    if (offset > text.length() || offset < 0) return NOT_FOUND_RESULT;
  }
}
 
Example 3
Source File: IdTableBuilding.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
@Nonnull
public Map<IdIndexEntry, Integer> map(final FileContent inputData) {
  final CharSequence chars = inputData.getContentAsText();
  final char[] charsArray = CharArrayUtil.fromSequenceWithoutCopying(chars);
  final IdDataConsumer consumer = new IdDataConsumer();
  myScanner.processWords(chars, new Processor<WordOccurrence>() {
    @Override
    public boolean process(final WordOccurrence t) {
      if (charsArray != null && t.getBaseText() == chars) {
        consumer.addOccurrence(charsArray, t.getStart(), t.getEnd(), convertToMask(t.getKind()));
      }
      else {
        consumer.addOccurrence(t.getBaseText(), t.getStart(), t.getEnd(), convertToMask(t.getKind()));
      }
      return true;
    }

    private int convertToMask(final WordOccurrence.Kind kind) {
      if (kind == null) return UsageSearchContext.ANY;
      if (kind == WordOccurrence.Kind.CODE) return UsageSearchContext.IN_CODE;
      if (kind == WordOccurrence.Kind.COMMENTS) return UsageSearchContext.IN_COMMENTS;
      if (kind == WordOccurrence.Kind.LITERALS) return UsageSearchContext.IN_STRINGS;
      if (kind == WordOccurrence.Kind.FOREIGN_LANGUAGE) return UsageSearchContext.IN_FOREIGN_LANGUAGES;
      return 0;
    }
  });
  return consumer.getResult();
}
 
Example 4
Source File: StringUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static void assertValidSeparators(@Nonnull CharSequence s) {
  char[] chars = CharArrayUtil.fromSequenceWithoutCopying(s);
  int slashRIndex = -1;

  if (chars != null) {
    for (int i = 0, len = s.length(); i < len; ++i) {
      if (chars[i] == '\r') {
        slashRIndex = i;
        break;
      }
    }
  }
  else {
    for (int i = 0, len = s.length(); i < len; i++) {
      if (s.charAt(i) == '\r') {
        slashRIndex = i;
        break;
      }
    }
  }

  if (slashRIndex != -1) {
    String context = String.valueOf(last(s.subSequence(0, slashRIndex), 10, true)) + first(s.subSequence(slashRIndex, s.length()), 10, true);
    context = escapeStringCharacters(context);
    LOG.error("Wrong line separators: '" + context + "' at offset " + slashRIndex);
  }
}
 
Example 5
Source File: PsiBuilderImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
private PsiBuilderImpl(@Nullable Project project,
                       @Nullable PsiFile containingFile,
                       @Nonnull LanguageVersion languageVersion,
                       @Nonnull ParserDefinition parserDefinition,
                       @Nonnull Lexer lexer,
                       @Nullable CharTable charTable,
                       @Nonnull CharSequence text,
                       @Nullable ASTNode originalTree,
                       @Nullable CharSequence lastCommittedText,
                       @Nullable MyTreeStructure parentLightTree,
                       @Nullable Object parentCachingNode) {
  myProject = project;
  myFile = containingFile;
  myLanguageVersion = languageVersion;
  myText = text;
  myTextArray = CharArrayUtil.fromSequenceWithoutCopying(text);
  myLexer = lexer;

  myWhitespaces = parserDefinition.getWhitespaceTokens(languageVersion);
  myComments = parserDefinition.getCommentTokens(languageVersion);
  myCharTable = charTable;
  myOriginalTree = originalTree;
  myLastCommittedText = lastCommittedText;
  if ((originalTree == null) != (lastCommittedText == null)) {
    throw new IllegalArgumentException("originalTree and lastCommittedText must be null/notnull together but got: originalTree=" +
                                       originalTree +
                                       "; lastCommittedText=" +
                                       (lastCommittedText == null ? null : "'" + StringUtil.first(lastCommittedText, 80, true) + "'"));
  }
  myParentLightTree = parentLightTree;
  myOffset = parentCachingNode instanceof LazyParseableToken ? ((LazyParseableToken)parentCachingNode).getStartOffset() : 0;

  cacheLexemes(parentCachingNode);
}
 
Example 6
Source File: PrefixSuffixStripperLexer.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public void start(CharSequence buffer, int startOffset, int endOffset, int initialState) {
  myBuffer = buffer;
  myBufferArray = CharArrayUtil.fromSequenceWithoutCopying(buffer);
  myTokenStart = startOffset;
  myTokenEnd = startOffset;
  myTokenType = null;
  myState = initialState;
  myBufferEnd = endOffset;
}
 
Example 7
Source File: BaseFilterLexer.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public void start(@Nonnull CharSequence buffer, int startOffset, int endOffset, int initialState) {
  super.start(buffer, startOffset, endOffset, initialState);
  myCachedBufferSequence = getBufferSequence();
  myCachedArraySequence = CharArrayUtil.fromSequenceWithoutCopying(myCachedBufferSequence);
}
 
Example 8
Source File: LoadTextUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nonnull
private static ConvertResult convertLineSeparatorsToSlashN(@Nonnull CharBuffer buffer) {
  int dst = 0;
  char prev = ' ';
  int crCount = 0;
  int lfCount = 0;
  int crlfCount = 0;

  final int length = buffer.length();
  final char[] bufferArray = CharArrayUtil.fromSequenceWithoutCopying(buffer);

  for (int src = 0; src < length; src++) {
    char c = bufferArray != null ? bufferArray[src] : buffer.charAt(src);
    switch (c) {
      case '\r':
        if (bufferArray != null) bufferArray[dst++] = '\n';
        else buffer.put(dst++, '\n');
        crCount++;
        break;
      case '\n':
        if (prev == '\r') {
          crCount--;
          crlfCount++;
        }
        else {
          if (bufferArray != null) bufferArray[dst++] = '\n';
          else buffer.put(dst++, '\n');
          lfCount++;
        }
        break;
      default:
        if (bufferArray != null) bufferArray[dst++] = c;
        else buffer.put(dst++, c);
        break;
    }
    prev = c;
  }

  String detectedLineSeparator = guessLineSeparator(crCount, lfCount, crlfCount);

  CharSequence result = buffer.length() == dst ? buffer : buffer.subSequence(0, dst);
  return new ConvertResult(result, detectedLineSeparator);
}