mirror of
https://github.com/KingDuckZ/kamokan.git
synced 2025-08-03 12:50:02 +00:00
Basic check to tell if a string looks like a possibly valid number.
This commit is contained in:
parent
51810fa2f2
commit
54d50cf6bc
2 changed files with 36 additions and 1 deletions
34
src/tawashi_implem/num_conv.hpp
Normal file
34
src/tawashi_implem/num_conv.hpp
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
/* Copyright 2017, Michele Santullo
|
||||||
|
* This file is part of "tawashi".
|
||||||
|
*
|
||||||
|
* "tawashi" 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 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* "tawashi" 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 "tawashi". If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <ciso646>
|
||||||
|
#include <duckhandy/lexical_cast.hpp>
|
||||||
|
|
||||||
|
namespace tawashi {
|
||||||
|
template <typename T>
|
||||||
|
inline bool seems_valid_number (const boost::string_ref& parValue) {
|
||||||
|
const std::size_t skip_sign = (sprout::is_signed<T>::value and not parValue.empty() and parValue[0] == '-' ? 1 : 0);
|
||||||
|
for (std::size_t z = skip_sign; z < parValue.size(); ++z) {
|
||||||
|
const char c = parValue[z];
|
||||||
|
if (c < '0' or c > '9')
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return parValue.size() - skip_sign <= dhandy::tags::dec<T>::count_digits_bt(sprout::numeric_limits<T>::max());
|
||||||
|
}
|
||||||
|
} //namespace tawashi
|
|
@ -24,6 +24,7 @@
|
||||||
#include "pathname/pathname.hpp"
|
#include "pathname/pathname.hpp"
|
||||||
#include "list_highlight_langs.hpp"
|
#include "list_highlight_langs.hpp"
|
||||||
#include "cgi_env.hpp"
|
#include "cgi_env.hpp"
|
||||||
|
#include "num_conv.hpp"
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
@ -126,7 +127,7 @@ namespace tawashi {
|
||||||
oss << ':' << port;
|
oss << ':' << port;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (not host_port.empty()) {
|
else if (not host_port.empty() and seems_valid_number<uint16_t>(host_port)) {
|
||||||
oss << ':' << host_port;
|
oss << ':' << host_port;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue