Java Type Coerce coerceToEntrySize(String s)

Here you can find the source of coerceToEntrySize(String s)

Description

Coerces a String into a Doom-standard 8-character format.

License

Open Source License

Parameter

Parameter Description
s the input string.

Return

a formatted string.

Declaration

public static String coerceToEntrySize(String s) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2009-2014 Black Rook Software
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
 ******************************************************************************/

public class Main {
    /** The size of a Wad entry name in bytes. */
    public static final int ENTRY_NAME_SIZE = 8;

    /**//from   w ww  . j  a va  2 s .com
     * Coerces a String into a Doom-standard 8-character format.
     * Stops at first non-alphanumeric, non-whitespace, non-square-bracket, non-underscore character.
     * Carats (^) are converted to backslashes. 
     * <ul>
     * <li>"Music File.wav" would become "MUSIC"</li>
     * <li>"d_runnin.mus" would become "D_RUNNIN"</li>
     * <li>"vile^1" would become "VILE\1"</li>
     * </ul>
     * @param s the input string.
     * @return   a formatted string.
     */
    public static String coerceToEntrySize(String s) {
        return coerceToEntry(s).trim();
    }

    /**
     * Coerces a String into a Doom-standard 8-character format.
     * Stops at first non-alphanumeric, non-whitespace, non-square-bracket, non-underscore character.
     * Carats (^) are converted to backslashes. 
     * <ul>
     * <li>"Music File.wav" would become "MUSIC" with three null characters.</li>
     * <li>"d_runnin.mus" would become "D_RUNNIN"</li>
     * <li>"vile^1" would become "VILE\1" with two null characters.</li>
     * </ul>
     * @param s the input string.
     * @return   a formatted string WITH TRAILING NULLS.
     */
    public static String coerceToEntry(String s) {
        char[] out = new char[ENTRY_NAME_SIZE];
        char[] sc = s.trim().toUpperCase().toCharArray();
        int i = 0, c = 0;
        while (c < sc.length && validEntryChar(sc[c])
                && i < ENTRY_NAME_SIZE) {
            if (sc[c] == '^')
                sc[c] = '\\';
            out[i++] = sc[c];
            c++;
        }
        return new String(out);
    }

    private static boolean validEntryChar(char ch) {
        return ch == '^' || ch == '[' || ch == ']' || ch == '\\'
                || ch == '_' || (ch >= 'A' && ch <= 'Z')
                || (ch >= '0' && ch <= '9') || ch == '-';
    }
}

Related

  1. coerceBool(Object val)
  2. coerceIntoComboId(Long entityId)
  3. coerceIntoEntityId(Long comboId)
  4. coerceTagValueInt(String tag, Object val)
  5. coerceToDouble(Object o)
  6. coerceToString(Object object)
  7. coerceToType(Object objectIn, Class clazz)
  8. coerceTypes(Class clazz, Object value)
  9. coerceTypes(Class clazz, Object value)