Commit fff7b807 by ibaker Committed by Oliver Woodman

Replace Util.toUpperInvariant() with Ascii.toUpperCase()

Even when fixed to the US locale (and thus avoiding surprising behaviour
in e.g. Turkish locale with "i" and "I") there are unexpected behaviours
when upper and lower casing non-ASCII characters.

For example it's sometimes not symmetric, e.g.:
"ẞ".toLowerCase() -> "ß"
"ß".toUpperCase() -> "SS"

In all the ExoPlayer usages we are either dealing with known-ASCII
strings (e.g. MIME types) or comparing against ASCII constant strings
anyway, so it seems easier to just use Guava's ASCII-only class in these
cases.

Util.toUpperInvariant() is null-tolerant, while Ascii.toLowercase() is
not. Most usages in this change are clearly non-null. The BandwidthMeter
usages aren't annotated @Nullable, but the current code *would* work if
countryCode was null in both cases. These methods will now throw NPE if
they're passed null.

PiperOrigin-RevId: 368816287
parent 5511bb66
...@@ -761,16 +761,6 @@ public final class Util { ...@@ -761,16 +761,6 @@ public final class Util {
} }
/** /**
* Converts text to upper case using {@link Locale#US}.
*
* @param text The text to convert.
* @return The upper case text, or null if {@code text} is null.
*/
public static @PolyNull String toUpperInvariant(@PolyNull String text) {
return text == null ? text : text.toUpperCase(Locale.US);
}
/**
* Formats a string using {@link Locale#US}. * Formats a string using {@link Locale#US}.
* *
* @see String#format(String, Object...) * @see String#format(String, Object...)
...@@ -2151,11 +2141,11 @@ public final class Util { ...@@ -2151,11 +2141,11 @@ public final class Util {
if (telephonyManager != null) { if (telephonyManager != null) {
String countryCode = telephonyManager.getNetworkCountryIso(); String countryCode = telephonyManager.getNetworkCountryIso();
if (!TextUtils.isEmpty(countryCode)) { if (!TextUtils.isEmpty(countryCode)) {
return toUpperInvariant(countryCode); return Ascii.toUpperCase(countryCode);
} }
} }
} }
return toUpperInvariant(Locale.getDefault().getCountry()); return Ascii.toUpperCase(Locale.getDefault().getCountry());
} }
/** /**
......
...@@ -25,6 +25,7 @@ import com.google.android.exoplayer2.util.Clock; ...@@ -25,6 +25,7 @@ import com.google.android.exoplayer2.util.Clock;
import com.google.android.exoplayer2.util.NetworkTypeObserver; import com.google.android.exoplayer2.util.NetworkTypeObserver;
import com.google.android.exoplayer2.util.SlidingPercentile; import com.google.android.exoplayer2.util.SlidingPercentile;
import com.google.android.exoplayer2.util.Util; import com.google.android.exoplayer2.util.Util;
import com.google.common.base.Ascii;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableListMultimap; import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
...@@ -170,7 +171,7 @@ public final class DefaultBandwidthMeter implements BandwidthMeter, TransferList ...@@ -170,7 +171,7 @@ public final class DefaultBandwidthMeter implements BandwidthMeter, TransferList
*/ */
public Builder setInitialBitrateEstimate(String countryCode) { public Builder setInitialBitrateEstimate(String countryCode) {
initialBitrateEstimates = initialBitrateEstimates =
getInitialBitrateEstimatesForCountry(Util.toUpperInvariant(countryCode)); getInitialBitrateEstimatesForCountry(Ascii.toUpperCase(countryCode));
return this; return this;
} }
......
...@@ -135,7 +135,7 @@ public class HlsMediaPlaylistParserTest { ...@@ -135,7 +135,7 @@ public class HlsMediaPlaylistParserTest {
.isEqualTo("https://priv.example.com/key.php?r=2682"); .isEqualTo("https://priv.example.com/key.php?r=2682");
// 0xA7A == 2682. // 0xA7A == 2682.
assertThat(segment.encryptionIV).isNotNull(); assertThat(segment.encryptionIV).isNotNull();
assertThat(Util.toUpperInvariant(segment.encryptionIV)).isEqualTo("A7A"); assertThat(segment.encryptionIV).ignoringCase().isEqualTo("A7A");
assertThat(segment.byteRangeLength).isEqualTo(51740); assertThat(segment.byteRangeLength).isEqualTo(51740);
assertThat(segment.byteRangeOffset).isEqualTo(2147586650L); assertThat(segment.byteRangeOffset).isEqualTo(2147586650L);
assertThat(segment.url).isEqualTo("https://priv.example.com/fileSequence2682.ts"); assertThat(segment.url).isEqualTo("https://priv.example.com/fileSequence2682.ts");
...@@ -148,7 +148,7 @@ public class HlsMediaPlaylistParserTest { ...@@ -148,7 +148,7 @@ public class HlsMediaPlaylistParserTest {
.isEqualTo("https://priv.example.com/key.php?r=2682"); .isEqualTo("https://priv.example.com/key.php?r=2682");
// 0xA7B == 2683. // 0xA7B == 2683.
assertThat(segment.encryptionIV).isNotNull(); assertThat(segment.encryptionIV).isNotNull();
assertThat(Util.toUpperInvariant(segment.encryptionIV)).isEqualTo("A7B"); assertThat(segment.encryptionIV).ignoringCase().isEqualTo("A7B");
assertThat(segment.byteRangeLength).isEqualTo(C.LENGTH_UNSET); assertThat(segment.byteRangeLength).isEqualTo(C.LENGTH_UNSET);
assertThat(segment.byteRangeOffset).isEqualTo(0); assertThat(segment.byteRangeOffset).isEqualTo(0);
assertThat(segment.url).isEqualTo("https://priv.example.com/fileSequence2683.ts"); assertThat(segment.url).isEqualTo("https://priv.example.com/fileSequence2683.ts");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment