Java Code Examples for com.intellij.openapi.util.text.StringHash

The following examples show how to use com.intellij.openapi.util.text.StringHash. These examples are extracted from open source projects. 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 Project: consulo   Source File: StringsPool.java    License: Apache License 2.0 5 votes vote down vote up
public static String getFromPool(String value) {
  if (value == null) return null;
  if (value.length() == 0) return EMPTY;

  final long hash = StringHash.calc(value);
  String reused = (String) myReusableStrings.get(hash);
  if (reused != null) return reused;
  // new String() is required because value often is passed as substring which has a reference to original char array
  // see {@link String.substring(int, int} method implementation.
  //noinspection RedundantStringConstructorCall
  reused = new String(value);
  myReusableStrings.put(hash, reused);
  return reused;
}
 
Example 2
Source Project: Intellij-Dust   Source File: GeneratedParserUtilBase.java    License: MIT License 5 votes vote down vote up
private boolean addExpected(StringBuilder sb, int offset, boolean expected) {
    String[] strings = new String[variants.size()];
    long[] hashes = new long[strings.length];
    Arrays.fill(strings, "");
    int count = 0;
    loop: for (Variant variant : expected? variants : unexpected) {
        if (offset == variant.offset) {
            String text = variant.object.toString();
            long hash = StringHash.calc(text);
            for (int i=0; i<count; i++) {
                if (hashes[i] == hash) continue loop;
            }
            hashes[count] = hash;
            strings[count] = text;
            count++;
        }
    }
    Arrays.sort(strings);
    count = 0;
    for (String s : strings) {
        if (s == "") continue;
        if (count++ > 0) {
            if (count > MAX_VARIANTS_TO_DISPLAY) {
                sb.append(" and ...");
                break;
            }
            else {
                sb.append(", ");
            }
        }
        char c = s.charAt(0);
        String displayText = c == '<' || StringUtil.isJavaIdentifierStart(c) ? s : '\'' + s + '\'';
        sb.append(displayText);
    }
    if (count > 1 && count < MAX_VARIANTS_TO_DISPLAY) {
        int idx = sb.lastIndexOf(", ");
        sb.replace(idx, idx + 1, " or");
    }
    return count > 0;
}
 
Example 3
Source Project: intellij-latte   Source File: GeneratedParserUtilBase.java    License: MIT License 4 votes vote down vote up
private boolean addExpected(StringBuilder sb, int position, boolean expected) {
	MyList<Variant> list = expected ? variants : unexpected;
	String[] strings = new String[list.size()];
	long[] hashes = new long[strings.length];
	Arrays.fill(strings, "");
	int count = 0;
	loop: for (Variant variant : list) {
		if (position == variant.position) {
			String text = variant.object.toString();
			long hash = StringHash.calc(text);
			for (int i=0; i<count; i++) {
				if (hashes[i] == hash) continue loop;
			}
			hashes[count] = hash;
			strings[count] = text;
			count++;
		}
	}
	Arrays.sort(strings);
	count = 0;
	for (String s : strings) {
		if (s.length() == 0) continue;
		if (count++ > 0) {
			if (count > MAX_VARIANTS_TO_DISPLAY) {
				sb.append(" and ...");
				break;
			}
			else {
				sb.append(", ");
			}
		}
		char c = s.charAt(0);
		String displayText = c == '<' || StringUtil.isJavaIdentifierStart(c) ? s : '\'' + s + '\'';
		sb.append(displayText);
	}
	if (count > 1 && count < MAX_VARIANTS_TO_DISPLAY) {
		int idx = sb.lastIndexOf(", ");
		sb.replace(idx, idx + 1, " or");
	}
	return count > 0;
}
 
