com.google.common.base.Ascii.equalsIgnoreCase()方法的使用及代码示例

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

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

Ascii.equalsIgnoreCase介绍

[英]Indicates whether the contents of the given character sequences s1 and s2 are equal, ignoring the case of any ASCII alphabetic characters between 'a' and 'z'or 'A' and 'Z' inclusive.

This method is significantly faster than String#equalsIgnoreCase and should be used in preference if at least one of the parameters is known to contain only ASCII characters.

Note however that this method does not always behave identically to expressions such as:

  • string.toUpperCase().equals("UPPER CASE ASCII")
  • string.toLowerCase().equals("lower case ascii")

due to case-folding of some non-ASCII characters (which does not occur in String#equalsIgnoreCase). However in almost all cases that ASCII strings are used, the author probably wanted the behavior provided by this method rather than the subtle and sometimes surprising behavior of toUpperCase() and toLowerCase().
[中]指示给定字符序列s1和s2的内容是否相等,忽略“a”和“z”或“a”和“z”之间的ASCII字母字符的大小写。
此方法比字符串#equalsIgnoreCase快得多,如果已知至少一个参数仅包含ASCII字符,则应优先使用此方法。
但是请注意,此方法的行为并不总是与表达式相同,例如:
*绳子。toUpperCase()。等于(“大写ASCII”)
*绳子。toLowerCase()。等于(“小写ascii”)
由于某些非ASCII字符的大小写折叠(在字符串#equalsIgnoreCase中不出现)。然而,在几乎所有使用ASCII字符串的情况下,作者可能希望使用此方法提供的行为,而不是toUpperCase()和toLowerCase()的微妙且有时令人惊讶的行为。

代码示例

代码示例来源:origin: line/armeria

private static boolean containsValue(String expectedValue, List<String> actualValues) {
  final int numActualValues = actualValues.size();
  for (int i = 0; i < numActualValues; i++) {
    if (Ascii.equalsIgnoreCase(expectedValue, actualValues.get(i))) {
      return true;
    }
  }
  return false;
}

代码示例来源:origin: line/armeria

@Nullable
static MediaType guessFromPath(String path, @Nullable String contentEncoding) {
  if (contentEncoding == null || Ascii.equalsIgnoreCase(contentEncoding, "identity")) {
    return guessFromPath(path);
  }
  requireNonNull(path, "path");
  // If the path is for a precompressed file, it will have an additional extension indicating the
  // encoding, which we don't want to use when determining content type.
  return guessFromPath(path.substring(0, path.lastIndexOf('.')));
}

代码示例来源:origin: google/guava

public void testEqualsIgnoreCase() {
 assertTrue(Ascii.equalsIgnoreCase("", ""));
 assertFalse(Ascii.equalsIgnoreCase("", "x"));
 assertFalse(Ascii.equalsIgnoreCase("x", ""));
 assertTrue(Ascii.equalsIgnoreCase(LOWER, UPPER));
 assertTrue(Ascii.equalsIgnoreCase(UPPER, LOWER));
 // Create new strings here to avoid early-out logic.
 assertTrue(Ascii.equalsIgnoreCase(new String(IGNORED), new String(IGNORED)));
 // Compare to: "\u00c1".equalsIgnoreCase("\u00e1") == true
 assertFalse(Ascii.equalsIgnoreCase("\u00c1", "\u00e1"));
 // Test chars just outside the alphabetic range ('A'-1 vs 'a'-1, 'Z'+1 vs 'z'+1)
 assertFalse(Ascii.equalsIgnoreCase("@", "`"));
 assertFalse(Ascii.equalsIgnoreCase("[", "{"));
}

代码示例来源:origin: line/armeria

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
  boolean handled = false;
  if (msg instanceof HttpResponse) {
    // The server rejected the upgrade request and sent its response in HTTP/1.
    assert upgradeEvt == UPGRADE_REJECTED;
    final String connection = ((HttpResponse) msg).headers().get(HttpHeaderNames.CONNECTION);
    needsToClose = connection != null && Ascii.equalsIgnoreCase("close", connection);
    handled = true;
  }
  if (msg instanceof HttpContent) {
    if (msg instanceof LastHttpContent) {
      // Received the rejecting response completely.
      onUpgradeResponse(ctx, false);
    }
    handled = true;
  }
  if (!handled) {
    ctx.fireChannelRead(msg);
  } else {
    ReferenceCountUtil.release(msg);
  }
}

代码示例来源:origin: line/armeria

@Nullable
@Override
public MediaType contentType() {
  final String contentTypeString = get(HttpHeaderNames.CONTENT_TYPE);
  if (contentTypeString == null) {
    return null;
  }
  final MediaType contentType = this.contentType;
  if (contentType != null && Ascii.equalsIgnoreCase(contentType.toString(), contentTypeString.trim())) {
    return contentType;
  }
  try {
    this.contentType = MediaType.parse(contentTypeString);
    return this.contentType;
  } catch (IllegalArgumentException unused) {
    // Invalid media type
    return null;
  }
}

代码示例来源:origin: google/guava

@GwtIncompatible // String.toUpperCase() has browser semantics
 public void testEqualsIgnoreCaseUnicodeEquivalence() {
  // Note that it's possible in future that the JDK's idea to toUpperCase() or equalsIgnoreCase()
  // may change and break assumptions in this test [*]. This is not a bug in the implementation of
  // Ascii.equalsIgnoreCase(), but it is a signal that its documentation may need updating as
  // regards edge cases.

  // The Unicode point {@code 00df} is the lowercase form of sharp-S (ß), whose uppercase is "SS".
  assertEquals("PASSWORD", "pa\u00dfword".toUpperCase()); // [*]
  assertFalse("pa\u00dfword".equalsIgnoreCase("PASSWORD")); // [*]
  assertFalse(Ascii.equalsIgnoreCase("pa\u00dfword", "PASSWORD"));
 }
}

