litesoft
@ HEAD
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
// This Source Code is in the Public Domain per: http://unlicense.org package org.litesoft.core.util; import org.litesoft.commonfoundation.base.*; import org.litesoft.commonfoundation.charstreams.*; public class UtilsCommon { protected UtilsCommon() { } /** * @param pText !null */ public static void formatToJavaSourceString( StringBuilder pSB, String pText ) { LowLevelEncode( pSB, new CharSourceFromSequence( pText ), true ); } public static String encodeToConstrainedASCII( String pText ) { if ( Currently.isNullOrEmpty( pText ) ) { return pText; } return LowLevelEncode( new StringBuilder(), new CharSourceFromSequence( pText, 0 ), true ).toString(); } public static String decodeFromConstrainedASCII( String pText ) { if ( Currently.isNullOrEmpty( pText ) ) { return pText; } return LowLevelDecode( new StringBuilder(), new CharSourceFromSequence( pText, 0 ), false ).toString(); } private static StringBuilder LowLevelEncode( StringBuilder pSB, CharSource pSource, boolean pDoubleQuoteSpecial ) { for ( int c; -1 != (c = pSource.get()); ) { if ( pDoubleQuoteSpecial && (c == '"') ) { pSB.append( '\\' ); pSB.append( (char) c ); } else { switch ( c ) { case '\\': pSB.append( '\\' ); pSB.append( '\\' ); break; case '\n': pSB.append( '\\' ); pSB.append( 'n' ); break; case '\r': pSB.append( '\\' ); pSB.append( 'r' ); break; case '\t': pSB.append( '\\' ); pSB.append( 't' ); break; default: if ( (' ' <= c) && (c < 127) ) { pSB.append( (char) c ); break; } pSB.append( '\\' ); pSB.append( 'x' ); pSB.append( Hex.toChar( c >> 24 ) ); pSB.append( Hex.toChar( c >> 16 ) ); pSB.append( Hex.toChar( c >> 8 ) ); pSB.append( Hex.toChar( c ) ); break; } } } return pSB; } private static StringBuilder LowLevelDecode( StringBuilder pSB, CharSource pSource, boolean pDoubleQuoteSpecial ) { for ( int c; -1 != (c = pSource.get()); ) { if ( (c < ' ') || (127 <= c) ) { throw new IllegalArgumentException( "Decoding Error: Char[" + pSource.getLastOffset() + "] Not between ' ' & Ascii 126 (inclusive)" ); } if ( c != '\\' ) { pSB.append( (char) c ); continue; } if ( -1 == (c = pSource.get()) ) { throw new IllegalArgumentException( "Decoding Error: No Char after escape \\" ); } if ( pDoubleQuoteSpecial && (c == '"') ) { pSB.append( (char) c ); } else { switch ( c ) { case '\\': pSB.append( (char) c ); break; case 'n': pSB.append( '\n' ); break; case 'r': pSB.append( '\r' ); break; case 't': pSB.append( '\t' ); break; case 'x': int c1 = pSource.get(); int c2 = pSource.get(); int c3 = pSource.get(); int c4 = pSource.get(); if ( c4 == -1 ) { throw new IllegalArgumentException( "Decoding Error: Not enough Chars after \\x" ); } try { int zChar = (Hex.fromCharChecked( (char) c1 ) << 24) + // (Hex.fromCharChecked( (char) c2 ) << 16) + // (Hex.fromCharChecked( (char) c3 ) << 8) + // Hex.fromCharChecked( (char) c4 ); pSB.append( (char) zChar ); } catch ( IllegalArgumentException e ) { throw new IllegalArgumentException( "Decoding Error: After \\x expected 4 hex digits: " + e.getMessage() ); } break; default: throw new IllegalArgumentException( "Decoding Error: Invalid Char '" + c + "' after escape \\" ); } } } return pSB; } } |
Commits for litesoft/trunk/Java/core/Anywhere/src/org/litesoft/core/util/UtilsCommon.java
Revision | Author | Commited | Message |
---|---|---|---|
950 Diff | GeorgeS | Thu 19 Jun, 2014 17:57:04 +0000 | New Lines |
948 Diff | GeorgeS | Sat 07 Jun, 2014 23:42:39 +0000 | Jusefuls Formatter Updated to New Code Format |
947 Diff | GeorgeS | Fri 06 Jun, 2014 23:36:56 +0000 | Correct Spelling of package! |
942 Diff | GeorgeS | Mon 02 Jun, 2014 23:41:46 +0000 | Extracting commonfoundation |
939 Diff | GeorgeS | Mon 02 Jun, 2014 21:30:31 +0000 | Extracting commonfoundation |
823 Diff | GeorgeS | Sun 19 Aug, 2012 16:10:13 +0000 | |
822 Diff | GeorgeS | Sun 19 Aug, 2012 01:03:51 +0000 | |
821 Diff | GeorgeS | Sun 19 Aug, 2012 00:08:41 +0000 | |
819 Diff | GeorgeS | Sat 18 Aug, 2012 18:09:40 +0000 | |
811 | GeorgeS | Sat 18 Aug, 2012 13:45:18 +0000 |