1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2026-02-28 13:29:43 +00:00
Files
CC-Tweaked/src/main/java/dan200/computercraft/shared/util/StringUtil.java
SquidDev 6ccffe9742 Refactor the filesystem and HTTP code
- Move the encoding/decoding from the Filesystem implementation to the
   individual handles.
 - Move each handle into an core.apis.handles package from the main fs
   API.
 - Move the HTTP response to inherit from these handles.
 - Allow binary handles' read function to accept a number, specifying
   how many characters to read - these will be returned as a Lua string.
 - Add readAll to binary handles
 - Allow binary handles' write function to accept a string which is
   decoded into the individual bytes.
 - Add "binary" argument to http.request and friends in order to return
   a binary handle.
 - Ensure file handles are open when reading from/writing to them.
 - Return the error message when opening a file fails.
2017-05-13 22:47:28 +01:00

58 lines
1.5 KiB
Java

package dan200.computercraft.shared.util;
public class StringUtil
{
public static String normaliseLabel( String label )
{
if( label == null ) return null;
int length = Math.min( 32, label.length() );
StringBuilder builder = new StringBuilder( length );
for (int i = 0; i < length; i++)
{
char c = label.charAt( i );
if( (c >= ' ' && c <= '~') || (c >= 161 && c <= 172) || (c >= 174 && c <= 255) )
{
builder.append( c );
}
else
{
builder.append( '?' );
}
}
return builder.toString();
}
/**
* Translates a Stat name
*/
@SuppressWarnings("deprecation")
public static String translateToLocal( String key )
{
return net.minecraft.util.text.translation.I18n.translateToLocal( key );
}
/**
* Translates a Stat name with format args
*/
@SuppressWarnings("deprecation")
public static String translateToLocalFormatted( String key, Object... format )
{
return net.minecraft.util.text.translation.I18n.translateToLocalFormatted( key, format );
}
public static byte[] encodeString( String string )
{
byte[] chars = new byte[ string.length() ];
for( int i = 0; i < chars.length; ++i )
{
char c = string.charAt( i );
chars[ i ] = c < 256 ? (byte) c : 63;
}
return chars;
}
}