mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-20 23:52:40 +08:00
fix coding style (#3366)
This commit is contained in:
+4
-9
@@ -24,23 +24,18 @@ Checks: >
|
||||
modernize-*,
|
||||
misc-*,
|
||||
portability-*,
|
||||
cppcoreguidelines-*,
|
||||
google-explicit-constructor,
|
||||
-modernize-use-trailing-return-type,
|
||||
-fuchsia-trailing-return,
|
||||
-readability-implicit-bool-conversion,
|
||||
-readability-implicit-bool-cast,
|
||||
-cppcoreguidelines-pro-type-vararg,
|
||||
-cppcoreguidelines-pro-type-static-cast-downcast,
|
||||
-readability-function-cognitive-complexity,
|
||||
-cppcoreguidelines-pro-bounds-pointer-arithmetic,
|
||||
-cppcoreguidelines-pro-bounds-array-to-pointer-decay,
|
||||
-misc-non-private-member-variables-in-classes,
|
||||
-readability-uppercase-literal-suffix,
|
||||
-cppcoreguidelines-no-malloc,
|
||||
-cppcoreguidelines-owning-memory,
|
||||
-cppcoreguidelines-non-private-member-variables-in-classes,
|
||||
-readability-use-anyofallof
|
||||
-readability-use-anyofallof,
|
||||
-readability-magic-numbers,
|
||||
-modernize-pass-by-value,
|
||||
-modernize-concat-nested-namespaces
|
||||
|
||||
CheckOptions:
|
||||
################## readability identifier naming ################################
|
||||
|
||||
@@ -31,10 +31,8 @@
|
||||
namespace cc {
|
||||
|
||||
AutoreleasePool::AutoreleasePool()
|
||||
: _name("")
|
||||
#if defined(CC_DEBUG) && (CC_DEBUG > 0)
|
||||
,
|
||||
_isClearing(false)
|
||||
: _isClearing(false)
|
||||
#endif
|
||||
{
|
||||
_managedObjectArray.reserve(150);
|
||||
@@ -79,8 +77,9 @@ void AutoreleasePool::clear() {
|
||||
|
||||
bool AutoreleasePool::contains(Ref *object) const {
|
||||
for (const auto &obj : _managedObjectArray) {
|
||||
if (obj == object)
|
||||
if (obj == object) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -100,20 +99,18 @@ void AutoreleasePool::dump() {
|
||||
//
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
PoolManager *PoolManager::s_singleInstance = nullptr;
|
||||
PoolManager *PoolManager::_singleInstance = nullptr;
|
||||
|
||||
PoolManager *PoolManager::getInstance() {
|
||||
if (s_singleInstance == nullptr) {
|
||||
s_singleInstance = new (std::nothrow) PoolManager();
|
||||
// Add the first auto release pool
|
||||
new (std::nothrow) AutoreleasePool("autorelease pool");
|
||||
if (_singleInstance == nullptr) {
|
||||
_singleInstance = new (std::nothrow) PoolManager();
|
||||
}
|
||||
return s_singleInstance;
|
||||
return _singleInstance;
|
||||
}
|
||||
|
||||
void PoolManager::destroyInstance() {
|
||||
delete s_singleInstance;
|
||||
s_singleInstance = nullptr;
|
||||
delete _singleInstance;
|
||||
_singleInstance = nullptr;
|
||||
}
|
||||
|
||||
PoolManager::PoolManager() {
|
||||
@@ -136,8 +133,9 @@ AutoreleasePool *PoolManager::getCurrentPool() const {
|
||||
|
||||
bool PoolManager::isObjectInPools(Ref *obj) const {
|
||||
for (const auto &pool : _releasePoolStack) {
|
||||
if (pool->contains(obj))
|
||||
if (pool->contains(obj)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -25,46 +25,32 @@
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef __AUTORELEASEPOOL_H__
|
||||
#define __AUTORELEASEPOOL_H__
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include "base/Ref.h"
|
||||
|
||||
/**
|
||||
* @addtogroup base
|
||||
* @{
|
||||
*/
|
||||
namespace cc {
|
||||
|
||||
/**
|
||||
* A pool for managing autorelease objects.
|
||||
* @js NA
|
||||
*/
|
||||
class CC_DLL AutoreleasePool {
|
||||
public:
|
||||
/**
|
||||
* @warning Don't create an autorelease pool in heap, create it in stack.
|
||||
* @js NA
|
||||
* @lua NA
|
||||
*/
|
||||
AutoreleasePool();
|
||||
|
||||
/**
|
||||
* Create an autorelease pool with specific name. This name is useful for debugging.
|
||||
* @warning Don't create an autorelease pool in heap, create it in stack.
|
||||
* @js NA
|
||||
* @lua NA
|
||||
*
|
||||
* @param name The name of created autorelease pool.
|
||||
*/
|
||||
AutoreleasePool(const std::string &name);
|
||||
explicit AutoreleasePool(const std::string &name);
|
||||
|
||||
/**
|
||||
* @js NA
|
||||
* @lua NA
|
||||
*/
|
||||
~AutoreleasePool();
|
||||
|
||||
/**
|
||||
@@ -75,8 +61,6 @@ public:
|
||||
* the same times as it was added.
|
||||
*
|
||||
* @param object The object to be added into the autorelease pool.
|
||||
* @js NA
|
||||
* @lua NA
|
||||
*/
|
||||
void addObject(Ref *object);
|
||||
|
||||
@@ -84,9 +68,6 @@ public:
|
||||
* Clear the autorelease pool.
|
||||
*
|
||||
* It will invoke each element's `release()` function.
|
||||
*
|
||||
* @js NA
|
||||
* @lua NA
|
||||
*/
|
||||
void clear();
|
||||
|
||||
@@ -95,9 +76,6 @@ public:
|
||||
* Whether the autorelease pool is doing `clear` operation.
|
||||
*
|
||||
* @return True if autorelease pool is clearing, false if not.
|
||||
*
|
||||
* @js NA
|
||||
* @lua NA
|
||||
*/
|
||||
bool isClearing() const { return _isClearing; };
|
||||
#endif
|
||||
@@ -107,8 +85,6 @@ public:
|
||||
*
|
||||
* @param object The object to be checked.
|
||||
* @return True if the autorelease pool contains the object, false if not
|
||||
* @js NA
|
||||
* @lua NA
|
||||
*/
|
||||
bool contains(Ref *object) const;
|
||||
|
||||
@@ -117,9 +93,6 @@ public:
|
||||
*
|
||||
* The result will look like:
|
||||
* Object pointer address object id reference count
|
||||
*
|
||||
* @js NA
|
||||
* @lua NA
|
||||
*/
|
||||
void dump();
|
||||
|
||||
@@ -144,12 +117,6 @@ private:
|
||||
#endif
|
||||
};
|
||||
|
||||
// end of base group
|
||||
/** @} */
|
||||
|
||||
/**
|
||||
* @cond
|
||||
*/
|
||||
class CC_DLL PoolManager {
|
||||
public:
|
||||
static PoolManager *getInstance();
|
||||
@@ -173,14 +140,9 @@ private:
|
||||
void push(AutoreleasePool *pool);
|
||||
void pop();
|
||||
|
||||
static PoolManager *s_singleInstance;
|
||||
static PoolManager *_singleInstance;
|
||||
|
||||
std::vector<AutoreleasePool *> _releasePoolStack;
|
||||
};
|
||||
/**
|
||||
* @endcond
|
||||
*/
|
||||
|
||||
} // namespace cc
|
||||
|
||||
#endif //__AUTORELEASEPOOL_H__
|
||||
|
||||
@@ -27,13 +27,7 @@
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef __CCCONFIG_H__
|
||||
#define __CCCONFIG_H__
|
||||
|
||||
/**
|
||||
* @file
|
||||
* cocos2d (cc) configuration file.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
// disable module if you didn't need it, this will reduce package size
|
||||
|
||||
@@ -117,5 +111,3 @@
|
||||
#ifndef CC_ENABLE_CACHE_JSB_FUNC_RESULT
|
||||
#define CC_ENABLE_CACHE_JSB_FUNC_RESULT 1
|
||||
#endif
|
||||
|
||||
#endif // __CCCONFIG_H__
|
||||
|
||||
@@ -23,19 +23,18 @@
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef CC_CORE_CORE_STD_H_
|
||||
#define CC_CORE_CORE_STD_H_
|
||||
#pragma once
|
||||
|
||||
#include "base/Macros.h"
|
||||
|
||||
// STD including
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <wchar.h>
|
||||
#include <math.h>
|
||||
#include <float.h>
|
||||
#include <assert.h>
|
||||
#include <time.h>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cwchar>
|
||||
#include <cmath>
|
||||
#include <cfloat>
|
||||
#include <cassert>
|
||||
#include <ctime>
|
||||
#include <limits>
|
||||
|
||||
// STL including
|
||||
@@ -70,5 +69,3 @@
|
||||
#define CC_JOB_SYSTEM_TBB 2
|
||||
|
||||
#define CC_JOB_SYSTEM CC_JOB_SYSTEM_TASKFLOW
|
||||
|
||||
#endif // CC_CORE_CORE_STD_H_
|
||||
|
||||
@@ -25,13 +25,12 @@
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <cstdint>
|
||||
|
||||
#ifndef __ASTC_H__
|
||||
#define __ASTC_H__
|
||||
#pragma once
|
||||
|
||||
typedef unsigned char astc_byte;
|
||||
typedef unsigned int astc_uint32;
|
||||
using astc_byte = unsigned char;
|
||||
using astc_uint32 = unsigned int;
|
||||
|
||||
// Size of a ASTC header
|
||||
|
||||
@@ -50,5 +49,3 @@ int astcGetWidth(const astc_byte *pHeader);
|
||||
// Read the image height from a ASTC header
|
||||
|
||||
int astcGetHeight(const astc_byte *pHeader);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -25,116 +25,128 @@
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "base/base64.h"
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace cc {
|
||||
|
||||
unsigned char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
std::string alphabet{"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"};
|
||||
|
||||
int _base64Decode(const unsigned char *input, unsigned int input_len, unsigned char *output, unsigned int *output_len) {
|
||||
static char inalphabet[256], decoder[256];
|
||||
int i, bits, c = 0, char_count, errors = 0;
|
||||
unsigned int input_idx = 0;
|
||||
unsigned int output_idx = 0;
|
||||
int doBase64Decode(const unsigned char *input, unsigned int inputLen, unsigned char *output, unsigned int *outputLen) {
|
||||
static std::vector<char> inalphabet(256);
|
||||
static std::vector<char> decoder(256);
|
||||
int i;
|
||||
int bits;
|
||||
int c = 0;
|
||||
int charCount;
|
||||
int errors = 0;
|
||||
unsigned int inputIdx = 0;
|
||||
unsigned int outputIdx = 0;
|
||||
|
||||
for (i = (sizeof alphabet) - 1; i >= 0; i--) {
|
||||
inalphabet[alphabet[i]] = 1;
|
||||
decoder[alphabet[i]] = i;
|
||||
}
|
||||
|
||||
char_count = 0;
|
||||
charCount = 0;
|
||||
bits = 0;
|
||||
for (input_idx = 0; input_idx < input_len; input_idx++) {
|
||||
c = input[input_idx];
|
||||
if (c == '=')
|
||||
for (inputIdx = 0; inputIdx < inputLen; inputIdx++) {
|
||||
c = input[inputIdx];
|
||||
if (c == '=') {
|
||||
break;
|
||||
if (c > 255 || !inalphabet[c])
|
||||
}
|
||||
|
||||
if (c > 255 || !inalphabet[c]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
bits += decoder[c];
|
||||
char_count++;
|
||||
if (char_count == 4) {
|
||||
output[output_idx++] = (bits >> 16);
|
||||
output[output_idx++] = ((bits >> 8) & 0xff);
|
||||
output[output_idx++] = (bits & 0xff);
|
||||
++charCount;
|
||||
if (charCount == 4) {
|
||||
output[outputIdx++] = (bits >> 16);
|
||||
output[outputIdx++] = ((bits >> 8) & 0xff);
|
||||
output[outputIdx++] = (bits & 0xff);
|
||||
bits = 0;
|
||||
char_count = 0;
|
||||
charCount = 0;
|
||||
} else {
|
||||
bits <<= 6;
|
||||
}
|
||||
}
|
||||
|
||||
if (c == '=') {
|
||||
switch (char_count) {
|
||||
switch (charCount) {
|
||||
case 1:
|
||||
#if (CC_PLATFORM != CC_PLATFORM_BADA)
|
||||
fprintf(stderr, "base64Decode: encoding incomplete: at least 2 bits missing");
|
||||
#endif
|
||||
errors++;
|
||||
++errors;
|
||||
break;
|
||||
case 2:
|
||||
output[output_idx++] = (bits >> 10);
|
||||
output[outputIdx++] = (bits >> 10);
|
||||
break;
|
||||
case 3:
|
||||
output[output_idx++] = (bits >> 16);
|
||||
output[output_idx++] = ((bits >> 8) & 0xff);
|
||||
output[outputIdx++] = (bits >> 16);
|
||||
output[outputIdx++] = ((bits >> 8) & 0xff);
|
||||
break;
|
||||
}
|
||||
} else if (input_idx < input_len) {
|
||||
if (char_count) {
|
||||
} else if (inputIdx < inputLen) {
|
||||
if (charCount) {
|
||||
#if (CC_PLATFORM != CC_PLATFORM_BADA)
|
||||
fprintf(stderr, "base64 encoding incomplete: at least %d bits truncated",
|
||||
((4 - char_count) * 6));
|
||||
((4 - charCount) * 6));
|
||||
#endif
|
||||
errors++;
|
||||
++errors;
|
||||
}
|
||||
}
|
||||
|
||||
*output_len = output_idx;
|
||||
*outputLen = outputIdx;
|
||||
return errors;
|
||||
}
|
||||
|
||||
void _base64Encode(const unsigned char *input, unsigned int input_len, char *output) {
|
||||
unsigned int char_count;
|
||||
void doBase64Encode(const unsigned char *input, unsigned int inputLen, char *output) {
|
||||
unsigned int charCount;
|
||||
unsigned int bits;
|
||||
unsigned int input_idx = 0;
|
||||
unsigned int output_idx = 0;
|
||||
unsigned int inputIdx = 0;
|
||||
unsigned int outputIdx = 0;
|
||||
|
||||
char_count = 0;
|
||||
charCount = 0;
|
||||
bits = 0;
|
||||
for (input_idx = 0; input_idx < input_len; input_idx++) {
|
||||
bits |= input[input_idx];
|
||||
for (inputIdx = 0; inputIdx < inputLen; inputIdx++) {
|
||||
bits |= input[inputIdx];
|
||||
|
||||
char_count++;
|
||||
if (char_count == 3) {
|
||||
output[output_idx++] = alphabet[(bits >> 18) & 0x3f];
|
||||
output[output_idx++] = alphabet[(bits >> 12) & 0x3f];
|
||||
output[output_idx++] = alphabet[(bits >> 6) & 0x3f];
|
||||
output[output_idx++] = alphabet[bits & 0x3f];
|
||||
charCount++;
|
||||
if (charCount == 3) {
|
||||
output[outputIdx++] = alphabet[(bits >> 18) & 0x3f];
|
||||
output[outputIdx++] = alphabet[(bits >> 12) & 0x3f];
|
||||
output[outputIdx++] = alphabet[(bits >> 6) & 0x3f];
|
||||
output[outputIdx++] = alphabet[bits & 0x3f];
|
||||
bits = 0;
|
||||
char_count = 0;
|
||||
charCount = 0;
|
||||
} else {
|
||||
bits <<= 8;
|
||||
}
|
||||
}
|
||||
|
||||
if (char_count) {
|
||||
if (char_count == 1) {
|
||||
if (charCount) {
|
||||
if (charCount == 1) {
|
||||
bits <<= 8;
|
||||
}
|
||||
|
||||
output[output_idx++] = alphabet[(bits >> 18) & 0x3f];
|
||||
output[output_idx++] = alphabet[(bits >> 12) & 0x3f];
|
||||
if (char_count > 1) {
|
||||
output[output_idx++] = alphabet[(bits >> 6) & 0x3f];
|
||||
output[outputIdx++] = alphabet[(bits >> 18) & 0x3f];
|
||||
output[outputIdx++] = alphabet[(bits >> 12) & 0x3f];
|
||||
if (charCount > 1) {
|
||||
output[outputIdx++] = alphabet[(bits >> 6) & 0x3f];
|
||||
} else {
|
||||
output[output_idx++] = '=';
|
||||
output[outputIdx++] = '=';
|
||||
}
|
||||
output[output_idx++] = '=';
|
||||
output[outputIdx++] = '=';
|
||||
}
|
||||
|
||||
output[output_idx++] = 0;
|
||||
output[outputIdx++] = 0;
|
||||
}
|
||||
|
||||
int base64Decode(const unsigned char *in, unsigned int inLength, unsigned char **out) {
|
||||
@@ -143,7 +155,7 @@ int base64Decode(const unsigned char *in, unsigned int inLength, unsigned char *
|
||||
//should be enough to store 6-bit buffers in 8-bit buffers
|
||||
*out = (unsigned char *)malloc(inLength / 4 * 3 + 1);
|
||||
if (*out) {
|
||||
int ret = _base64Decode(in, inLength, *out, &outLength);
|
||||
int ret = doBase64Decode(in, inLength, *out, &outLength);
|
||||
|
||||
if (ret > 0) {
|
||||
free(*out);
|
||||
@@ -160,7 +172,7 @@ int base64Encode(const unsigned char *in, unsigned int inLength, char **out) {
|
||||
//should be enough to store 8-bit buffers in 6-bit buffers
|
||||
*out = (char *)malloc(outLength + 1);
|
||||
if (*out) {
|
||||
_base64Encode(in, inLength, *out);
|
||||
doBase64Encode(in, inLength, *out);
|
||||
}
|
||||
return outLength;
|
||||
}
|
||||
|
||||
@@ -25,9 +25,7 @@
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef __SUPPORT_BASE64_H__
|
||||
#define __SUPPORT_BASE64_H__
|
||||
/// @cond DO_NOT_SHOW
|
||||
#pragma once
|
||||
|
||||
#include "base/Macros.h"
|
||||
|
||||
@@ -37,17 +35,11 @@ extern "C" {
|
||||
|
||||
namespace cc {
|
||||
|
||||
/** @file
|
||||
base64 helper functions
|
||||
*/
|
||||
|
||||
/**
|
||||
* Decodes a 64base encoded memory. The decoded memory is
|
||||
* expected to be freed by the caller by calling `free()`
|
||||
*
|
||||
* @returns the length of the out buffer
|
||||
*
|
||||
@since v0.8.1
|
||||
*/
|
||||
int CC_DLL base64Decode(const unsigned char *in, unsigned int inLength, unsigned char **out);
|
||||
|
||||
@@ -57,7 +49,6 @@ int CC_DLL base64Decode(const unsigned char *in, unsigned int inLength, unsigned
|
||||
*
|
||||
* @returns the length of the out buffer
|
||||
*
|
||||
@since v2.1.4
|
||||
*/
|
||||
int CC_DLL base64Encode(const unsigned char *in, unsigned int inLength, char **out);
|
||||
|
||||
@@ -66,6 +57,3 @@ int CC_DLL base64Encode(const unsigned char *in, unsigned int inLength, char **o
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
/// @endcond
|
||||
#endif // __SUPPORT_BASE64_H__
|
||||
|
||||
@@ -59,11 +59,11 @@ namespace CSSColorParser {
|
||||
|
||||
// http://www.w3.org/TR/css3-color/
|
||||
struct NamedColor {
|
||||
const char *const name;
|
||||
const Color color;
|
||||
const char * name;
|
||||
Color color;
|
||||
};
|
||||
|
||||
const std::vector<NamedColor> namedColors = {
|
||||
const std::vector<NamedColor> NAMED_COLORS = {
|
||||
{"transparent", {0, 0, 0, 0}},
|
||||
{"aliceblue", {240, 248, 255, 1}},
|
||||
{"antiquewhite", {250, 235, 215, 1}},
|
||||
@@ -214,13 +214,13 @@ const std::vector<NamedColor> namedColors = {
|
||||
{"yellowgreen", {154, 205, 50, 1}}};
|
||||
|
||||
template <typename T>
|
||||
uint8_t clamp_css_byte(T i) { // Clamp to integer 0 .. 255.
|
||||
uint8_t clampCssByte(T i) { // Clamp to integer 0 .. 255.
|
||||
i = static_cast<T>(::round(i)); // Seems to be what Chrome does (vs truncation).
|
||||
return static_cast<uint8_t>(i < 0 ? 0 : i > 255 ? 255 : i);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
float clamp_css_float(T f) { // Clamp to float 0.0 .. 1.0.
|
||||
float clampCssFloat(T f) { // Clamp to float 0.0 .. 1.0.
|
||||
return f < 0 ? 0 : f > 1 ? 1 : float(f);
|
||||
}
|
||||
|
||||
@@ -232,23 +232,21 @@ int64_t parseInt(const std::string &str, uint8_t base = 10) {
|
||||
return strtoll(str.c_str(), nullptr, base);
|
||||
}
|
||||
|
||||
uint8_t parse_css_int(const std::string &str) { // int or percentage.
|
||||
uint8_t parseCssInt(const std::string &str) { // int or percentage.
|
||||
if (str.length() && str.back() == '%') {
|
||||
return clamp_css_byte(parseFloat(str) / 100.0f * 255.0f);
|
||||
} else {
|
||||
return clamp_css_byte(parseInt(str));
|
||||
return clampCssByte(parseFloat(str) / 100.0f * 255.0f);
|
||||
}
|
||||
return clampCssByte(parseInt(str));
|
||||
}
|
||||
|
||||
float parseCssFloat(const std::string &str) { // float or percentage.
|
||||
if (str.length() && str.back() == '%') {
|
||||
return clampCssFloat(parseFloat(str) / 100.0f);
|
||||
}
|
||||
return clampCssFloat(parseFloat(str));
|
||||
}
|
||||
|
||||
float parse_css_float(const std::string &str) { // float or percentage.
|
||||
if (str.length() && str.back() == '%') {
|
||||
return clamp_css_float(parseFloat(str) / 100.0f);
|
||||
} else {
|
||||
return clamp_css_float(parseFloat(str));
|
||||
}
|
||||
}
|
||||
|
||||
float css_hue_to_rgb(float m1, float m2, float h) {
|
||||
float cssHueToRgb(float m1, float m2, float h) {
|
||||
if (h < 0.0f) {
|
||||
h += 1.0f;
|
||||
} else if (h > 1.0f) {
|
||||
@@ -277,8 +275,8 @@ std::vector<std::string> split(const std::string &s, char delim) {
|
||||
return elems;
|
||||
}
|
||||
|
||||
Color parse(const std::string &css_str) {
|
||||
std::string str = css_str;
|
||||
Color parse(const std::string &cssStr) {
|
||||
std::string str = cssStr;
|
||||
|
||||
// Remove all whitespace, not compliant, but should just be more accepting.
|
||||
str.erase(std::remove(str.begin(), str.end(), ' '), str.end());
|
||||
@@ -286,9 +284,9 @@ Color parse(const std::string &css_str) {
|
||||
// Convert to lowercase.
|
||||
std::transform(str.begin(), str.end(), str.begin(), ::tolower);
|
||||
|
||||
for (size_t i = 0; i < namedColors.size(); i++) {
|
||||
if (str == namedColors[i].name) {
|
||||
return {namedColors[i].color};
|
||||
for (const auto &namedColor : NAMED_COLORS) {
|
||||
if (str == namedColor.name) {
|
||||
return namedColor.color;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -299,28 +297,30 @@ Color parse(const std::string &css_str) {
|
||||
parseInt(str.substr(1), 16); // REFINE(deanm): Stricter parsing.
|
||||
if (!(iv >= 0 && iv <= 0xfff)) {
|
||||
return {};
|
||||
} else {
|
||||
return Color(
|
||||
static_cast<uint8_t>(((iv & 0xf00) >> 4) | ((iv & 0xf00) >> 8)),
|
||||
static_cast<uint8_t>((iv & 0xf0) | ((iv & 0xf0) >> 4)),
|
||||
static_cast<uint8_t>((iv & 0xf) | ((iv & 0xf) << 4)), 1);
|
||||
}
|
||||
} else if (str.length() == 7) {
|
||||
int64_t iv =
|
||||
parseInt(str.substr(1), 16); // REFINE(deanm): Stricter parsing.
|
||||
}
|
||||
|
||||
return Color(
|
||||
static_cast<uint8_t>(((iv & 0xf00) >> 4) | ((iv & 0xf00) >> 8)),
|
||||
static_cast<uint8_t>((iv & 0xf0) | ((iv & 0xf0) >> 4)),
|
||||
static_cast<uint8_t>((iv & 0xf) | ((iv & 0xf) << 4)), 1);
|
||||
}
|
||||
|
||||
if (str.length() == 7) {
|
||||
int64_t iv = parseInt(str.substr(1), 16); // REFINE(deanm): Stricter parsing.
|
||||
if (!(iv >= 0 && iv <= 0xffffff)) {
|
||||
return {}; // Covers NaN.
|
||||
} else {
|
||||
return Color(static_cast<uint8_t>((iv & 0xff0000) >> 16),
|
||||
static_cast<uint8_t>((iv & 0xff00) >> 8),
|
||||
static_cast<uint8_t>(iv & 0xff), 1);
|
||||
}
|
||||
|
||||
return Color(static_cast<uint8_t>((iv & 0xff0000) >> 16),
|
||||
static_cast<uint8_t>((iv & 0xff00) >> 8),
|
||||
static_cast<uint8_t>(iv & 0xff), 1);
|
||||
}
|
||||
|
||||
return Color();
|
||||
}
|
||||
|
||||
size_t op = str.find_first_of('('), ep = str.find_first_of(')');
|
||||
size_t op = str.find_first_of('(');
|
||||
size_t ep = str.find_first_of(')');
|
||||
if (op != std::string::npos && ep + 1 == str.length()) {
|
||||
const std::string fname = str.substr(0, op);
|
||||
const std::vector<std::string> params =
|
||||
@@ -333,22 +333,24 @@ Color parse(const std::string &css_str) {
|
||||
if (params.size() != 4) {
|
||||
return {};
|
||||
}
|
||||
alpha = parse_css_float(params.back());
|
||||
alpha = parseCssFloat(params.back());
|
||||
} else {
|
||||
if (params.size() != 3) {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
return Color(parse_css_int(params[0]), parse_css_int(params[1]),
|
||||
parse_css_int(params[2]), alpha);
|
||||
return Color(parseCssInt(params[0]), parseCssInt(params[1]),
|
||||
parseCssInt(params[2]), alpha);
|
||||
|
||||
} else if (fname == "hsla" || fname == "hsl") {
|
||||
}
|
||||
|
||||
if (fname == "hsla" || fname == "hsl") {
|
||||
if (fname == "hsla") {
|
||||
if (params.size() != 4) {
|
||||
return {};
|
||||
}
|
||||
alpha = parse_css_float(params.back());
|
||||
alpha = parseCssFloat(params.back());
|
||||
} else {
|
||||
if (params.size() != 3) {
|
||||
return {};
|
||||
@@ -356,23 +358,25 @@ Color parse(const std::string &css_str) {
|
||||
}
|
||||
|
||||
float h = parseFloat(params[0]) / 360.0f;
|
||||
while (h < 0.0f)
|
||||
h++;
|
||||
while (h > 1.0f)
|
||||
h--;
|
||||
while (h < 0.0f) {
|
||||
++h;
|
||||
}
|
||||
while (h > 1.0f) {
|
||||
--h;
|
||||
}
|
||||
|
||||
// NOTE(deanm): According to the CSS spec s/l should only be
|
||||
// percentages, but we don't bother and let float or percentage.
|
||||
float s = parse_css_float(params[1]);
|
||||
float l = parse_css_float(params[2]);
|
||||
float s = parseCssFloat(params[1]);
|
||||
float l = parseCssFloat(params[2]);
|
||||
|
||||
float m2 = l <= 0.5f ? l * (s + 1.0f) : l + s - l * s;
|
||||
float m1 = l * 2.0f - m2;
|
||||
|
||||
return Color(
|
||||
clamp_css_byte(css_hue_to_rgb(m1, m2, h + 1.0f / 3.0f) * 255.0f),
|
||||
clamp_css_byte(css_hue_to_rgb(m1, m2, h) * 255.0f),
|
||||
clamp_css_byte(css_hue_to_rgb(m1, m2, h - 1.0f / 3.0f) * 255.0f),
|
||||
clampCssByte(cssHueToRgb(m1, m2, h + 1.0f / 3.0f) * 255.0f),
|
||||
clampCssByte(cssHueToRgb(m1, m2, h) * 255.0f),
|
||||
clampCssByte(cssHueToRgb(m1, m2, h - 1.0f / 3.0f) * 255.0f),
|
||||
alpha);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,19 +47,17 @@
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
// IN THE SOFTWARE.
|
||||
|
||||
#ifndef CSS_COLOR_PARSER_CPP
|
||||
#define CSS_COLOR_PARSER_CPP
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <math.h>
|
||||
#include <cmath>
|
||||
|
||||
namespace CSSColorParser {
|
||||
|
||||
struct Color {
|
||||
inline Color() {
|
||||
}
|
||||
inline Color(unsigned char r_, unsigned char g_, unsigned char b_, float a_)
|
||||
: r(r_), g(g_), b(b_), a(a_ > 1 ? 1 : a_ < 0 ? 0 : a_) {
|
||||
inline Color() = default;
|
||||
inline Color(unsigned char red, unsigned char green, unsigned char blue, float alpha)
|
||||
: r(red), g(green), b(blue), a(alpha > 1 ? 1 : alpha < 0 ? 0 : alpha) {
|
||||
}
|
||||
unsigned char r = 0, g = 0, b = 0;
|
||||
float a = 1.0f;
|
||||
@@ -73,8 +71,6 @@ inline bool operator!=(const Color &lhs, const Color &rhs) {
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
|
||||
Color parse(const std::string &css_str);
|
||||
Color parse(const std::string &cssStr);
|
||||
|
||||
} // namespace CSSColorParser
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user