From ed1deacce8c035b65f6d99e815ca46be90962880 Mon Sep 17 00:00:00 2001 From: Henning Stahlke Date: Fri, 5 Jun 2020 18:16:48 +0200 Subject: [PATCH] props.nas: add isValidPropName and makeValidPropName; add props.nut --- Nasal/props.nas | 32 +++++++++++++++++++++++++++++++- Nasal/props.nut | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 Nasal/props.nut diff --git a/Nasal/props.nas b/Nasal/props.nas index 652976de8..cfe1b1bda 100644 --- a/Nasal/props.nas +++ b/Nasal/props.nas @@ -112,7 +112,37 @@ var Node = { me.getPath()~" "~me.getType()); return 0; } - } + }, + + # checks if a string can be used as a prop name + # returns 0 (invalid) or 1 (valid) + # as string operations are expensive, use this only when necessary, + # especially do not use in update loops + isValidPropName: func(s) { + if (!size(s)) return 0; + var _ispropspecial = func(c) { c == `_` or c == `.` or c == `-`; } + var _ispropname = func(c) string.isalnum(c) or _ispropspecial(c); + if (!string.isalpha(s[0]) and s[0] != `_`) return 0; + for (var i=1; i < size(s); i += 1) { + if (!_ispropname(s[i])) return 0; + } + return 1; + }, + + # replaces any invalid char by "_" + # as string operations are expensive, use this only when necessary, + # especially do not use in update loops + makeValidPropName: func(s) { + if (!size(s)) return nil; + var _ispropspecial = func(c) { c == `_` or c == `.` or c == `-`; } + var _ispropname = func(c) string.isalnum(c) or _ispropspecial(c); + var rv = ""; + rv ~= (!string.isalpha(s[0])) ? "_" : chr(s[0]); + for (var i=1; i < size(s); i += 1) { + rv ~= (_ispropname(s[i])) ? chr(s[i]) : "_"; + } + return rv; + }, }; ## diff --git a/Nasal/props.nut b/Nasal/props.nut new file mode 100644 index 000000000..e644a51e9 --- /dev/null +++ b/Nasal/props.nut @@ -0,0 +1,43 @@ +#------------------------------------------------------------------------------- +# SPDX-License-Identifier: GPL-2.0-or-later +#------------------------------------------------------------------------------- +# props.Node.nut - Nasal Unit Test for props.Node.nas +# created: 06/2020 +# Copyright (C) 2020 by Henning Stahlke +#------------------------------------------------------------------------------- + +var setUp = func { + print("setUp "~caller(0)[2]~" "); +}; + +var tearDown = func { + +}; + +var test_isValidPropName = func() { + # test valid names + unitTest.assert(props.Node.isValidPropName("abc123") == 1, "isValidPropName()"); + unitTest.assert(props.Node.isValidPropName("_abc123") == 1, "isValidPropName()"); + unitTest.assert(props.Node.isValidPropName("_1a") == 1, "isValidPropName()"); + unitTest.assert(props.Node.isValidPropName("_1a.-") == 1, "isValidPropName()"); + # test invalid names + unitTest.assert(props.Node.isValidPropName("1a") == 0, "isValidPropName()"); + unitTest.assert(props.Node.isValidPropName("abä") == 0, "isValidPropName()"); + unitTest.assert(props.Node.isValidPropName("foo:bar") == 0, "isValidPropName()"); + unitTest.assert(props.Node.isValidPropName("foo