java.lang.String.codePointAt()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(10.9k)|赞(0)|评价(0)|浏览(176)

本文整理了Java中java.lang.String.codePointAt()方法的一些代码示例,展示了String.codePointAt()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。String.codePointAt()方法的具体详情如下:
包路径:java.lang.String
类名称:String
方法名:codePointAt

String.codePointAt介绍

[英]Returns the character (Unicode code point) at the specified index. The index refers to char values (Unicode code units) and ranges from 0 to #length()- 1.

If the char value specified at the given index is in the high-surrogate range, the following index is less than the length of this String, and the char value at the following index is in the low-surrogate range, then the supplementary code point corresponding to this surrogate pair is returned. Otherwise, the char value at the given index is returned.
[中]返回指定索引处的字符(Unicode代码点)。索引引用char值(Unicode代码单位),范围从0到#length()- 1
如果在给定索引处指定的char值在高代理项范围内,则以下索引小于此String的长度,并且以下索引处的char值在低代理项范围内,则返回与此代理项对对应的补充码点。否则,将返回给定索引处的char值。

代码示例

代码示例来源:origin: stanfordnlp/CoreNLP

public static boolean langIndependentPuncCheck(String token) {
 boolean isNotWord = true;
 for (int offset = 0; offset < token.length(); ) {
   final int codepoint = token.codePointAt(offset);
   if (Character.isLetterOrDigit(codepoint)) {
    isNotWord = false;
   }
   offset += Character.charCount(codepoint);
 }
 return isNotWord;
}

代码示例来源:origin: prestodb/presto

static int codePointIndexToCharIndex(String s, int codePointCount) {
 for (int i = 0, j = 0, length = s.length(), c; i < length; i += Character.charCount(c)) {
  if (j == codePointCount) {
   return i;
  }
  c = s.codePointAt(i);
  if ((Character.isISOControl(c) && c != '\n' && c != '\r')
    || c == Buffer.REPLACEMENT_CHARACTER) {
   return -1;
  }
  j++;
 }
 return s.length();
}

代码示例来源:origin: neo4j/neo4j

private int ltrimIndex( String value )
{
  int start = 0, length = value.length();
  while ( start < length )
  {
    int codePoint = value.codePointAt( start );
    if ( !Character.isWhitespace( codePoint ) )
    {
      break;
    }
    start += Character.charCount( codePoint );
  }
  return start;
}

代码示例来源:origin: redisson/redisson

private Constant(String content) {
  Arrays.fill(contains, false);
  StringBuilder sb = new StringBuilder();
  for (int i = 0; i < content.length(); i++) {
    int c = content.codePointAt(i);
    if (c < 128)
      contains[c] = true;
    else
      sb.appendCodePoint(c);
  }
  if (sb.length() > 0) {
    noASCII = true;
    this.content = sb.toString();
  }
}

代码示例来源:origin: redisson/redisson

public static boolean isPrintable(final String data) {
  final int length = data.length();
  for (int offset = 0; offset < length; ) {
    final int codePoint = data.codePointAt(offset);
    if (!isPrintable(codePoint)) {
      return false;
    }
    offset += Character.charCount(codePoint);
  }
  return true;
}

代码示例来源:origin: square/okhttp

/** Returns {@code s} with control characters and non-ASCII characters replaced with '?'. */
private static String toHumanReadableAscii(String s) {
 for (int i = 0, length = s.length(), c; i < length; i += Character.charCount(c)) {
  c = s.codePointAt(i);
  if (c > '\u001f' && c < '\u007f') continue;
  Buffer buffer = new Buffer();
  buffer.writeUtf8(s, 0, i);
  buffer.writeUtf8CodePoint('?');
  for (int j = i + Character.charCount(c); j < length; j += Character.charCount(c)) {
   c = s.codePointAt(j);
   buffer.writeUtf8CodePoint(c > '\u001f' && c < '\u007f' ? c : '?');
  }
  return buffer.readUtf8();
 }
 return s;
}

