Package: StringVdmType

StringVdmType

nameinstructionbranchcomplexitylinemethod
StringVdmType(String)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
escapeString(String)
M: 70 C: 0
0%
M: 12 C: 0
0%
M: 7 C: 0
0%
M: 18 C: 0
0%
M: 1 C: 0
0%
formatValue(IVdmValue)
M: 10 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
isEscapeNeeded(String, boolean)
M: 23 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
isQuoted(String)
M: 31 C: 0
0%
M: 10 C: 0
0%
M: 6 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
isString()
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * #%~
3: * org.overture.ide.debug
4: * %%
5: * Copyright (C) 2008 - 2014 Overture
6: * %%
7: * This program is free software: you can redistribute it and/or modify
8: * it under the terms of the GNU General Public License as
9: * published by the Free Software Foundation, either version 3 of the
10: * License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public
18: * License along with this program. If not, see
19: * <http://www.gnu.org/licenses/gpl-3.0.html>.
20: * #~%
21: */
22: package org.overture.ide.debug.core.model;
23:
24: public class StringVdmType extends AtomicVdmType
25: {
26:         public StringVdmType(String name)
27:         {
28:                 super(name);
29:         }
30:
31:         public boolean isString()
32:         {
33:                 return true;
34:         }
35:
36:         public String formatValue(IVdmValue value)
37:         {
38:                 String string = value.getRawValue();
39:
40:•                if (string == null)
41:                 {
42:                         return null;
43:                 }
44:                 return escapeString(string);
45:         }
46:
47:         private static String escapeString(String string)
48:         {
49:                 final boolean alreadyQuoted = isQuoted(string);
50:                 final boolean escapeNeed = isEscapeNeeded(string, alreadyQuoted);
51:•                if (!escapeNeed)
52:                 {
53:•                        if (alreadyQuoted)
54:                         {
55:                                 return string;
56:                         } else
57:                         {
58:                                 return '"' + string + '"';
59:                         }
60:                 }
61:                 final StringBuffer escaped = new StringBuffer(string.length() + 8);
62:•                if (!alreadyQuoted)
63:                 {
64:                         escaped.append('"');
65:                 }
66:•                for (int i = 0; i < string.length(); i++)
67:                 {
68:                         char c = string.charAt(i);
69:•                        switch (c)
70:                         {
71:                                 case '"':
72:                                         escaped.append("\\\""); //$NON-NLS-1$
73:                                         break;
74:                                 default:
75:                                         escaped.append(c);
76:                                         break;
77:                         }
78:                 }
79:•                if (!alreadyQuoted)
80:                 {
81:                         escaped.append('"');
82:                 }
83:                 return escaped.toString();
84:         }
85:
86:         private static boolean isQuoted(String string)
87:         {
88:•                if (string.length() >= 2)
89:                 {
90:                         final char firstChar = string.charAt(0);
91:                         final char lastChar = string.charAt(string.length() - 1);
92:•                        if (firstChar == '\'' && lastChar == '\'' || firstChar == '"'
93:•                                        && lastChar == '"')
94:                         {
95:                                 return true;
96:                         }
97:                 }
98:                 return false;
99:         }
100:
101:         private static boolean isEscapeNeeded(String string, boolean isQuoted)
102:         {
103:                 int i = 0, len = string.length();
104:•                if (isQuoted)
105:                 {
106:                         ++i;
107:                         --len;
108:                 }
109:•                for (; i < len; ++i)
110:                 {
111:•                        if (string.charAt(i) == '"')
112:                         {
113:                                 return true;
114:                         }
115:                 }
116:                 return false;
117:         }
118: }