001/* 002 * Copyright (c) 2012-2018 Institut National des Sciences Appliquées de Lyon (INSA Lyon) and others 003 * 004 * This program and the accompanying materials are made available under the 005 * terms of the Eclipse Public License 2.0 which is available at 006 * http://www.eclipse.org/legal/epl-2.0. 007 * 008 * SPDX-License-Identifier: EPL-2.0 009 */ 010 011package org.eclipse.golo.compiler.utils; 012 013public final class StringUnescaping { 014 015 private StringUnescaping() { 016 //utility class 017 } 018 019 public static String unescape(String str) { 020 StringBuilder sb = new StringBuilder(str.length()); 021 for (int i = 0; i < str.length(); i++) { 022 char ch = str.charAt(i); 023 if (ch == '\\') { 024 char nextChar = (i == str.length() - 1) ? '\\' : str.charAt(i + 1); 025 switch (nextChar) { 026 case 'u': 027 ch = (char) Integer.parseInt(str.substring(i + 2, i + 6), 16); 028 i += 4; 029 break; 030 case '\\': 031 ch = '\\'; 032 break; 033 case 'b': 034 ch = '\b'; 035 break; 036 case 'f': 037 ch = '\f'; 038 break; 039 case 'n': 040 ch = '\n'; 041 break; 042 case 'r': 043 ch = '\r'; 044 break; 045 case 't': 046 ch = '\t'; 047 break; 048 case '\"': 049 ch = '\"'; 050 break; 051 case '\'': 052 ch = '\''; 053 break; 054 default: 055 // not a special char, do nothing 056 break; 057 } 058 i++; 059 } 060 sb.append(ch); 061 } 062 return sb.toString(); 063 } 064}