代码示例来源:origin: square/retrofit

private static String canonicalizeForPath(String input, boolean alreadyEncoded) {
 int codePoint;
 for (int i = 0, limit = input.length(); i < limit; i += Character.charCount(codePoint)) {
  codePoint = input.codePointAt(i);
  if (codePoint < 0x20 || codePoint >= 0x7f
    || PATH_SEGMENT_ALWAYS_ENCODE_SET.indexOf(codePoint) != -1
    || (!alreadyEncoded && (codePoint == '/' || codePoint == '%'))) {
   // Slow path: the character at i requires encoding!
   Buffer out = new Buffer();
   out.writeUtf8(input, 0, i);
   canonicalizeForPath(out, input, i, limit, alreadyEncoded);
   return out.readUtf8();
  }
 }
 // Fast path: no characters required encoding.
 return input;
}

代码示例来源:origin: facebook/litho

private static String getBasePropMatcherName(final MethodParamModel prop, final String suffix) {
 final String name = prop.getName();
 final int fst = Character.toUpperCase(name.codePointAt(0));
 return 'm'
   + String.copyValueOf(Character.toChars(fst))
   + name.substring(name.offsetByCodePoints(0, 1))
   + suffix;
}

代码示例来源:origin: skylot/jadx

public void testComplexIf(String a, int b) {
    if (d == null || (c == 0 && b != -1 && d.length() == 0)) {
      c = a.codePointAt(c);
    } else {
      if (a.hashCode() != 0xCDE) {
        c = f.compareTo(a);
      }
    }
  }
}

代码示例来源:origin: prestodb/presto

/** Returns {@code s} with control characters and non-ASCII characters replaced with '?'. */
public static String toHumanReadableAscii(String s) {
 for (int i = 0, length = s.length(), c; i < length; i += Character.charCount(c)) {
  c = s.codePointAt(i);
  if (c > '\u001f' && c < '\u007f') continue;
  Buffer buffer = new Buffer();
  buffer.writeUtf8(s, 0, i);
  for (int j = i; j < length; j += Character.charCount(c)) {
   c = s.codePointAt(j);
   buffer.writeUtf8CodePoint(c > '\u001f' && c < '\u007f' ? c : '?');
  }
  return buffer.readUtf8();
 }
 return s;
}

代码示例来源:origin: libgdx/libgdx

public int getWidth (String text) {
  if (text == null) throw new IllegalArgumentException("text cannot be null.");
  if (text.length() == 0) return 0;
  char[] chars = text.toCharArray();
  GlyphVector vector = font.layoutGlyphVector(GlyphPage.renderContext, chars, 0, chars.length, Font.LAYOUT_LEFT_TO_RIGHT);
  int width = 0;
  int extraX = 0;
  boolean startNewLine = false;
  for (int glyphIndex = 0, n = vector.getNumGlyphs(); glyphIndex < n; glyphIndex++) {
    int charIndex = vector.getGlyphCharIndex(glyphIndex);
    int codePoint = text.codePointAt(charIndex);
    Rectangle bounds = getGlyphBounds(vector, glyphIndex, codePoint);
    if (startNewLine && codePoint != '\n') extraX = -bounds.x;
    if (glyphIndex > 0) extraX += paddingLeft + paddingRight + paddingAdvanceX;
    width = Math.max(width, bounds.x + extraX + bounds.width);
    if (codePoint == '\n') startNewLine = true;
  }
  return width;
}

代码示例来源:origin: libgdx/libgdx