代码示例来源:origin: line/armeria

private boolean handle100Continue(int id, HttpRequest nettyReq, HttpHeaders nettyHeaders) {
  if (nettyReq.protocolVersion().compareTo(HttpVersion.HTTP_1_1) < 0) {
    // Ignore HTTP/1.0 requests.
    return true;
  }
  final String expectValue = nettyHeaders.get(HttpHeaderNames.EXPECT);
  if (expectValue == null) {
    // No 'expect' header.
    return true;
  }
  // '100-continue' is the only allowed expectation.
  if (!Ascii.equalsIgnoreCase("100-continue", expectValue)) {
    return false;
  }
  // Send a '100 Continue' response.
  writer.writeHeaders(id, 1, CONTINUE_RESPONSE, false);
  // Remove the 'expect' header so that it's handled in a way invisible to a Service.
  nettyHeaders.remove(HttpHeaderNames.EXPECT);
  return true;
}

代码示例来源:origin: com.blackducksoftware.magpie/magpie

/**
 * Test to see if this content type has the specified type and subtype. Comparison is case-insensitive.
 */
public boolean is(CharSequence type, CharSequence subtype) {
  return Ascii.equalsIgnoreCase(this.type, type) && Ascii.equalsIgnoreCase(this.subtype, subtype);
}

代码示例来源:origin: com.proofpoint.platform/http-server

@Override
public String getHeader(String name)
{
  if (equalsIgnoreCase(name, "content-length") || equalsIgnoreCase(name, "content-encoding")) {
    return null;
  }
  return request.getHeader(name);
}

代码示例来源:origin: com.proofpoint.platform/http-server

private void lookAhead()
  {
    do {
      if (!delegate.hasMoreElements()) {
        moreElementsState = MoreElementsState.NO;
        return;
      }
      nextElement = delegate.nextElement();
    } while (equalsIgnoreCase(nextElement, "content-length") || equalsIgnoreCase(nextElement, "content-encoding"));
    moreElementsState = MoreElementsState.YES;
  }
}

代码示例来源:origin: com.blackducksoftware.magpie/magpie

/**
 * Test to see if this content type has the specified suffix.
 */
public boolean hasSuffix(CharSequence suffix) {
  int pos = subtype.length() - suffix.length();
  return pos > 0 && subtype.charAt(pos - 1) == '+'
      && Ascii.equalsIgnoreCase(subtype.subSequence(pos, subtype.length()), suffix);
}

代码示例来源:origin: com.blackducksoftware.magpie/magpie

/**
 * Returns a UUID from a URI.
 */
public static UUID fromUri(URI uri) {
  checkArgument(Ascii.equalsIgnoreCase(uri.getScheme(), "urn"), "expected 'urn' scheme: %s", uri);
  String ssp = uri.getSchemeSpecificPart();
  checkArgument(ssp != null && Ascii.equalsIgnoreCase(ssp.substring(0, 5), "uuid:"), "expected 'uuid' namespace: %s ", uri);
  return fromString(ssp.substring(5));
}

代码示例来源:origin: line/centraldogma

