diff --git a/README.adoc b/README.adoc index 0a170fa..5673ad1 100644 --- a/README.adoc +++ b/README.adoc @@ -13190,6 +13190,8 @@ ERROR: package host-nodejs installs executables without proper RPATH Examples: +* String +** link:rootfs_overlay/lkmc/nodejs/alphanumeric.js[]: https://stackoverflow.com/questions/4444477/how-to-tell-if-a-string-contains-a-certain-character-in-javascript/58359106#58359106 * `process` ** link:rootfs_overlay/lkmc/nodejs/command_line_arguments.js[] * `fs` diff --git a/rootfs_overlay/lkmc/nodejs/alphanumeric.js b/rootfs_overlay/lkmc/nodejs/alphanumeric.js new file mode 100755 index 0000000..a628e85 --- /dev/null +++ b/rootfs_overlay/lkmc/nodejs/alphanumeric.js @@ -0,0 +1,50 @@ +#!/usr/bin/env node + +// https://cirosantilli.com/linux-kernel-module-cheat#node-js + +const assert = require('assert'); + +const char_is_alphanumeric = function(c) { + let code = c.codePointAt(0); + return ( + // 0-9 + (code > 47 && code < 58) || + // A-Z + (code > 64 && code < 91) || + // a-z + (code > 96 && code < 123) + ) +} + +const is_alphanumeric = function (str) { + for (let c of str) { + if (!char_is_alphanumeric(c)) { + return false; + } + } + return true; +}; + +// Arbitrarily defined as alphanumeric or '-' or '_'. +const is_almost_alphanumeric = function (str) { + for (let c of str) { + if ( + !char_is_alphanumeric(c) && + !is_almost_alphanumeric.almost_chars.has(c) + ) { + return false; + } + } + return true; +}; +is_almost_alphanumeric.almost_chars = new Set(['-', '_']); + +assert( is_alphanumeric('aB0')); +assert(!is_alphanumeric('aB0_-')); +assert(!is_alphanumeric('aB0_-*')); +assert(!is_alphanumeric('你好')); + +assert( is_almost_alphanumeric('aB0')); +assert( is_almost_alphanumeric('aB0_-')); +assert(!is_almost_alphanumeric('aB0_-*')); +assert(!is_almost_alphanumeric('你好'));