public int getWidth (String text) {
  if (text == null) throw new IllegalArgumentException("text cannot be null.");
  if (text.length() == 0) return 0;
  char[] chars = text.toCharArray();
  GlyphVector vector = font.layoutGlyphVector(GlyphPage.renderContext, chars, 0, chars.length, Font.LAYOUT_LEFT_TO_RIGHT);
  int width = 0;
  int extraX = 0;
  boolean startNewLine = false;
  for (int glyphIndex = 0, n = vector.getNumGlyphs(); glyphIndex < n; glyphIndex++) {
    int charIndex = vector.getGlyphCharIndex(glyphIndex);
    int codePoint = text.codePointAt(charIndex);
    Rectangle bounds = getGlyphBounds(vector, glyphIndex, codePoint);
    if (startNewLine && codePoint != '\n') extraX = -bounds.x;
    if (glyphIndex > 0) extraX += paddingLeft + paddingRight + paddingAdvanceX;
    width = Math.max(width, bounds.x + extraX + bounds.width);
    if (codePoint == '\n') startNewLine = true;
  }
  return width;
}

代码示例来源:origin: square/okhttp

static void percentDecode(Buffer out, String encoded, int pos, int limit, boolean plusIsSpace) {
 int codePoint;
 for (int i = pos; i < limit; i += Character.charCount(codePoint)) {
  codePoint = encoded.codePointAt(i);
  if (codePoint == '%' && i + 2 < limit) {
   int d1 = decodeHexDigit(encoded.charAt(i + 1));
   int d2 = decodeHexDigit(encoded.charAt(i + 2));
   if (d1 != -1 && d2 != -1) {
    out.writeByte((d1 << 4) + d2);
    i += 2;
    continue;
   }
  } else if (codePoint == '+' && plusIsSpace) {
   out.writeByte(' ');
   continue;
  }
  out.writeUtf8CodePoint(codePoint);
 }
}

代码示例来源:origin: libgdx/libgdx

public int getHeight (String text) {
  if (text == null) throw new IllegalArgumentException("text cannot be null.");
  if (text.length() == 0) return 0;
  char[] chars = text.toCharArray();
  GlyphVector vector = font.layoutGlyphVector(GlyphPage.renderContext, chars, 0, chars.length, Font.LAYOUT_LEFT_TO_RIGHT);
  int lines = 0, height = 0;
  for (int i = 0, n = vector.getNumGlyphs(); i < n; i++) {
    int charIndex = vector.getGlyphCharIndex(i);
    int codePoint = text.codePointAt(charIndex);
    if (codePoint == ' ') continue;
    Rectangle bounds = getGlyphBounds(vector, i, codePoint);
    height = Math.max(height, ascent + bounds.y + bounds.height);
    if (codePoint == '\n') {
      lines++;
      height = 0;
    }
  }
  return lines * getLineHeight() + height;
}

代码示例来源:origin: libgdx/libgdx

public int getHeight (String text) {
  if (text == null) throw new IllegalArgumentException("text cannot be null.");
  if (text.length() == 0) return 0;
  char[] chars = text.toCharArray();
  GlyphVector vector = font.layoutGlyphVector(GlyphPage.renderContext, chars, 0, chars.length, Font.LAYOUT_LEFT_TO_RIGHT);
  int lines = 0, height = 0;
  for (int i = 0, n = vector.getNumGlyphs(); i < n; i++) {
    int charIndex = vector.getGlyphCharIndex(i);
    int codePoint = text.codePointAt(charIndex);
    if (codePoint == ' ') continue;
    Rectangle bounds = getGlyphBounds(vector, i, codePoint);
    height = Math.max(height, ascent + bounds.y + bounds.height);
    if (codePoint == '\n') {
      lines++;
      height = 0;
    }
  }
  return lines * getLineHeight() + height;
}

代码示例来源:origin: libgdx/libgdx

/** Queues the glyphs in the specified text to be loaded. Note that the glyphs are not actually loaded until
 * {@link #loadGlyphs()} is called. */