Example 4
Source Project: intellij-xquery   Source File: GeneratedParserUtilBase.java    License: Apache License 2.0 4 votes vote down vote up
private boolean addExpected(StringBuilder sb, int position, boolean expected) {
    MyList<Variant> list = expected ? variants : unexpected;
    String[] strings = new String[list.size()];
    long[] hashes = new long[strings.length];
    Arrays.fill(strings, "");
    int count = 0;
    loop: for (Variant variant : list) {
        if (position == variant.position) {
            String text = variant.object.toString();
            long hash = StringHash.calc(text);
            for (int i=0; i<count; i++) {
                if (hashes[i] == hash) continue loop;
            }
            hashes[count] = hash;
            strings[count] = text;
            count++;
        }
    }
    Arrays.sort(strings);
    count = 0;
    for (String s : strings) {
        if (s.length() == 0) continue;
        if (count++ > 0) {
            if (count > MAX_VARIANTS_TO_DISPLAY) {
                sb.append(" and ...");
                break;
            }
            else {
                sb.append(", ");
            }
        }
        char c = s.charAt(0);
        String displayText = c == '<' || isJavaIdentifierStart(c) ? s : '\'' + s + '\'';
        sb.append(displayText);
    }
    if (count > 1 && count < MAX_VARIANTS_TO_DISPLAY) {
        int idx = sb.lastIndexOf(", ");
        sb.replace(idx, idx + 1, " or");
    }
    return count > 0;
}
 
Example 5
Source Project: consulo   Source File: TreeState.java    License: Apache License 2.0 4 votes vote down vote up
@Nonnull
private static String calcType(@Nullable Object userObject) {
  if (userObject == null) return "";
  String name = userObject.getClass().getName();
  return Integer.toHexString(StringHash.murmur(name, 31)) + ":" + StringUtil.getShortName(name);
}
 
Example 6
Source Project: consulo   Source File: UsedColors.java    License: Apache License 2.0 4 votes vote down vote up
private static int hashColor(@Nonnull String name, int colorsCount) {
  return Math.abs(StringHash.murmur(name, 0x55AA)) % colorsCount;
}
 
Example 7
Source Project: consulo   Source File: PatternCompilerImpl.java    License: Apache License 2.0 4 votes vote down vote up
public LazyPresentablePattern(final Node node) {
  myNode = node;
  myHashCode = StringHash.calc(toString());
}
 
Example 8
Source Project: consulo   Source File: ConsoleRootType.java    License: Apache License 2.0 4 votes vote down vote up
@Nonnull
public String getContentPathName(@Nonnull String id) {
  return Long.toHexString(StringHash.calc(id));
}
 
Example 9
Source Project: consulo   Source File: ConsoleRootType.java    License: Apache License 2.0 4 votes vote down vote up
@Nonnull
public String getHistoryPathName(@Nonnull String id) {
  return Long.toHexString(StringHash.calc(id));
}
 
Example 10
Source Project: consulo   Source File: ConsoleHistoryController.java    License: Apache License 2.0 4 votes vote down vote up
@Nonnull
private String getOldHistoryFilePath(final String id) {
  String pathName = myRootType.getConsoleTypeId() + Long.toHexString(StringHash.calc(id));
  return ContainerPathManager.get().getSystemPath() + File.separator + "userHistory" + File.separator + pathName + ".hist.xml";
}
 
Example 11
Source Project: consulo   Source File: GeneratedParserUtilBase.java    License: Apache License 2.0 4 votes vote down vote up
private boolean addExpected(StringBuilder sb, int position, boolean expected) {
  MyList<Variant> list = expected ? variants : unexpected;
  String[] strings = new String[list.size()];
  long[] hashes = new long[strings.length];
  Arrays.fill(strings, "");
  int count = 0;
  loop: for (Variant variant : list) {
    if (position == variant.position) {
      String text = variant.object.toString();
      long hash = StringHash.calc(text);
      for (int i=0; i<count; i++) {
        if (hashes[i] == hash) continue loop;
      }
      hashes[count] = hash;
      strings[count] = text;
      count++;
    }
  }
  Arrays.sort(strings);
  count = 0;
  for (String s : strings) {
    if (s.length() == 0) continue;
    if (count++ > 0) {
      if (count > MAX_VARIANTS_TO_DISPLAY) {
        sb.append(" and ...");
        break;
      }
      else {
        sb.append(", ");
      }
    }
    char c = s.charAt(0);
    String displayText = c == '<' || isJavaIdentifierStart(c) ? s : '\'' + s + '\'';
    sb.append(displayText);
  }
  if (count > 1 && count < MAX_VARIANTS_TO_DISPLAY) {
    int idx = sb.lastIndexOf(", ");
    sb.replace(idx, idx + 1, " or");
  }
  return count > 0;
}