Const correctness improvements:

removed some unnecessary const_casts that remove const.
This commit is contained in:
bubnikv 2020-01-03 16:32:56 +01:00
parent 30f7a2b8e5
commit 9406b50447
13 changed files with 41 additions and 40 deletions

View file

@ -630,39 +630,38 @@ size_t ConfigBase::load_from_gcode_string(const char* str)
return 0;
// Walk line by line in reverse until a non-configuration key appears.
char *data_start = const_cast<char*>(str);
const char *data_start = str;
// boost::nowide::ifstream seems to cook the text data somehow, so less then the 64k of characters may be retrieved.
char *end = data_start + strlen(str);
const char *end = data_start + strlen(str);
size_t num_key_value_pairs = 0;
for (;;) {
// Extract next line.
for (--end; end > data_start && (*end == '\r' || *end == '\n'); --end);
if (end == data_start)
break;
char *start = end;
*(++end) = 0;
const char *start = end ++;
for (; start > data_start && *start != '\r' && *start != '\n'; --start);
if (start == data_start)
break;
// Extracted a line from start to end. Extract the key = value pair.
if (end - (++start) < 10 || start[0] != ';' || start[1] != ' ')
if (end - (++ start) < 10 || start[0] != ';' || start[1] != ' ')
break;
char *key = start + 2;
const char *key = start + 2;
if (!(*key >= 'a' && *key <= 'z') || (*key >= 'A' && *key <= 'Z'))
// A key must start with a letter.
break;
char *sep = strchr(key, '=');
if (sep == nullptr || sep[-1] != ' ' || sep[1] != ' ')
const char *sep = key;
for (; sep != end && *sep != '='; ++ sep) ;
if (sep == end || sep[-1] != ' ' || sep[1] != ' ')
break;
char *value = sep + 2;
const char *value = sep + 2;
if (value > end)
break;
char *key_end = sep - 1;
const char *key_end = sep - 1;
if (key_end - key < 3)
break;
*key_end = 0;
// The key may contain letters, digits and underscores.
for (char *c = key; c != key_end; ++c)
for (const char *c = key; c != key_end; ++ c)
if (!((*c >= 'a' && *c <= 'z') || (*c >= 'A' && *c <= 'Z') || (*c >= '0' && *c <= '9') || *c == '_')) {
key = nullptr;
break;
@ -670,7 +669,7 @@ size_t ConfigBase::load_from_gcode_string(const char* str)
if (key == nullptr)
break;
try {
this->set_deserialize(key, value);
this->set_deserialize(std::string(key, key_end), std::string(value, end));
++num_key_value_pairs;
}
catch (UnknownOptionException & /* e */) {
@ -760,15 +759,15 @@ ConfigOption* DynamicConfig::optptr(const t_config_option_key &opt_key, bool cre
void DynamicConfig::read_cli(const std::vector<std::string> &tokens, t_config_option_keys* extra, t_config_option_keys* keys)
{
std::vector<char*> args;
std::vector<const char*> args;
// push a bogus executable name (argv[0])
args.emplace_back(const_cast<char*>(""));
args.emplace_back("");
for (size_t i = 0; i < tokens.size(); ++ i)
args.emplace_back(const_cast<char *>(tokens[i].c_str()));
this->read_cli(int(args.size()), &args[0], extra, keys);
args.emplace_back(tokens[i].c_str());
this->read_cli(int(args.size()), args.data(), extra, keys);
}
bool DynamicConfig::read_cli(int argc, char** argv, t_config_option_keys* extra, t_config_option_keys* keys)
bool DynamicConfig::read_cli(int argc, const char* const argv[], t_config_option_keys* extra, t_config_option_keys* keys)
{
// cache the CLI option => opt_key mapping
std::map<std::string,std::string> opts;

View file

@ -1779,7 +1779,7 @@ public:
// Command line processing
void read_cli(const std::vector<std::string> &tokens, t_config_option_keys* extra, t_config_option_keys* keys = nullptr);
bool read_cli(int argc, char** argv, t_config_option_keys* extra, t_config_option_keys* keys = nullptr);
bool read_cli(int argc, const char* const argv[], t_config_option_keys* extra, t_config_option_keys* keys = nullptr);
std::map<t_config_option_key, std::unique_ptr<ConfigOption>>::const_iterator cbegin() const { return options.cbegin(); }
std::map<t_config_option_key, std::unique_ptr<ConfigOption>>::const_iterator cend() const { return options.cend(); }

View file

@ -683,7 +683,7 @@ void AMFParserContext::endElement(const char * /* name */)
config->set_deserialize(opt_key, m_value[1]);
} else if (m_path.size() == 3 && m_path[1] == NODE_TYPE_OBJECT && m_object && strcmp(opt_key, "layer_height_profile") == 0) {
// Parse object's layer height profile, a semicolon separated list of floats.
char *p = const_cast<char*>(m_value[1].c_str());
char *p = m_value[1].data();
for (;;) {
char *end = strchr(p, ';');
if (end != nullptr)
@ -698,7 +698,7 @@ void AMFParserContext::endElement(const char * /* name */)
// Parse object's layer height profile, a semicolon separated list of floats.
unsigned char coord_idx = 0;
Eigen::Matrix<float, 5, 1, Eigen::DontAlign> point(Eigen::Matrix<float, 5, 1, Eigen::DontAlign>::Zero());
char *p = const_cast<char*>(m_value[1].c_str());
char *p = m_value[1].data();
for (;;) {
char *end = strchr(p, ';');
if (end != nullptr)
@ -718,7 +718,7 @@ void AMFParserContext::endElement(const char * /* name */)
else if (m_path.size() == 5 && m_path[1] == NODE_TYPE_OBJECT && m_path[3] == NODE_TYPE_RANGE &&
m_object && strcmp(opt_key, "layer_height_range") == 0) {
// Parse object's layer_height_range, a semicolon separated doubles.
char* p = const_cast<char*>(m_value[1].c_str());
char* p = m_value[1].data();
char* end = strchr(p, ';');
*end = 0;
@ -1010,7 +1010,7 @@ bool load_amf(const char* path, DynamicPrintConfig* config, Model* model, bool c
return false;
std::string zip_mask(2, '\0');
file.read(const_cast<char*>(zip_mask.data()), 2);
file.read(zip_mask.data(), 2);
file.close();
return (zip_mask == "PK") ? load_amf_archive(path, config, model, check_version) : load_amf_file(path, config, model);

View file

@ -420,7 +420,7 @@ bool loadvector(FILE *pFile, std::vector<std::string> &v)
if (::fread(&len, sizeof(len), 1, pFile) != 1)
return false;
std::string s(" ", len);
if (::fread(const_cast<char*>(s.c_str()), 1, len, pFile) != len)
if (::fread(s.data(), 1, len, pFile) != len)
return false;
v.push_back(std::move(s));
}
@ -442,7 +442,7 @@ bool loadvectornameidx(FILE *pFile, std::vector<T> &v)
if (::fread(&len, sizeof(len), 1, pFile) != 1)
return false;
v[i].name.assign(" ", len);
if (::fread(const_cast<char*>(v[i].name.c_str()), 1, len, pFile) != len)
if (::fread(v[i].name.data(), 1, len, pFile) != len)
return false;
}
return true;

View file

@ -149,7 +149,7 @@ private:
Semver(semver_t ver) : ver(ver) {}
static semver_t semver_zero() { return { 0, 0, 0, nullptr, nullptr }; }
static char * strdup(const std::string &str) { return ::semver_strdup(const_cast<char*>(str.c_str())); }
static char * strdup(const std::string &str) { return ::semver_strdup(str.data()); }
};

View file

@ -535,7 +535,7 @@ std::string encode_path(const char *src)
// Convert a wide string to a local code page.
int size_needed = ::WideCharToMultiByte(0, 0, wstr_src.data(), (int)wstr_src.size(), nullptr, 0, nullptr, nullptr);
std::string str_dst(size_needed, 0);
::WideCharToMultiByte(0, 0, wstr_src.data(), (int)wstr_src.size(), const_cast<char*>(str_dst.data()), size_needed, nullptr, nullptr);
::WideCharToMultiByte(0, 0, wstr_src.data(), (int)wstr_src.size(), str_dst.data(), size_needed, nullptr, nullptr);
return str_dst;
#else /* WIN32 */
return src;
@ -552,7 +552,7 @@ std::string decode_path(const char *src)
// Convert the string encoded using the local code page to a wide string.
int size_needed = ::MultiByteToWideChar(0, 0, src, len, nullptr, 0);
std::wstring wstr_dst(size_needed, 0);
::MultiByteToWideChar(0, 0, src, len, const_cast<wchar_t*>(wstr_dst.data()), size_needed);
::MultiByteToWideChar(0, 0, src, len, wstr_dst.data(), size_needed);
// Convert a wide string to utf8.
return boost::nowide::narrow(wstr_dst.c_str());
#else /* WIN32 */