diff options
Diffstat (limited to 'source')
-rw-r--r-- | source/fud_directory.cpp | 2 | ||||
-rw-r--r-- | source/fud_file.cpp | 59 | ||||
-rw-r--r-- | source/fud_format.cpp | 59 | ||||
-rw-r--r-- | source/fud_string.cpp | 54 | ||||
-rw-r--r-- | source/fud_string_convert.cpp | 25 |
5 files changed, 106 insertions, 93 deletions
diff --git a/source/fud_directory.cpp b/source/fud_directory.cpp index 1dc1aba..7dcf911 100644 --- a/source/fud_directory.cpp +++ b/source/fud_directory.cpp @@ -146,7 +146,7 @@ Result<DirectoryEntry, FudStatus> Directory::info() m_errorCode = 0; } - return retValue; + return RetType::okay(std::move(retValue)); } Result<Option<DirectoryEntry>, FudStatus> Directory::getNextEntry() diff --git a/source/fud_file.cpp b/source/fud_file.cpp index 8dab031..8f84648 100644 --- a/source/fud_file.cpp +++ b/source/fud_file.cpp @@ -34,11 +34,11 @@ FileResult RegularFile::open( Allocator* allocator) { if (allocator == nullptr) { - return FudStatus::NullPointer; + return FileResult::error(FudStatus::NullPointer); } if (!filename.nullTerminated()) { - return FudStatus::ArgumentInvalid; + return FileResult::error(FudStatus::ArgumentInvalid); } int dirFd = dirFdOption.valueOr(AT_FDCWD); @@ -57,11 +57,11 @@ FileResult RegularFile::open( openFlags = O_RDWR; break; default: - return FudStatus::ArgumentInvalid; + return FileResult::error(FudStatus::ArgumentInvalid); } if (flags.hasFlag(OpenFlagEnum::Append) && flags.hasFlag(OpenFlagEnum::Truncate)) { - return FudStatus::OperationInvalid; + return FileResult::error(FudStatus::OperationInvalid); } openFlags |= flags.flags(); @@ -74,31 +74,31 @@ FileResult RegularFile::open( auto status = syscall(SYS_openat2, dirFd, filename.data(), &openHow, sizeof(openHow)); if (status == -1) { if constexpr (EAGAIN != EWOULDBLOCK && status == EWOULDBLOCK) { - return FudStatus::Partial; + return FileResult::error(FudStatus::Partial); } switch (errno) { case ETXTBSY: case EAGAIN: - return FudStatus::Partial; + return FileResult::error(FudStatus::Partial); case ENOENT: - return FudStatus::NotFound; + return FileResult::error(FudStatus::NotFound); case EBADF: case EFBIG: case EOVERFLOW: case EINVAL: case EISDIR: case ENAMETOOLONG: - return FudStatus::ArgumentInvalid; + return FileResult::error(FudStatus::ArgumentInvalid); case EROFS: case EACCES: case EPERM: - return FudStatus::PermissionDenied; + return FileResult::error(FudStatus::PermissionDenied); case ELOOP: case EXDEV: case ENFILE: case E2BIG: default: - return FudStatus::Failure; + return FileResult::error(FudStatus::Failure); } } fudAssert(status <= std::numeric_limits<decltype(file.m_fd)>::max()); @@ -108,14 +108,14 @@ FileResult RegularFile::open( Stat sBuffer{}; auto fStatus = fstat(file.m_fd, &sBuffer); if (fStatus == -1) { - return FudStatus::Failure; + return FileResult::error(FudStatus::Failure); } if ((sBuffer.st_mode & S_IFMT) != S_IFREG) { - return FudStatus::ObjectInvalid; + return FileResult::error(FudStatus::ObjectInvalid); } - return file; + return FileResult::okay(std::move(file)); } FileResult RegularFile::create( @@ -128,11 +128,11 @@ FileResult RegularFile::create( Allocator* allocator) { if (allocator == nullptr) { - return FudStatus::NullPointer; + return FileResult::error(FudStatus::NullPointer); } if (!filename.nullTerminated()) { - return FudStatus::ArgumentInvalid; + return FileResult::error(FudStatus::ArgumentInvalid); } int dirFd = dirFdOption.valueOr(AT_FDCWD); @@ -151,11 +151,11 @@ FileResult RegularFile::create( openFlags = O_RDWR; break; default: - return FudStatus::ArgumentInvalid; + return FileResult::error(FudStatus::ArgumentInvalid); } if (flags.hasFlag(OpenFlagEnum::Append) && flags.hasFlag(OpenFlagEnum::Truncate)) { - return FudStatus::OperationInvalid; + return FileResult::error(FudStatus::OperationInvalid); } openFlags |= flags.flags() | O_CREAT | (O_EXCL * static_cast<uint8_t>(exclusive)); @@ -169,23 +169,23 @@ FileResult RegularFile::create( auto status = syscall(SYS_openat2, dirFd, filename.data(), &openHow, sizeof(openHow)); if (status == -1) { if constexpr (EAGAIN != EWOULDBLOCK && status == EWOULDBLOCK) { - return FudStatus::Partial; + return FileResult::error(FudStatus::Partial); } switch (errno) { case ETXTBSY: case EAGAIN: - return FudStatus::Partial; + return FileResult::error(FudStatus::Partial); case EBADF: case EFBIG: case EOVERFLOW: case EINVAL: case EISDIR: case ENAMETOOLONG: - return FudStatus::ArgumentInvalid; + return FileResult::error(FudStatus::ArgumentInvalid); case EROFS: case EACCES: case EPERM: - return FudStatus::PermissionDenied; + return FileResult::error(FudStatus::PermissionDenied); case EDQUOT: case ENOENT: case ELOOP: @@ -193,7 +193,7 @@ FileResult RegularFile::create( case ENFILE: case E2BIG: default: - return FudStatus::Failure; + return FileResult::error(FudStatus::Failure); } } fudAssert(status <= std::numeric_limits<decltype(file.m_fd)>::max()); @@ -203,14 +203,14 @@ FileResult RegularFile::create( Stat sBuffer{}; auto fStatus = fstat(file.m_fd, &sBuffer); if (fStatus == -1) { - return FudStatus::Failure; + return FileResult::error(FudStatus::Failure); } if ((sBuffer.st_mode & S_IFMT) != S_IFREG) { - return FudStatus::ObjectInvalid; + return FileResult::error(FudStatus::ObjectInvalid); } - return file; + return FileResult::okay(std::move(file)); } RegularFile::~RegularFile() @@ -284,23 +284,24 @@ FudStatus RegularFile::close() Result<size_t, FudStatus> RegularFile::size() const { + using RetType = Result<size_t, FudStatus>; auto fileSize = lseek(m_fd, 0, SEEK_END); if (fileSize == -1) { switch (errno) { case EBADF: case ESPIPE: - return FudStatus::ObjectInvalid; + return RetType::error(FudStatus::ObjectInvalid); default: - return FudStatus::Failure; + return RetType::error(FudStatus::Failure); } } auto seekBegin = lseek(m_fd, 0, SEEK_SET); if (seekBegin == -1) { - return FudStatus::Failure; + return RetType::error(FudStatus::Failure); } - return static_cast<size_t>(fileSize); + return RetType::okay(static_cast<size_t>(fileSize)); } } // namespace fud diff --git a/source/fud_format.cpp b/source/fud_format.cpp index 960a146..f1fc3cf 100644 --- a/source/fud_format.cpp +++ b/source/fud_format.cpp @@ -25,8 +25,9 @@ FudStatus getFormatSign(StringView& formatView, FormatSpec& spec); Result<FormatSpec, FudStatus> FormatSpec::parse(StringView formatView, size_t& specLength) { + using RetType = Result<FormatSpec, FudStatus>; if (formatView.length() < 2 || formatView[0] != FormatSpec::openBracket) { - return FudStatus::ArgumentInvalid; + return RetType::error(FudStatus::ArgumentInvalid); } auto hasEnded = [](StringView& view) { @@ -43,82 +44,82 @@ Result<FormatSpec, FudStatus> FormatSpec::parse(StringView formatView, size_t& s bool hasColon{false}; auto lengthResult = impl::preScanSpec(formatView, spec, hasPosition, hasColon); if (lengthResult.isError()) { - return lengthResult.takeError(); + return RetType::error(lengthResult.takeError()); } length += lengthResult.takeOkay(); if (length < 1) { - return FudStatus::FormatInvalid; + return RetType::error(FudStatus::FormatInvalid); } specLength = length; auto status = impl::getPosition(formatView, spec, hasPosition); if (status != FudStatus::Success) { - return status; + return RetType::error(status); } // check early ending if (!hasColon) { if (hasEnded(formatView)) { - return spec; + return RetType::okay(spec); } - return FudStatus::FormatInvalid; + return RetType::error(FudStatus::FormatInvalid); } // check validity for being non-default spec and advance if (formatView[0] != ':') { - return FudStatus::FormatInvalid; + return RetType::error(FudStatus::FormatInvalid); } formatView.advanceUnsafe(); if (hasEnded(formatView)) { - return spec; + return RetType::okay(spec); } // Spec status = impl::getFill(formatView, spec); if (status != FudStatus::Success) { - return status; + return RetType::error(status); } if (hasEnded(formatView)) { - return spec; + return RetType::okay(spec); } // sign, alternate, leading zero status = impl::getSpecialOpts(formatView, spec); if (status != FudStatus::Success) { - return status; + return RetType::error(status); } if (hasEnded(formatView)) { - return spec; + return RetType::okay(spec); } // Width status = impl::getWidth(formatView, spec); if (status != FudStatus::Success) { - return status; + return RetType::error(status); } if (hasEnded(formatView)) { - return spec; + return RetType::okay(spec); } // Precision status = impl::getPrecision(formatView, spec); if (status != FudStatus::Success) { - return status; + return RetType::error(status); } if (hasEnded(formatView)) { - return spec; + return RetType::okay(spec); } // Locale @@ -129,26 +130,27 @@ Result<FormatSpec, FudStatus> FormatSpec::parse(StringView formatView, size_t& s } if (hasEnded(formatView)) { - return spec; + return RetType::okay(spec); } // Format Sign status = impl::getFormatSign(formatView, spec); if (status != FudStatus::Success) { - return status; + return RetType::error(status); } if (!hasEnded(formatView)) { - return FudStatus::FormatInvalid; + return RetType::error(FudStatus::FormatInvalid); } - return spec; + return RetType::okay(spec); } namespace impl { Result<size_t, FudStatus> preScanSpec(StringView formatView, FormatSpec& spec, bool& hasPosition, bool& hasColon) { + using RetType = Result<size_t, FudStatus>; int nesting = 0; uint32_t captureGroups = 0; size_t index = 0; @@ -162,14 +164,14 @@ Result<size_t, FudStatus> preScanSpec(StringView formatView, FormatSpec& spec, b switch (letter) { case FormatSpec::openBracket: if (not hasColon) { - return FudStatus::FormatInvalid; + return RetType::error(FudStatus::FormatInvalid); } if (takesWidth) { spec.takesWidth = true; } else if (takesPrecision) { spec.takesPrecision = true; } else { - return FudStatus::FormatInvalid; + return RetType::error(FudStatus::FormatInvalid); } nesting++; captureGroups++; @@ -193,7 +195,7 @@ Result<size_t, FudStatus> preScanSpec(StringView formatView, FormatSpec& spec, b } if (nesting > 1) { - return FudStatus::FormatInvalid; + return RetType::error(FudStatus::FormatInvalid); } index++; @@ -201,10 +203,10 @@ Result<size_t, FudStatus> preScanSpec(StringView formatView, FormatSpec& spec, b if (nesting != 0 || captureGroups > 2 || index >= formatView.length() || formatView[index] != FormatSpec::closeBracket) { - return FudStatus::FormatInvalid; + return RetType::error(FudStatus::FormatInvalid); } - return index + 1U; + return RetType::okay(index + 1U); } FudStatus getPosition(StringView& formatView, FormatSpec& spec, bool& hasPosition) @@ -570,6 +572,7 @@ FudStatus fillUnsignedBuffer( [[nodiscard]] Result<bool, FudStatus> validateFloatFormatType(FormatType formatType) { + using RetType = Result<bool, FudStatus>; auto uppercase = false; switch (formatType) { @@ -604,13 +607,13 @@ FudStatus fillUnsignedBuffer( case FormatType::BinaryUpper: case FormatType::String: case FormatType::Escaped: - return FudStatus::FormatInvalid; + return RetType::error(FudStatus::FormatInvalid); break; default: - return FudStatus::Failure; + return RetType::error(FudStatus::Failure); } - return uppercase; + return RetType::okay(uppercase); } ExponentBuffer getScientificExponent(int exponent, uint8_t& exponentLength, bool uppercase) diff --git a/source/fud_string.cpp b/source/fud_string.cpp index 4943dac..edb25da 100644 --- a/source/fud_string.cpp +++ b/source/fud_string.cpp @@ -51,27 +51,27 @@ StringResult String::makeFromCString(const char* cString, Allocator* allocator) } String output{}; - output.m_allocator = allocator; + output.m_allocator = reinterpret_cast<uintptr_t>(allocator); utf8* data{nullptr}; size_t capacity = length + 1; bool isLarge = capacity > SsoBufSize; if (isLarge) { - output.m_repr.large.capacity = capacity & largeStringCapacitymask; + output.m_repr.large.capacity = capacity; output.m_repr.large.length = length; auto dataResult = output.allocator()->allocate(output.m_repr.large.capacity); if (dataResult.isError()) { return StringResult::error(dataResult.getError()); } output.m_repr.large.data = static_cast<utf8*>(dataResult.getOkay()); - output.m_repr.large.isLarge = 1; data = output.m_repr.large.data; + output.setLarge(); } else { capacity = SsoBufSize; static_assert(SsoBufSize < std::numeric_limits<int8_t>::max()); - output.m_repr.small.isLarge = 0; output.m_repr.small.length = static_cast<uint8_t>(length) & smallStringLengthMask; data = output.m_repr.small.buffer.data(); + output.setSmall(); } fudAssert(data != nullptr); @@ -99,7 +99,7 @@ StringResult String::from(const String& rhs) if (rhs.isLarge()) { output.m_repr.large = rhs.m_repr.large; output.m_repr.large.data = static_cast<utf8*>( - M_TakeOrReturn(output.allocator()->allocate(output.m_repr.large.capacity))); + M_TakeOrReturn(StringResult, output.allocator()->allocate(output.m_repr.large.capacity))); data = output.m_repr.large.data; capacity = output.m_repr.large.capacity; length = output.m_repr.large.length; @@ -135,22 +135,22 @@ StringResult String::from(StringView view, Allocator* allocator) } String output{}; - output.m_allocator = allocator; + output.m_allocator = reinterpret_cast<uintptr_t>(allocator); size_t capacity = view.length() + 1U; bool isLarge = capacity > SsoBufSize; utf8* data{nullptr}; if (isLarge) { - output.m_repr.large.capacity = capacity & largeStringCapacitymask; + output.m_repr.large.capacity = capacity; output.m_repr.large.length = view.length(); - output.m_repr.large.data = static_cast<utf8*>(M_TakeOrReturn(output.allocator()->allocate(capacity))); - output.m_repr.large.isLarge = 1; + output.m_repr.large.data = static_cast<utf8*>(M_TakeOrReturn(StringResult, output.allocator()->allocate(capacity))); data = output.m_repr.large.data; + output.setLarge(); } else { capacity = SsoBufSize; static_assert(SsoBufSize < std::numeric_limits<int8_t>::max()); - output.m_repr.small.isLarge = 0; output.m_repr.small.length = static_cast<uint8_t>(view.length()) & smallStringLengthMask; data = output.m_repr.small.buffer.data(); + output.setSmall(); } fudAssert(data != nullptr); auto copyStatus = copyMem(data, capacity, view.m_data, view.length()); @@ -194,7 +194,11 @@ FudStatus String::copy(const String& rhs) size_t length{}; if (isLarge()) { - m_repr.large.data = static_cast<utf8*>(M_TakeOrReturn(allocator()->allocate(m_repr.large.capacity))); + auto allocResult = allocator()->allocate(m_repr.large.capacity); + if (allocResult.isError()) { + return allocResult.takeError(); + } + m_repr.large.data = static_cast<utf8*>(allocResult.takeOkay()); capacity = m_repr.large.capacity; length = m_repr.large.length; data = m_repr.large.data; @@ -251,11 +255,11 @@ FudStatus String::resize(size_t newCapacity) return FudStatus::OperationInvalid; } - if (!isLarge() && newCapacity <= SSO_BUF_SIZE) { + if (!isLarge() && newCapacity <= SsoBufSize) { return FudStatus::Success; } - if (newCapacity <= SSO_BUF_SIZE) { + if (newCapacity <= SsoBufSize) { fudAssert(isLarge()); auto len = static_cast<uint8_t>(length()); @@ -264,7 +268,7 @@ FudStatus String::resize(size_t newCapacity) fudAssert(copyResult == FudStatus::Success); auto deallocStatus = allocator()->deallocate(m_repr.large.data, m_repr.large.capacity); - m_repr.small.isLarge = 0; + setSmall(); m_repr.small.length = len & smallStringLengthMask; copyMem(m_repr.small.buffer, temp); m_repr.small.buffer[len] = '\0'; @@ -272,7 +276,11 @@ FudStatus String::resize(size_t newCapacity) return deallocStatus != FudStatus::Success ? FudStatus::DeallocFailure : FudStatus::Success; } - auto* newData = static_cast<utf8*>(M_TakeOrReturn(allocator()->allocate(newCapacity))); + auto allocResult = allocator()->allocate(newCapacity); + if (allocResult.isError()) { + return allocResult.takeError(); + } + auto* newData = static_cast<utf8*>(allocResult.takeOkay()); fudAssert(newData != nullptr); auto copyResult = copyMem(newData, newCapacity, data(), length()); @@ -285,8 +293,8 @@ FudStatus String::resize(size_t newCapacity) size_t len = length(); - m_repr.large.isLarge = 1; - m_repr.large.capacity = newCapacity & largeStringCapacitymask; + setLarge(); + m_repr.large.capacity = newCapacity; m_repr.large.data = newData; m_repr.large.length = len; m_repr.large.data[m_repr.large.length] = '\0'; @@ -622,19 +630,19 @@ StringResult String::catenate(const char* rhs) const size_t outputCapacity = outputLength + 1; utf8* outputData{nullptr}; if (outputCapacity > SsoBufSize) { - output.m_repr.large.capacity = outputCapacity & largeStringCapacitymask; + output.m_repr.large.capacity = outputCapacity; output.m_repr.large.length = outputLength; auto dataResult = output.allocator()->allocate(output.m_repr.large.capacity); if (dataResult.isError()) { return StringResult::error(dataResult.getError()); } output.m_repr.large.data = static_cast<utf8*>(dataResult.getOkay()); - output.m_repr.large.isLarge = 1; + output.setLarge(); outputData = output.m_repr.large.data; } else { outputCapacity = SsoBufSize; static_assert(SsoBufSize < std::numeric_limits<int8_t>::max()); - output.m_repr.small.isLarge = 0; + output.setSmall(); output.m_repr.small.length = static_cast<uint8_t>(outputLength) & smallStringLengthMask; outputData = output.m_repr.small.buffer.data(); } @@ -669,19 +677,19 @@ StringResult String::catenate(const String& rhs) const size_t outputCapacity = outputLength + 1; utf8* outputData{nullptr}; if (outputCapacity > SsoBufSize) { - output.m_repr.large.capacity = outputCapacity & largeStringCapacitymask; + output.m_repr.large.capacity = outputCapacity; output.m_repr.large.length = outputLength; auto dataResult = output.allocator()->allocate(output.m_repr.large.capacity); if (dataResult.isError()) { return StringResult::error(dataResult.getError()); } output.m_repr.large.data = static_cast<utf8*>(dataResult.getOkay()); - output.m_repr.large.isLarge = 1; + output.setLarge(); outputData = output.m_repr.large.data; } else { outputCapacity = SsoBufSize; static_assert(SsoBufSize < std::numeric_limits<int8_t>::max()); - output.m_repr.small.isLarge = 0; + output.setSmall(); output.m_repr.small.length = static_cast<uint8_t>(outputLength) & smallStringLengthMask; outputData = output.m_repr.small.buffer.data(); } diff --git a/source/fud_string_convert.cpp b/source/fud_string_convert.cpp index 428ab36..fdf3436 100644 --- a/source/fud_string_convert.cpp +++ b/source/fud_string_convert.cpp @@ -36,59 +36,60 @@ Result<bool, FudStatus> checkNegative(StringView& view, size_t& skipIndex) { bool isNegative = view.data()[0] == '-'; if (isNegative && view.length() == 1) { - return FudStatus::ArgumentInvalid; + return FudError{FudStatus::ArgumentInvalid}; } if (isNegative) { skipIndex += 1; view.advanceUnsafe(); } - return isNegative; + return Okay<bool>{isNegative}; } Result<Radix, FudStatus> determineRadix(StringView input, size_t& index) { if (input.length() < 1) { - return FudStatus::ArgumentInvalid; + return FudError{FudStatus::ArgumentInvalid}; } if (input.length() == 1 && input.data()[0] == '0') { - return Radix::Octal; + return Okay<Radix>{Radix::Octal}; } if (input.length() == 1) { - return Radix::Decimal; + return Okay<Radix>{Radix::Decimal}; } if (input.data()[0] == '0' && (input.data()[1] == 'x' || input.data()[1] == 'X')) { index += 2; - return Radix::Hexadecimal; + return Okay<Radix>{Radix::Hexadecimal}; } if (input.data()[0] == '0') { auto nextChar = input.data()[1]; auto nextVal = AsciiLookup[nextChar]; if (nextVal >= 0 && nextVal < static_cast<uint8_t>(Radix::Octal)) { - return Radix::Octal; + return Okay<Radix>{Radix::Octal}; } if (nextVal >= static_cast<uint8_t>(Radix::Octal)) { - return FudStatus::ArgumentInvalid; + return FudError{FudStatus::ArgumentInvalid}; } } - return Radix::Decimal; + return Okay<Radix>{Radix::Decimal}; } Result<uint8_t, FudStatus> getRadix(StringView& view, size_t& skipIndex, Option<uint8_t> specifiedRadixOption) { + using RetType = Result<uint8_t, FudStatus>; if (specifiedRadixOption.isNone()) { size_t radixIndex = 0; auto status = determineRadix(view, radixIndex); if (status.isOkay()) { skipIndex += radixIndex; view.advanceUnsafe(radixIndex); - return static_cast<uint8_t>(status.takeOkay()); + return RetType::okay(static_cast<uint8_t>(status.takeOkay())); } - return status.takeError(); + return RetType::error(status); } auto radix = specifiedRadixOption.value(); @@ -104,7 +105,7 @@ Result<uint8_t, FudStatus> getRadix(StringView& view, size_t& skipIndex, Option< view.advanceUnsafe(2); } - return radix; + return RetType::okay(radix); } } // namespace fud::impl |