Skip to content

Commit

Permalink
throw error when meet non ascii (#2229)
Browse files Browse the repository at this point in the history
* throw error when meet non ascii

* check ascii for config strings.
  • Loading branch information
guolinke committed Jul 18, 2019
1 parent 0d02499 commit 0d59859
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
5 changes: 5 additions & 0 deletions include/LightGBM/dataset.h
Expand Up @@ -8,6 +8,7 @@
#include <LightGBM/config.h>
#include <LightGBM/feature_group.h>
#include <LightGBM/meta.h>
#include <LightGBM/utils/common.h>
#include <LightGBM/utils/openmp_wrapper.h>
#include <LightGBM/utils/random.h>
#include <LightGBM/utils/text_reader.h>
Expand Down Expand Up @@ -555,6 +556,10 @@ class Dataset {
// replace ' ' in feature_names with '_'
bool spaceInFeatureName = false;
for (auto& feature_name : feature_names_) {
// check ascii
if (!Common::CheckASCII(feature_name)) {
Log::Fatal("Do not support non-ascii characters in feature name.");
}
if (feature_name.find(' ') != std::string::npos) {
spaceInFeatureName = true;
std::replace(feature_name.begin(), feature_name.end(), ' ', '_');
Expand Down
9 changes: 9 additions & 0 deletions include/LightGBM/utils/common.h
Expand Up @@ -895,6 +895,15 @@ static T SafeLog(T x) {
}
}

inline bool CheckASCII(const std::string& s) {
for (auto c : s) {
if (static_cast<unsigned char>(c) > 127) {
return false;
}
}
return true;
}

} // namespace Common

} // namespace LightGBM
Expand Down
3 changes: 3 additions & 0 deletions src/io/config.cpp
Expand Up @@ -17,6 +17,9 @@ void Config::KV2Map(std::unordered_map<std::string, std::string>& params, const
if (tmp_strs.size() == 2) {
std::string key = Common::RemoveQuotationSymbol(Common::Trim(tmp_strs[0]));
std::string value = Common::RemoveQuotationSymbol(Common::Trim(tmp_strs[1]));
if (!Common::CheckASCII(key) || !Common::CheckASCII(value)) {
Log::Fatal("Do not support non-ascii characters in config.");
}
if (key.size() > 0) {
auto value_search = params.find(key);
if (value_search == params.end()) { // not set
Expand Down

0 comments on commit 0d59859

Please sign in to comment.