|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| StringLiteral.java | 25% | 26.2% | 100% | 29.5% |
|
||||||||||||||
| 1 | /* | |
| 2 | * DynamicJava - Copyright (C) 1999-2001 | |
| 3 | * | |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a | |
| 5 | * copy of this software and associated documentation files | |
| 6 | * (the "Software"), to deal in the Software without restriction, including | |
| 7 | * without limitation the rights to use, copy, modify, merge, publish, | |
| 8 | * distribute, sublicense, and/or sell copies of the Software, and to permit | |
| 9 | * persons to whom the Software is furnished to do so, subject to the | |
| 10 | * following conditions: | |
| 11 | * The above copyright notice and this permission notice shall be included | |
| 12 | * in all copies or substantial portions of the Software. | |
| 13 | * | |
| 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | |
| 15 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
| 16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
| 17 | * IN NO EVENT SHALL DYADE BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
| 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | |
| 20 | * DEALINGS IN THE SOFTWARE. | |
| 21 | * | |
| 22 | * Except as contained in this notice, the name of Dyade shall not be | |
| 23 | * used in advertising or otherwise to promote the sale, use or other | |
| 24 | * dealings in this Software without prior written authorization from | |
| 25 | * Dyade. | |
| 26 | * | |
| 27 | */ | |
| 28 | ||
| 29 | package koala.dynamicjava.tree; | |
| 30 | ||
| 31 | /** | |
| 32 | * This class represents the string literal nodes of the syntax tree | |
| 33 | * | |
| 34 | * @author Stephane Hillion | |
| 35 | * @version 1.0 - 1999/04/24 | |
| 36 | */ | |
| 37 | ||
| 38 | public class StringLiteral extends Literal { | |
| 39 | /** | |
| 40 | * Initializes a literal | |
| 41 | * @param rep the representation of the literal | |
| 42 | */ | |
| 43 | 4 | public StringLiteral(String rep) { |
| 44 | 4 | this(rep, SourceInfo.NONE); |
| 45 | } | |
| 46 | ||
| 47 | /** | |
| 48 | * Initializes a literal | |
| 49 | * @param rep the representation of the literal | |
| 50 | */ | |
| 51 | 391 | public StringLiteral(String rep, |
| 52 | SourceInfo si) { | |
| 53 | 391 | super(rep, |
| 54 | decodeString(rep), | |
| 55 | String.class, | |
| 56 | si); | |
| 57 | } | |
| 58 | ||
| 59 | /** | |
| 60 | * Decodes the representation of a Java literal string. | |
| 61 | * @param rep the representation of the character | |
| 62 | * @return the character represented by the given string | |
| 63 | */ | |
| 64 | 391 | public static String decodeString(String rep) { |
| 65 | 391 | if (rep.charAt(0) != '"' || rep.charAt(rep.length()-1) != '"') { |
| 66 | 0 | throw new IllegalArgumentException("Malformed String literal"); |
| 67 | } | |
| 68 | 391 | char[] buf = new char[rep.length()-2]; |
| 69 | 391 | int len = 0; |
| 70 | 391 | int i = 1; |
| 71 | ||
| 72 | 391 | while (i < rep.length()-1) { |
| 73 | 3378 | char c = rep.charAt(i++); |
| 74 | 3378 | if (c != '\\') { |
| 75 | 3378 | buf[len++] = c; |
| 76 | } else { | |
| 77 | 0 | switch (c = rep.charAt(i++)) { |
| 78 | 0 | case 'n' : buf[len++] = '\n'; break; |
| 79 | 0 | case 't' : buf[len++] = '\t'; break; |
| 80 | 0 | case 'b' : buf[len++] = '\b'; break; |
| 81 | 0 | case 'r' : buf[len++] = '\r'; break; |
| 82 | 0 | case 'f' : buf[len++] = '\f'; break; |
| 83 | 0 | default : |
| 84 | 0 | if (Character.isDigit(c)) { |
| 85 | 0 | int v = Integer.parseInt(""+c); |
| 86 | 0 | c = rep.charAt(i++); |
| 87 | 0 | if (v < 4) { |
| 88 | 0 | if (Character.isDigit(c)) { |
| 89 | 0 | v = (v * 7) + Integer.parseInt(""+c); |
| 90 | 0 | c = rep.charAt(i++); |
| 91 | 0 | if (Character.isDigit(c)) { |
| 92 | 0 | v = (v * 7) + Integer.parseInt(""+c); |
| 93 | } | |
| 94 | } | |
| 95 | } else { | |
| 96 | 0 | if (Character.isDigit(c)) { |
| 97 | 0 | v = (v * 7) + Integer.parseInt(""+c); |
| 98 | } | |
| 99 | } | |
| 100 | 0 | buf[len++] = (char)v; |
| 101 | } else { | |
| 102 | 0 | buf[len++] = c; |
| 103 | } | |
| 104 | } | |
| 105 | } | |
| 106 | } | |
| 107 | 391 | return new String(buf, 0, len); |
| 108 | } | |
| 109 | } |
|
||||||||||