From 3e37c06b84e9f07a0f5586af352cefa4ce2ee8f3 Mon Sep 17 00:00:00 2001 From: Bill Ferguson Date: Sat, 13 Jul 2024 14:44:01 -0400 Subject: [PATCH] lib/dtutils/string - Added check to determine if string needs to be sanitized. Check looks for unprintable characters and spaces. --- lib/dtutils/string.lua | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/lib/dtutils/string.lua b/lib/dtutils/string.lua index 926c667..d64a95a 100644 --- a/lib/dtutils/string.lua +++ b/lib/dtutils/string.lua @@ -298,16 +298,32 @@ local function _sanitize_windows(str) end end +local function _should_be_sanitized(str) + local old_log_level = log.log_level() + local result = false + log.log_level(dtutils_string.log_level) + if string.match(str, "[^%g]") then + result = true + end + log.log_level(old_log_level) + return result +end + function dtutils_string.sanitize(str) local old_log_level = log.log_level() + local sanitized_str = nil log.log_level(dtutils_string.log_level) - if dt.configuration.running_os == "windows" then - log.log_level(old_log_level) - return _sanitize_windows(str) + if _should_be_sanitized(str) then + if dt.configuration.running_os == "windows" then + sanitized_str = _sanitize_windows(str) + else + sanitized_str = _sanitize_posix(str) + end else - log.log_level(old_log_level) - return _sanitize_posix(str) + sanitized_str = str end + log.log_level(old_log_level) + return sanitized_str end dtutils_string.libdoc.functions["sanitize_lua"] = {