@JsonCreator
public AccessToken(@JsonProperty("token_type") String tokenType,
          @JsonProperty("access_token") String accessToken,
          @JsonProperty("expires_in") long expiresIn,
          @JsonProperty("refresh_token") String refreshToken) {
  requireNonNull(tokenType, "tokenType");
  checkArgument(Ascii.equalsIgnoreCase(tokenType, BEARER),
         "tokenType: %s (expected: %s)", tokenType, BEARER);
  this.accessToken = requireNonNull(accessToken, "accessToken");
  this.expiresIn = expiresIn;
  this.refreshToken = requireNonNull(refreshToken, "refreshToken");
  deadline = Instant.now().plus(expiresIn, ChronoUnit.SECONDS);
}

代码示例来源:origin: com.proofpoint.platform/http-server

@Override
public int getIntHeader(String name)
{
  if (equalsIgnoreCase(name, "content-length")) {
    return -1;
  }
  return request.getIntHeader(name);
}

代码示例来源:origin: com.blackducksoftware.magpie/magpie

/**
 * Returns a UUID from a URI string representation.
 */
public static UUID fromUriString(CharSequence uri) {
  checkArgument(uri.length() == 45, "UUID URN should be exactly 45 characters (was %s): %s", uri.length(), uri);
  checkArgument(Ascii.equalsIgnoreCase("urn:uuid:", uri.subSequence(0, 9)), "expected UUID URN scheme: %s", uri);
  return fromString(uri.subSequence(9, 45));
}

代码示例来源:origin: google/closure-templates

/**
 * Returns {@code true} if the attribute name is static and matches the given name (ignoring
 * case).
 */
public boolean definitelyMatchesAttributeName(String attributeName) {
 return getChild(0) instanceof RawTextNode
   && Ascii.equalsIgnoreCase(attributeName, ((RawTextNode) getChild(0)).getRawText());
}

代码示例来源:origin: com.google.template/soy

/**
 * Returns {@code true} if the attribute name is static and matches the given name (ignoring
 * case).
 */
public boolean definitelyMatchesAttributeName(String attributeName) {
 return getChild(0) instanceof RawTextNode
   && Ascii.equalsIgnoreCase(attributeName, ((RawTextNode) getChild(0)).getRawText());
}

代码示例来源:origin: com.google.guava/guava-tests

@Benchmark boolean equalsIgnoreCaseStringOnly(int reps) {
 // This benchmark has no concept of "noWorkToDo".
 String lhs = testString;
 String rhs = testString.toUpperCase();
 boolean dummy = false;
 for (int i = 0; i < reps; i++) {
  dummy ^= Ascii.equalsIgnoreCase(lhs, rhs);
 }
 return dummy;
}

代码示例来源:origin: com.google.guava/guava-tests

public void testEqualsIgnoreCase() {
 assertTrue(Ascii.equalsIgnoreCase("", ""));
 assertFalse(Ascii.equalsIgnoreCase("", "x"));
 assertFalse(Ascii.equalsIgnoreCase("x", ""));
 assertTrue(Ascii.equalsIgnoreCase(LOWER, UPPER));
 assertTrue(Ascii.equalsIgnoreCase(UPPER, LOWER));
 // Create new strings here to avoid early-out logic.
 assertTrue(Ascii.equalsIgnoreCase(new String(IGNORED), new String(IGNORED)));
 // Compare to: "\u00c1".equalsIgnoreCase("\u00e1") == true
 assertFalse(Ascii.equalsIgnoreCase("\u00c1", "\u00e1"));
 // Test chars just outside the alphabetic range ('A'-1 vs 'a'-1, 'Z'+1 vs 'z'+1)
 assertFalse(Ascii.equalsIgnoreCase("@", "`"));
 assertFalse(Ascii.equalsIgnoreCase("[", "{"));
}

代码示例来源:origin: com.google.guava/guava-tests

@GwtIncompatible // String.toUpperCase() has browser semantics
 public void testEqualsIgnoreCaseUnicodeEquivalence() {
  // Note that it's possible in future that the JDK's idea to toUpperCase() or equalsIgnoreCase()
  // may change and break assumptions in this test [*]. This is not a bug in the implementation of
  // Ascii.equalsIgnoreCase(), but it is a signal that its documentation may need updating as
  // regards edge cases.

  // The Unicode point {@code 00df} is the lowercase form of sharp-S (ß), whose uppercase is "SS".
  assertEquals("PASSWORD", "pa\u00dfword".toUpperCase()); // [*]
  assertFalse("pa\u00dfword".equalsIgnoreCase("PASSWORD"));  // [*]
  assertFalse(Ascii.equalsIgnoreCase("pa\u00dfword", "PASSWORD"));
 }
}

相关文章