Skip to content

Commit

Permalink
Make String.format usage locale safe (discord-jda#1616)
Browse files Browse the repository at this point in the history
  • Loading branch information
Scarsz authored May 29, 2021
1 parent e44bf71 commit 1dad81d
Show file tree
Hide file tree
Showing 12 changed files with 29 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/main/java/net/dv8tion/jda/api/EmbedBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public MessageEmbed build()
if (isEmpty())
throw new IllegalStateException("Cannot build an empty embed!");
if (description.length() > MessageEmbed.TEXT_MAX_LENGTH)
throw new IllegalStateException(String.format("Description is longer than %d! Please limit your input!", MessageEmbed.TEXT_MAX_LENGTH));
throw new IllegalStateException(Helpers.format("Description is longer than %d! Please limit your input!", MessageEmbed.TEXT_MAX_LENGTH));
if (length() > MessageEmbed.EMBED_MAX_LENGTH_BOT)
throw new IllegalStateException("Cannot build an embed with more than " + MessageEmbed.EMBED_MAX_LENGTH_BOT + " characters!");
final String description = this.description.length() < 1 ? null : this.description.toString();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/dv8tion/jda/api/entities/Activity.java
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ public long getElapsedTime(TemporalUnit unit)
@Override
public String toString()
{
return String.format("RichPresenceTimestamp(%d-%d)", start, end);
return Helpers.format("RichPresenceTimestamp(%d-%d)", start, end);
}

@Override
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/net/dv8tion/jda/api/entities/RichPresence.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package net.dv8tion.jda.api.entities;

import net.dv8tion.jda.internal.utils.Helpers;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.EnumSet;
Expand Down Expand Up @@ -251,7 +253,7 @@ public long getMax()
@Override
public String toString()
{
return String.format("RichPresenceParty(%s | [%d, %d])", id, size, max);
return Helpers.format("RichPresenceParty(%s | [%d, %d])", id, size, max);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package net.dv8tion.jda.api.exceptions;

import net.dv8tion.jda.internal.requests.Route;
import net.dv8tion.jda.internal.utils.Helpers;

/**
* Indicates that we received a {@code 429: Too Many Requests} response
Expand All @@ -33,7 +34,7 @@ public RateLimitedException(Route.CompiledRoute route, long retryAfter)

public RateLimitedException(String route, long retryAfter)
{
super(String.format("The request was ratelimited! Retry-After: %d Route: %s", retryAfter, route));
super(Helpers.format("The request was ratelimited! Retry-After: %d Route: %s", retryAfter, route));
this.rateLimitedRoute = route;
this.retryAfter = retryAfter;
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/net/dv8tion/jda/api/utils/WidgetUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import net.dv8tion.jda.internal.entities.EntityBuilder;
import net.dv8tion.jda.internal.requests.Requester;
import net.dv8tion.jda.internal.utils.Checks;
import net.dv8tion.jda.internal.utils.Helpers;
import net.dv8tion.jda.internal.utils.IOUtil;
import okhttp3.OkHttpClient;
import okhttp3.Request;
Expand Down Expand Up @@ -140,7 +141,7 @@ public static String getPremadeWidgetHtml(@Nonnull String guildId, @Nonnull Widg
Checks.notNull(theme, "WidgetTheme");
Checks.notNegative(width, "Width");
Checks.notNegative(height, "Height");
return String.format(WIDGET_HTML, guildId, theme.name().toLowerCase(), width, height);
return Helpers.format(WIDGET_HTML, guildId, theme.name().toLowerCase(), width, height);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/net/dv8tion/jda/api/utils/data/DataArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import net.dv8tion.jda.api.utils.data.etf.ExTermDecoder;
import net.dv8tion.jda.api.utils.data.etf.ExTermEncoder;
import net.dv8tion.jda.internal.utils.Checks;
import net.dv8tion.jda.internal.utils.Helpers;
import org.jetbrains.annotations.Contract;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -722,7 +723,7 @@ private <T> T get(@Nonnull Class<T> type, int index, @Nullable Function<String,
else if (numberMapper != null && value instanceof Number)
return numberMapper.apply((Number) value);

throw new ParsingException(String.format("Cannot parse value for index %d into type %s: %s instance of %s",
throw new ParsingException(Helpers.format("Cannot parse value for index %d into type %s: %s instance of %s",
index, type.getSimpleName(), value, value.getClass().getSimpleName()));
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/net/dv8tion/jda/api/utils/data/DataObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import net.dv8tion.jda.api.utils.data.etf.ExTermDecoder;
import net.dv8tion.jda.api.utils.data.etf.ExTermEncoder;
import net.dv8tion.jda.internal.utils.Checks;
import net.dv8tion.jda.internal.utils.Helpers;
import org.jetbrains.annotations.Contract;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -769,7 +770,7 @@ private <T> T get(@Nonnull Class<T> type, @Nonnull String key, @Nullable Functio
else if (value instanceof String && stringParse != null)
return stringParse.apply((String) value);

throw new ParsingException(String.format("Cannot parse value for %s into type %s: %s instance of %s",
throw new ParsingException(Helpers.format("Cannot parse value for %s into type %s: %s instance of %s",
key, type.getSimpleName(), value, value.getClass().getSimpleName()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import net.dv8tion.jda.api.utils.data.DataObject;
import net.dv8tion.jda.internal.JDAImpl;
import net.dv8tion.jda.internal.managers.AudioManagerImpl;
import net.dv8tion.jda.internal.utils.Helpers;
import net.dv8tion.jda.internal.utils.IOUtil;
import net.dv8tion.jda.internal.utils.JDALogger;
import org.slf4j.Logger;
Expand Down Expand Up @@ -89,7 +90,7 @@ protected AudioWebSocket(
keepAlivePool = getJDA().getAudioLifeCyclePool();

//Append the Secure Websocket scheme so that our websocket library knows how to connect
wssEndpoint = String.format("wss://%s/?v=%d", endpoint, JDAInfo.AUDIO_GATEWAY_VERSION);
wssEndpoint = Helpers.format("wss://%s/?v=%d", endpoint, JDAInfo.AUDIO_GATEWAY_VERSION);

if (sessionId == null || sessionId.isEmpty())
throw new IllegalArgumentException("Cannot create a voice connection using a null/empty sessionId!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import net.dv8tion.jda.internal.requests.DeferredRestAction;
import net.dv8tion.jda.internal.requests.RestActionImpl;
import net.dv8tion.jda.internal.requests.Route;
import net.dv8tion.jda.internal.utils.Helpers;

import javax.annotation.Nonnull;
import java.util.EnumSet;
Expand Down Expand Up @@ -63,7 +64,7 @@ public String getName()
@Override
public String getDiscriminator()
{
return String.format("%04d", discriminator);
return Helpers.format("%04d", discriminator);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import net.dv8tion.jda.api.requests.Response;
import net.dv8tion.jda.internal.JDAImpl;
import net.dv8tion.jda.internal.requests.ratelimit.BotRateLimiter;
import net.dv8tion.jda.internal.utils.Helpers;
import net.dv8tion.jda.internal.utils.JDALogger;
import net.dv8tion.jda.internal.utils.config.AuthorizationConfig;
import okhttp3.Call;
Expand All @@ -39,6 +40,7 @@
import java.net.UnknownHostException;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Locale;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentMap;
Expand All @@ -47,7 +49,7 @@
public class Requester
{
public static final Logger LOG = JDALogger.getLog(Requester.class);
public static final String DISCORD_API_PREFIX = String.format("https://discord.com/api/v%d/", JDAInfo.DISCORD_REST_VERSION);
public static final String DISCORD_API_PREFIX = Helpers.format("https://discord.com/api/v%d/", JDAInfo.DISCORD_REST_VERSION);
public static final String USER_AGENT = "DiscordBot (" + JDAInfo.GITHUB + ", " + JDAInfo.VERSION + ")";
public static final RequestBody EMPTY_BODY = RequestBody.create(null, new byte[0]);
public static final MediaType MEDIA_TYPE_JSON = MediaType.parse("application/json; charset=utf-8");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@

public class MessageActionImpl extends RestActionImpl<Message> implements MessageAction
{
private static final String CONTENT_TOO_BIG = String.format("A message may not exceed %d characters. Please limit your input!", Message.MAX_CONTENT_LENGTH);
private static final String CONTENT_TOO_BIG = Helpers.format("A message may not exceed %d characters. Please limit your input!", Message.MAX_CONTENT_LENGTH);
protected static EnumSet<Message.MentionType> defaultMentions = EnumSet.allOf(Message.MentionType.class);
protected static boolean defaultMentionRepliedUser = true;
protected static boolean defaultFailOnInvalidReply = false;
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/net/dv8tion/jda/internal/utils/Helpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@

package net.dv8tion.jda.internal.utils;

import java.util.Collection;
import java.util.EnumSet;
import java.util.Iterator;
import java.util.Objects;
import java.util.*;

/**
* This class has major inspiration from <a href="https://commons.apache.org/proper/commons-lang/" target="_blank">Lang 3</a>
Expand All @@ -29,6 +26,13 @@
public final class Helpers
{

// locale-safe String#format

public static String format(String format, Object... args)
{
return String.format(Locale.ROOT, format, args);
}

// ## StringUtils ##

public static boolean isEmpty(final CharSequence seq)
Expand Down

0 comments on commit 1dad81d

Please sign in to comment.