Repository: tesseract-ocr/tesseract
File: src/ccutil/unicharmap.cpp (C++)

Code before:
///////////////////////////////////////////////////////////////////////

#include <cassert>
#include "unichar.h"
#include "unicharmap.h"

UNICHARMAP::UNICHARMAP() :
nodes(nullptr) {
}

UNICHARMAP::~UNICHARMAP() {
  delete[] nodes;
}

// Search the given unichar representation in the tree, using length characters
// from it maximum. Each character in the string is interpreted as an index in
// an array of nodes.
UNICHAR_ID UNICHARMAP::unichar_to_id(const char* const unichar_repr,
                                     int length) const {
  UNICHARMAP_NODE* current_nodes = nodes;

  assert(*unichar_repr != '\0');
  assert(length > 0 && length <= UNICHAR_LEN);

  int index = 0;
  if (unichar_repr[index] == '\0') return INVALID_UNICHAR_ID;
  do {
    if (index + 1 >= length || unichar_repr[index + 1] == '\0')
      return current_nodes[static_cast<unsigned char>(unichar_repr[index])].id;
    current_nodes =
        current_nodes[static_cast<unsigned char>(unichar_repr[index])].children;
    ++index;
  } while (true);
}

// Search the given unichar representation in the tree, creating the possibly
// missing nodes. Once the right place has been found, insert the given id and
// update the inserted flag to keep track of the insert. Each character in the
// string is interpreted as an index in an array of nodes.
void UNICHARMAP::insert(const char* const unichar_repr, UNICHAR_ID id) {
  const char* current_char = unichar_repr;
  if (*current_char == '\0') return;
  UNICHARMAP_NODE** current_nodes_pointer = &nodes;
  do {
    if (*current_nodes_pointer == nullptr)
      *current_nodes_pointer = new UNICHARMAP_NODE[256];
    if (current_char[1] == '\0') {
      (*current_nodes_pointer)
          [static_cast<unsigned char>(*current_char)].id = id;
      return;
    }

Review comment (suggestion):
That won't handle the case `length <= 0` in release code (which removes the `assert`).

Code after:
///////////////////////////////////////////////////////////////////////

#include <cassert>
#include "unichar.h"
#include "unicharmap.h"

UNICHARMAP::UNICHARMAP() :
nodes(nullptr) {
}

UNICHARMAP::~UNICHARMAP() {
  delete[] nodes;
}

// Search the given unichar representation in the tree, using length characters
// from it maximum. Each character in the string is interpreted as an index in
// an array of nodes.
UNICHAR_ID UNICHARMAP::unichar_to_id(const char* const unichar_repr,
                                     int length) const {
  UNICHARMAP_NODE* current_nodes = nodes;

  assert(*unichar_repr != '\0');
  assert(length > 0 && length <= UNICHAR_LEN);

  int index = 0;
  if (length <= 0 || unichar_repr[index] == '\0') return INVALID_UNICHAR_ID;
  do {
    if (index + 1 >= length || unichar_repr[index + 1] == '\0')
      return current_nodes[static_cast<unsigned char>(unichar_repr[index])].id;
    current_nodes =
        current_nodes[static_cast<unsigned char>(unichar_repr[index])].children;
    ++index;
  } while (true);
}

// Search the given unichar representation in the tree, creating the possibly
// missing nodes. Once the right place has been found, insert the given id and
// update the inserted flag to keep track of the insert. Each character in the
// string is interpreted as an index in an array of nodes.
void UNICHARMAP::insert(const char* const unichar_repr, UNICHAR_ID id) {
  const char* current_char = unichar_repr;
  if (*current_char == '\0') return;
  UNICHARMAP_NODE** current_nodes_pointer = &nodes;
  do {
    if (*current_nodes_pointer == nullptr)
      *current_nodes_pointer = new UNICHARMAP_NODE[256];
    if (current_char[1] == '\0') {
      (*current_nodes_pointer)
          [static_cast<unsigned char>(*current_char)].id = id;
      return;
    }