diff --git a/apps/code/variable_box_controller.cpp b/apps/code/variable_box_controller.cpp index eb78d6681..eed3de696 100644 --- a/apps/code/variable_box_controller.cpp +++ b/apps/code/variable_box_controller.cpp @@ -271,12 +271,12 @@ int VariableBoxController::NodeNameCompare(ScriptNode * node, const char * name, int VariableBoxController::nodesCountForOrigin(NodeOrigin origin) const { if (origin == NodeOrigin::Builtins) { - return m_builtinNodesCount; + return static_cast(m_builtinNodesCount); } - return *(const_cast(this)->nodesCountPointerForOrigin(origin)); + return static_cast(*(const_cast(this)->nodesCountPointerForOrigin(origin))); } -int * VariableBoxController::nodesCountPointerForOrigin(NodeOrigin origin) { +size_t * VariableBoxController::nodesCountPointerForOrigin(NodeOrigin origin) { if (origin == NodeOrigin::CurrentScript) { return &m_currentScriptNodesCount; } @@ -935,7 +935,7 @@ bool VariableBoxController::contains(const char * name, int nameLength) { void VariableBoxController::addNode(ScriptNode::Type type, NodeOrigin origin, const char * name, int nameLength, const char * nodeSourceName, const char * description) { assert(origin == NodeOrigin::CurrentScript || origin == NodeOrigin::Importation); - int * currentNodeCount = nodesCountPointerForOrigin(origin); + size_t * currentNodeCount = nodesCountPointerForOrigin(origin); if (*currentNodeCount >= MaxNodesCountForOrigin(origin)) { // There is no room to add another node return; @@ -943,21 +943,24 @@ void VariableBoxController::addNode(ScriptNode::Type type, NodeOrigin origin, co /* We want to insert the node in alphabetical order, so we look for the * insertion index. */ ScriptNode * nodes = nodesForOrigin(origin); - int insertionIndex = 0; - while (insertionIndex < *currentNodeCount) { - ScriptNode * node = nodes + insertionIndex; - int nameComparison = NodeNameCompare(node, name, nameLength); - assert(nameComparison != 0); // We already checked that the name is not present already - if (nameComparison > 0) { - break; + size_t insertionIndex = 0; + if (*currentNodeCount != 0) { + while (insertionIndex < *currentNodeCount) { + ScriptNode * node = nodes + insertionIndex; + int nameComparison = NodeNameCompare(node, name, nameLength); + assert(nameComparison != 0); // We already checked that the name is not present already + if (nameComparison > 0) { + break; + } + insertionIndex++; + } + + // Shift all the following nodes + for (size_t i = *currentNodeCount; i > insertionIndex; i--) { + nodes[i] = nodes[i - 1]; } - insertionIndex++; } - // Shift all the following nodes - for (int i = *currentNodeCount - 1; i >= insertionIndex; i--) { - nodes[i+1] = nodes[i]; - } // Check if the node source name fits, if not, do not use it if (!ScriptNodeCell::CanDisplayNameAndSource(nameLength, nodeSourceName)) { nodeSourceName = nullptr; diff --git a/apps/code/variable_box_controller.h b/apps/code/variable_box_controller.h index 31f1dd6ff..201d26268 100644 --- a/apps/code/variable_box_controller.h +++ b/apps/code/variable_box_controller.h @@ -41,9 +41,8 @@ public: void empty(); private: - //TODO LEA use size_t constexpr static size_t k_maxNumberOfDisplayedItems = (Ion::Display::Height - Metric::TitleBarHeight - Metric::PopUpTopMargin) / ScriptNodeCell::k_simpleItemHeight + 2; // +2 if the cells are cropped on top and at the bottom - constexpr static int k_maxScriptNodesCount = 32; //TODO LEA + constexpr static size_t k_maxScriptNodesCount = 32; //TODO LEA constexpr static int k_totalBuiltinNodesCount = 107; constexpr static uint8_t k_scriptOriginsCount = 3; constexpr static uint8_t k_subtitleCellType = NodeCellType; // We don't care as it is not selectable @@ -66,12 +65,12 @@ private: static int NodeNameCompare(ScriptNode * node, const char * name, int nameLength, bool * strictlyStartsWith = nullptr); // Nodes and nodes count - static int MaxNodesCountForOrigin(NodeOrigin origin) { + static size_t MaxNodesCountForOrigin(NodeOrigin origin) { assert(origin == NodeOrigin::CurrentScript || origin == NodeOrigin::Importation); return k_maxScriptNodesCount; } int nodesCountForOrigin(NodeOrigin origin) const; - int * nodesCountPointerForOrigin(NodeOrigin origin); + size_t * nodesCountPointerForOrigin(NodeOrigin origin); ScriptNode * nodesForOrigin(NodeOrigin origin); ScriptNode * scriptNodeAtIndex(int index); @@ -109,9 +108,9 @@ private: MessageTableCell m_subtitleCells[k_scriptOriginsCount]; ScriptStore * m_scriptStore; // TODO LEA Put these in an array? - int m_currentScriptNodesCount; - int m_builtinNodesCount; - int m_importedNodesCount; + size_t m_currentScriptNodesCount; + size_t m_builtinNodesCount; + size_t m_importedNodesCount; int m_shortenResultCharCount; // This is used to send only the completing text when we are autocompleting };