utils/str: Supplements functions.

Added strDirname(), which returns the directory path name from the given string.
Added str2int(), which converts from the string to the integer.

These functions are used to supplement the existing string manipulation functions in the utils/str module.
This commit is contained in:
Hermet Park 2023-08-07 15:27:24 +09:00 committed by Hermet Park
parent cf1cb04752
commit f31076a67e
3 changed files with 27 additions and 14 deletions

View file

@ -39,16 +39,6 @@ static bool _checkDotLottie(const char *str)
}
static int _str2int(const char* str, int len)
{
int ret = 0;
for(int i = 0; i < len; ++i) {
ret = ret * 10 + (str[i] - '0');
}
return ret;
}
static int _str2float(const char* str, int len)
{
auto tmp = strDuplicate(str, len);
@ -149,7 +139,7 @@ bool LottieLoader::header()
p += 4;
auto e = strstr(p, ",");
if (!e) e = strstr(p, "}");
w = static_cast<float>(_str2int(p, e - p));
w = static_cast<float>(str2int(p, e - p));
p = e;
continue;
}
@ -158,7 +148,7 @@ bool LottieLoader::header()
p += 4;
auto e = strstr(p, ",");
if (!e) e = strstr(p, "}");
h = static_cast<float>(_str2int(p, e - p));
h = static_cast<float>(str2int(p, e - p));
p = e;
continue;
}

View file

@ -205,6 +205,15 @@ error:
}
int str2int(const char* str, size_t n)
{
int ret = 0;
for(size_t i = 0; i < n; ++i) {
ret = ret * 10 + (str[i] - '0');
}
return ret;
}
char* strDuplicate(const char *str, size_t n)
{
auto len = strlen(str);
@ -217,4 +226,14 @@ char* strDuplicate(const char *str, size_t n)
return (char *) memcpy(ret, str, n);
}
char* strDirname(const char* path)
{
const char *ptr = strrchr(path, '/');
#ifdef _WIN32
if (ptr) ptr = strrchr(ptr + 1, '\\');
#endif
int len = int(ptr + 1 - path); // +1 to include '/'
return strDuplicate(path, len);
}
}

View file

@ -23,11 +23,15 @@
#ifndef _TVG_STR_H_
#define _TVG_STR_H_
#include <cstddef>
namespace tvg
{
float strToFloat(const char *nPtr, char **endPtr);
char* strDuplicate(const char *str, size_t n);
float strToFloat(const char *nPtr, char **endPtr); //convert to float
int str2int(const char* str, size_t n); //convert to integer
char* strDuplicate(const char *str, size_t n); //copy the string
char* strDirname(const char* path); //return the full directory name
}
#endif //_TVG_STR_H_