Add the golang keywords to the set of identifier checks. · aeron-io/simple-binary-encoding@1e9a3e5 (original) (raw)
`@@ -198,4 +198,97 @@ private static boolean isJavaIdentifier(final String token)
`
198
198
``
199
199
`return true;
`
200
200
` }
`
``
201
+
``
202
`+
private static final Set GOLANG_KEYWORDS = new HashSet<>(
`
``
203
`+
Arrays.asList(new String[]
`
``
204
`+
{
`
``
205
`+
/* https://golang.org/ref/spec#Keywords */
`
``
206
`+
"break", "default", "func", "interface", "select",
`
``
207
`+
"case", "defer", "go", "map", "struct",
`
``
208
`+
"chan", "else", "goto", "package", "switch",
`
``
209
`+
"const", "fallthrough", "if", "range", "type",
`
``
210
`+
"continue", "for", "import", "return", "var",
`
``
211
+
``
212
`+
/* https://golang.org/ref/spec#Predeclared_identifiers */
`
``
213
`+
/* types */
`
``
214
`+
"bool", "byte", "complex64", "complex128", "error", "float32", "float64",
`
``
215
`+
"int", "int8", "int16", "int32", "int64", "rune", "string",
`
``
216
`+
"uint", "uint8", "uint16", "uint32", "uint64", "uintptr",
`
``
217
`+
/* constants */
`
``
218
`+
"true", "false", "iota",
`
``
219
`+
/* zero value */
`
``
220
`+
"nil",
`
``
221
`+
/* functions */
`
``
222
`+
"append", "cap", "close", "complex", "copy", "delete", "imag", "len",
`
``
223
`+
"make", "new", "panic", "print", "println", "real", "recover"
`
``
224
`+
}));
`
``
225
+
``
226
`+
/**
`
``
227
`+
- "Check" value for validity of usage as a golang identifier. From:
`
``
228
`+
`
``
229
`+
`
``
230
`+
- identifier = letter { letter | unicode_digit }
`
``
231
`+
- letter = unicode_letter | "_" .
`
``
232
`+
`
``
233
`+
- unicode_letter and unicode_digit are defined in section 4.5 of the the unicode
`
``
234
`+
- standard at http://www.unicode.org/versions/Unicode8.0.0/ and
`
``
235
`+
- the Java Character and Digit functions are unicode friendly
`
``
236
`+
`
``
237
`+
- @param value to check
`
``
238
`+
- @return true for validity as a golang name. false if not.
`
``
239
`+
*/
`
``
240
`+
public static boolean isSbeGolangName(final String value)
`
``
241
`+
{
`
``
242
`+
if (possibleGolangKeyword(value))
`
``
243
`+
{
`
``
244
`+
if (isGolangKeyword(value))
`
``
245
`+
{
`
``
246
`+
return false;
`
``
247
`+
}
`
``
248
`+
}
`
``
249
`+
else
`
``
250
`+
{
`
``
251
`+
return false;
`
``
252
`+
}
`
``
253
+
``
254
`+
return true;
`
``
255
`+
}
`
``
256
+
``
257
`+
public static boolean isGolangKeyword(final String token)
`
``
258
`+
{
`
``
259
`+
return GOLANG_KEYWORDS.contains(token);
`
``
260
`+
}
`
``
261
+
``
262
`+
private static boolean possibleGolangKeyword(final String value)
`
``
263
`+
{
`
``
264
`+
for (int i = 0, size = value.length(); i < size; i++)
`
``
265
`+
{
`
``
266
`+
final char c = value.charAt(i);
`
``
267
+
``
268
`+
if (i == 0 && isSbeGolangIdentifierStart(c))
`
``
269
`+
{
`
``
270
`+
continue;
`
``
271
`+
}
`
``
272
+
``
273
`+
if (isSbeGolangIdentifierPart(c))
`
``
274
`+
{
`
``
275
`+
continue;
`
``
276
`+
}
`
``
277
+
``
278
`+
return false;
`
``
279
`+
}
`
``
280
+
``
281
`+
return true;
`
``
282
`+
}
`
``
283
+
``
284
`+
private static boolean isSbeGolangIdentifierStart(final char c)
`
``
285
`+
{
`
``
286
`+
return Character.isLetter(c) || c == '_';
`
``
287
`+
}
`
``
288
+
``
289
`+
private static boolean isSbeGolangIdentifierPart(final char c)
`
``
290
`+
{
`
``
291
`+
return Character.isLetterOrDigit(c) || c == '_';
`
``
292
`+
}
`
``
293
+
201
294
`}
`