From df298c6046d4a3fd4f8b4ba81845d4623804eb46 Mon Sep 17 00:00:00 2001 From: Robin Gareus Date: Wed, 26 Apr 2023 17:24:05 +0200 Subject: [PATCH] Add API to sanitize UTF-8 strings --- libs/pbd/pbd/utf8_utils.h | 32 ++++++++++++++++++++++++++++++++ libs/pbd/utf8_utils.cc | 33 +++++++++++++++++++++++++++++++++ libs/pbd/wscript | 1 + 3 files changed, 66 insertions(+) create mode 100644 libs/pbd/pbd/utf8_utils.h create mode 100644 libs/pbd/utf8_utils.cc diff --git a/libs/pbd/pbd/utf8_utils.h b/libs/pbd/pbd/utf8_utils.h new file mode 100644 index 0000000000..3aeb9c5733 --- /dev/null +++ b/libs/pbd/pbd/utf8_utils.h @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2023 Robin Gareus + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef _libpbd_utf8_utils_h_ +#define _libpbd_utf8_utils_h_ + +#include + +#include "pbd/libpbd_visibility.h" + +namespace PBD +{ + LIBPBD_API std::string sanitize_utf8 (std::string const&); + +} // namespace PBD + +#endif diff --git a/libs/pbd/utf8_utils.cc b/libs/pbd/utf8_utils.cc new file mode 100644 index 0000000000..fb57debb9d --- /dev/null +++ b/libs/pbd/utf8_utils.cc @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2023 Robin Gareus + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include + +#include "pbd/utf8_utils.h" + +std::string +PBD::sanitize_utf8 (std::string const& s) +{ + std::string rv; + char const* data = s.c_str (); + for (char const *ptr = data, *pend = data; *pend != '\0'; ptr = pend + 1) { + g_utf8_validate (ptr, -1, &pend); + rv.append (ptr, pend); + } + return rv; +} diff --git a/libs/pbd/wscript b/libs/pbd/wscript index 7294ad41f0..33eaba7b9c 100644 --- a/libs/pbd/wscript +++ b/libs/pbd/wscript @@ -89,6 +89,7 @@ libpbd_sources = [ 'tlsf.cc', 'transmitter.cc', 'undo.cc', + 'utf8_utils.cc', 'uuid.cc', 'whitespace.cc', 'xml++.cc',