mirror of
https://github.com/Xaymar/obs-StreamFX
synced 2024-11-10 22:05:06 +00:00
util-math: Add SizeFromString method
Converts a String to an std::pair of int64_t (long long), which contain the size or 0 if none could be parsed. Any delimiter except digits(0-9), a minus sign(-) or a plus sign(+) between width and height are allowed. If a plus or minus sign is used as a delimiter, it must immediately be followed by the second size number. This allows for formats such as: 100x100, 100:100, 100p100, 100@100, 100+100 and so on, but not formats such as 100+:100, 100ThisIsSomeReall+yLongText100, etc. The parameter 'allowSquare' also determines what to do when the height parameter is not found. A value of true will have the function return <width,width> instead of <width,0>.
This commit is contained in:
parent
1f60e3d56f
commit
304db23335
2 changed files with 47 additions and 2 deletions
|
@ -20,8 +20,10 @@
|
|||
#include "util-math.h"
|
||||
#include "util-memory.h"
|
||||
#include <malloc.h>
|
||||
#include <stdlib.h>
|
||||
#include <cctype>
|
||||
|
||||
void* util::vec3a::operator new(size_t count){
|
||||
void* util::vec3a::operator new(size_t count) {
|
||||
return _aligned_malloc(count, 16);
|
||||
}
|
||||
|
||||
|
@ -37,7 +39,7 @@ void util::vec3a::operator delete[](void* p) {
|
|||
_aligned_free(p);
|
||||
}
|
||||
|
||||
void* util::vec4a::operator new(size_t count){
|
||||
void* util::vec4a::operator new(size_t count) {
|
||||
return _aligned_malloc(count, 16);
|
||||
}
|
||||
|
||||
|
@ -52,3 +54,42 @@ void util::vec4a::operator delete(void* p) {
|
|||
void util::vec4a::operator delete[](void* p) {
|
||||
_aligned_free(p);
|
||||
}
|
||||
|
||||
std::pair<int64_t, int64_t> util::SizeFromString(std::string text, bool allowSquare) {
|
||||
int64_t width, height;
|
||||
|
||||
const char* begin = text.c_str();
|
||||
const char* end = text.c_str() + text.size() + 1;
|
||||
char* here = const_cast<char*>(end);
|
||||
|
||||
long long res = strtoll(begin, &here, 0);
|
||||
if (errno == ERANGE) {
|
||||
return { 0, 0 };
|
||||
}
|
||||
width = res;
|
||||
|
||||
while (here != end) {
|
||||
if (isdigit(*here) || (*here == '-') || (*here == '+')) {
|
||||
break;
|
||||
}
|
||||
here++;
|
||||
}
|
||||
if (here == end) {
|
||||
// Are we allowed to return a square?
|
||||
if (allowSquare) {
|
||||
// Yes: Return width,width.
|
||||
return { width, width };
|
||||
} else {
|
||||
// No: Return width,0.
|
||||
return { width, 0 };
|
||||
}
|
||||
}
|
||||
|
||||
res = strtoll(here, nullptr, 0);
|
||||
if (errno == ERANGE) {
|
||||
return { width, 0 };
|
||||
}
|
||||
height = res;
|
||||
|
||||
return { width, height };
|
||||
}
|
||||
|
|
|
@ -20,6 +20,8 @@
|
|||
#pragma once
|
||||
#include <math.h>
|
||||
#include <inttypes.h>
|
||||
#include <utility>
|
||||
#include <string>
|
||||
|
||||
// OBS
|
||||
#include <libobs/graphics/vec2.h>
|
||||
|
@ -60,4 +62,6 @@ namespace util {
|
|||
static void vec4a::operator delete(void* p);
|
||||
static void vec4a::operator delete[](void* p);
|
||||
};
|
||||
|
||||
std::pair<int64_t, int64_t> SizeFromString(std::string text, bool allowSquare = true);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue