Skip to content

Commit 3e6ce56

Browse files
committed
_toFlags() and _fromFlags() to isolate the bitshift magic
1 parent fc756e8 commit 3e6ce56

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

src/ESPAsyncWebServer.h

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,7 @@ class AsyncURIMatcher {
788788
}
789789

790790
AsyncURIMatcher(AsyncURIMatcher &&c) : _value(std::move(c._value)), _flags(c._flags) {
791-
c._flags = intptr_t(Type::None);
791+
c._flags = _toFlags(Type::None, None);
792792
}
793793

794794
~AsyncURIMatcher() {
@@ -824,7 +824,7 @@ class AsyncURIMatcher {
824824
_flags = r._flags;
825825
if (r._isRegex()) {
826826
// We have adopted it
827-
r._flags = intptr_t(Type::None);
827+
r._flags = _toFlags(Type::None, None);
828828
}
829829
return *this;
830830
}
@@ -854,9 +854,9 @@ class AsyncURIMatcher {
854854
#endif
855855

856856
// extract matcher type from _flags
857-
const intptr_t flags = _flags >> 1; // shift off disambiguation bit;
858-
const Type type = static_cast<Type>(flags & 0xFFFF); // Type is lower 16 bits
859-
const uint16_t modifiers = flags >> 16; // Modifiers are upper 16 bits
857+
Type type;
858+
uint16_t modifiers;
859+
_fromFlags(_flags, type, modifiers);
860860

861861
// apply modifiers
862862
String path = request->url();
@@ -1007,7 +1007,6 @@ class AsyncURIMatcher {
10071007
}
10081008
#endif
10091009

1010-
// Core private constructor
10111010
AsyncURIMatcher(String uri, Type type, uint16_t modifiers) : _value(std::move(uri)) {
10121011
#ifdef ASYNCWEBSERVER_REGEX
10131012
if ((type == Type::Regex) || ((type == Type::Auto) && _value.startsWith("^") && _value.endsWith("$"))) {
@@ -1037,13 +1036,23 @@ class AsyncURIMatcher {
10371036
type = Type::BackwardCompatible;
10381037
}
10391038
}
1040-
_flags = uint32_t(modifiers) << 16 | uint16_t(type);
1039+
_flags = _toFlags(type, modifiers);
1040+
}
1041+
1042+
static intptr_t _toFlags(Type type, uint16_t modifiers) {
1043+
intptr_t f = uint32_t(modifiers) << 16 | uint16_t(type);
10411044
// Use lsb to disambiguate from regex pointer in the case where someone has regex activated but uses a non-regex type.
10421045
// We always do this shift, even if regex is not enabled, to keep the layout identical and also catch programmatic errors earlier.
10431046
// For example a mistake is to set a modifier flag to (1 << 15), which is the msb of the uint16_t.
10441047
// This msb is discarded during this shift operation.
10451048
// So pay attention to not have more than 15 modifier flags.
1046-
_flags = (_flags << 1) + 1;
1049+
return (f << 1) + 1;
1050+
}
1051+
1052+
static void _fromFlags(intptr_t in_flags, Type &out_type, uint16_t &out_modifiers) {
1053+
in_flags >>= 1; // shift off disambiguation bit;
1054+
out_type = static_cast<Type>(in_flags & 0xFFFF); // Type is lower 16 bits
1055+
out_modifiers = in_flags >> 16; // Modifiers are upper 16 bits
10471056
}
10481057
};
10491058

0 commit comments

Comments
 (0)