#include "storage_function.h" #include "poincare_helpers.h" #include #include #include "poincare/src/parsing/parser.h" #include #include #include #include #include using namespace Poincare; namespace Shared { constexpr char StorageFunction::k_parenthesedArgument[]; bool StorageFunction::BaseNameCompliant(const char * baseName, NameNotCompliantError * error) { assert(baseName[0] != 0); UTF8Decoder decoder(baseName); CodePoint c = decoder.nextCodePoint(); if (UTF8Helper::CodePointIsNumber(c)) { // The name cannot start with a number if (error != nullptr) { *error = NameNotCompliantError::NameCannotStartWithNumber; } return false; } // The name should only have allowed characters while (c != UCodePointNull) { if (!(UTF8Helper::CodePointIsUpperCaseLetter(c) || UTF8Helper::CodePointIsLowerCaseLetter(c) || UTF8Helper::CodePointIsNumber(c)) || c == '_') { if (error != nullptr) { *error = NameNotCompliantError::CharacterNotAllowed; } return false; } c = decoder.nextCodePoint(); } // The name should not be a reserved name if (Parser::IsReservedName(baseName, strlen(baseName))) { if (error != nullptr) { *error = NameNotCompliantError::ReservedName; } return false; } return true; } bool StorageFunction::isActive() const { return recordData()->isActive(); } KDColor StorageFunction::color() const { return recordData()->color(); } void StorageFunction::setActive(bool active) { recordData()->setActive(active); } int StorageFunction::nameWithArgument(char * buffer, size_t bufferSize, char arg) { const char * functionName = fullName(); size_t baseNameLength = SymbolAbstract::TruncateExtension(buffer, functionName, bufferSize - k_parenthesedArgumentLength); int result = baseNameLength + strlcpy(&buffer[baseNameLength], k_parenthesedArgument, bufferSize-baseNameLength); if (baseNameLength+1 < bufferSize - 1) { buffer[baseNameLength+1] = arg; } return result; } template T StorageFunction::templatedApproximateAtAbscissa(T x, Poincare::Context * context) const { if (isCircularlyDefined(context)) { return NAN; } constexpr int bufferSize = CodePoint::MaxCodePointCharLength + 1; char unknownX[bufferSize]; Poincare::SerializationHelper::CodePoint(unknownX, bufferSize, Poincare::Symbol::UnknownX); return PoincareHelpers::ApproximateWithValueForSymbol(expressionReduced(context), unknownX, x, *context); } StorageFunction::FunctionRecordData * StorageFunction::recordData() const { assert(!isNull()); Ion::Storage::Record::Data d = value(); return reinterpret_cast(const_cast(d.buffer)); } } template float Shared::StorageFunction::templatedApproximateAtAbscissa(float, Poincare::Context*) const; template double Shared::StorageFunction::templatedApproximateAtAbscissa(double, Poincare::Context*) const;