strncmp replaced

This commit is contained in:
Mikulas Florek 2015-11-12 17:59:49 +01:00
parent 4d37279542
commit 7ce4a8ddc2
7 changed files with 28 additions and 16 deletions

View file

@ -2357,7 +2357,7 @@ public:
bool isRelativePath(const char* path) override
{
return strncmp(m_base_path.c_str(), path, m_base_path.length()) == 0;
return compareStringN(m_base_path.c_str(), path, m_base_path.length()) == 0;
}
@ -2368,7 +2368,7 @@ public:
char tmp[MAX_PATH_LENGTH];
Lumix::PathUtils::normalize(source, tmp, sizeof(tmp));
if (strncmp(m_base_path.c_str(), tmp, m_base_path.length()) == 0)
if (compareStringN(m_base_path.c_str(), tmp, m_base_path.length()) == 0)
{
const char* rel_path_start = tmp + m_base_path.length();
if (rel_path_start[0] == '/')

View file

@ -49,9 +49,7 @@ public:
m_files[id] = file;
char path[MAX_PATH_LENGTH];
if (strncmp(m_buffer.data(),
m_base_path.c_str(),
m_base_path.length()) != 0)
if (compareStringN(m_buffer.data(), m_base_path.c_str(), m_base_path.length()) != 0)
{
copyString(path, m_base_path.c_str());
catString(path, m_buffer.data());

View file

@ -2,7 +2,7 @@
#include "lumix.h"
//#include <new>
#include <new>
namespace Lumix

View file

@ -262,17 +262,16 @@ void JsonSerializer::serializeArrayItem(bool value)
bool JsonSerializer::isNextBoolean() const
{
if (m_is_string_token) return false;
if (m_token_size == 4 && strncmp(m_token, "true", 4) == 0) return true;
if (m_token_size == 5 && strncmp(m_token, "false", 5) == 0) return true;
if (m_token_size == 4 && compareStringN(m_token, "true", 4) == 0) return true;
if (m_token_size == 5 && compareStringN(m_token, "false", 5) == 0) return true;
return false;
}
void JsonSerializer::deserialize(bool& value, bool default_value)
{
value = !m_is_string_token
? m_token_size == 4 && (strncmp(m_token, "true", 4) == 0)
: default_value;
value = !m_is_string_token ? m_token_size == 4 && (compareStringN(m_token, "true", 4) == 0)
: default_value;
deserializeToken();
}
@ -552,7 +551,7 @@ void JsonSerializer::deserializeArrayItem(bool& value, bool default_value)
else
{
value =
m_token_size == 4 && strncmp("true", m_token, m_token_size) == 0;
m_token_size == 4 && compareStringN("true", m_token, m_token_size) == 0;
}
deserializeToken();
}
@ -565,7 +564,7 @@ void JsonSerializer::deserialize(const char* label,
deserializeLabel(label);
if (!m_is_string_token)
{
value = m_token_size == 4 && strncmp("true", m_token, 4) == 0;
value = m_token_size == 4 && compareStringN("true", m_token, 4) == 0;
}
else
{
@ -740,7 +739,7 @@ void JsonSerializer::deserializeLabel(const char* label)
<< "\", expected string.";
deserializeToken();
}
if (strncmp(label, m_token, m_token_size) != 0)
if (compareStringN(label, m_token, m_token_size) != 0)
{
error().log() << "Unexpected label \""
<< string(m_token, m_token_size, m_allocator)

View file

@ -11,6 +11,20 @@ static char makeLowercase(char c)
}
int compareStringN(const char* lhs, const char* rhs, int length)
{
while(*lhs && *lhs == *rhs && length)
{
++lhs;
++rhs;
--length;
}
if(length == 0) return 0;
return *lhs - *rhs;
}
int compareString(const char* lhs, const char* rhs)
{
while(*lhs && *lhs == *rhs)

View file

@ -35,6 +35,7 @@ LUMIX_ENGINE_API char* trimmed(char* str);
LUMIX_ENGINE_API bool startsWith(const char* str, const char* prefix);
LUMIX_ENGINE_API int stringLength(const char* str);
LUMIX_ENGINE_API int compareString(const char* lhs, const char* rhs);
LUMIX_ENGINE_API int compareStringN(const char* lhs, const char* rhs, int length);
LUMIX_ENGINE_API void copyMemory(void* dest, const void* src, size_t count);

View file

@ -1184,7 +1184,7 @@ struct ConvertTask : public Lumix::MT::Task
}
++last;
if (last < mesh_name + 4) FLT_MAX;
if (strncmp(last - 4, "_LOD", 4) != 0) return FLT_MAX;
if (Lumix::compareStringN(last - 4, "_LOD", 4) != 0) return FLT_MAX;
const char* end_of_factor = last - 4;
const char* begin_factor = end_of_factor - 1;
if (begin_factor <= mesh_name) return FLT_MAX;
@ -1218,7 +1218,7 @@ struct ConvertTask : public Lumix::MT::Task
}
++last;
if (last < mesh_name + 4) return -1;
if (strncmp(last - 4, "_LOD", 4) != 0) return -1;
if (Lumix::compareStringN(last - 4, "_LOD", 4) != 0) return -1;
int lod;
Lumix::fromCString(last, len - int(last - mesh_name), &lod);