public void addGlyphs (String text) {
  if (text == null) throw new IllegalArgumentException("text cannot be null.");
  char[] chars = text.toCharArray();
  GlyphVector vector = font.layoutGlyphVector(GlyphPage.renderContext, chars, 0, chars.length, Font.LAYOUT_LEFT_TO_RIGHT);
  for (int i = 0, n = vector.getNumGlyphs(); i < n; i++) {
    int codePoint = text.codePointAt(vector.getGlyphCharIndex(i));
    Rectangle bounds = getGlyphBounds(vector, i, codePoint);
    getGlyph(vector.getGlyphCode(i), codePoint, bounds, vector, i);
  }
}

代码示例来源:origin: libgdx/libgdx

/** Queues the glyphs in the specified text to be loaded. Note that the glyphs are not actually loaded until
 * {@link #loadGlyphs()} is called. */
public void addGlyphs (String text) {
  if (text == null) throw new IllegalArgumentException("text cannot be null.");
  char[] chars = text.toCharArray();
  GlyphVector vector = font.layoutGlyphVector(GlyphPage.renderContext, chars, 0, chars.length, Font.LAYOUT_LEFT_TO_RIGHT);
  for (int i = 0, n = vector.getNumGlyphs(); i < n; i++) {
    int codePoint = text.codePointAt(vector.getGlyphCharIndex(i));
    Rectangle bounds = getGlyphBounds(vector, i, codePoint);
    getGlyph(vector.getGlyphCode(i), codePoint, bounds, vector, i);
  }
}

代码示例来源:origin: square/retrofit

private static void canonicalizeForPath(Buffer out, String input, int pos, int limit,
  boolean alreadyEncoded) {
 Buffer utf8Buffer = null; // Lazily allocated.
 int codePoint;
 for (int i = pos; i < limit; i += Character.charCount(codePoint)) {
  codePoint = input.codePointAt(i);
  if (alreadyEncoded
    && (codePoint == '\t' || codePoint == '\n' || codePoint == '\f' || codePoint == '\r')) {
   // Skip this character.
  } else if (codePoint < 0x20 || codePoint >= 0x7f
    || PATH_SEGMENT_ALWAYS_ENCODE_SET.indexOf(codePoint) != -1
    || (!alreadyEncoded && (codePoint == '/' || codePoint == '%'))) {
   // Percent encode this character.
   if (utf8Buffer == null) {
    utf8Buffer = new Buffer();
   }
   utf8Buffer.writeUtf8CodePoint(codePoint);
   while (!utf8Buffer.exhausted()) {
    int b = utf8Buffer.readByte() & 0xff;
    out.writeByte('%');
    out.writeByte(HEX_DIGITS[(b >> 4) & 0xf]);
    out.writeByte(HEX_DIGITS[b & 0xf]);
   }
  } else {
   // This character doesn't need encoding. Just copy it over.
   out.writeUtf8CodePoint(codePoint);
  }
 }
}

代码示例来源:origin: neo4j/neo4j

@Test
void shouldHandleSingleByteCodePoints()
{
  // Given
  UTF8StringValueBuilder builder = new UTF8StringValueBuilder();
  int codepoint = "$".codePointAt( 0 );
  // When
  builder.addCodePoint( codepoint );
  builder.addCodePoint( codepoint );
  builder.addCodePoint( codepoint );
  // Then
  TextValue textValue = builder.build();
  assertThat( textValue.stringValue(), equalTo("$$$"));
}

代码示例来源:origin: neo4j/neo4j

@Test
void shouldHandleTwoByteCodePoints()
{
  // Given
  UTF8StringValueBuilder builder = new UTF8StringValueBuilder();
  int codepoint = "¢".codePointAt( 0 );
  // When
  builder.addCodePoint( codepoint );
  builder.addCodePoint( codepoint );
  builder.addCodePoint( codepoint );
  // Then
  TextValue textValue = builder.build();
  assertThat( textValue.stringValue(), equalTo("¢¢¢"));
}

相关文章

微信公众号

最新文章

更多