pasaeta loca de clang-format (despres m'arrepentiré pero bueno)
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
#include "enter_name.h"
|
||||
|
||||
#include <stddef.h> // Para size_t
|
||||
#include <stdlib.h> // Para rand
|
||||
#include <string_view> // Para basic_string_view, string_view
|
||||
#include <stddef.h> // Para size_t
|
||||
#include <stdlib.h> // Para rand
|
||||
|
||||
#include "utils.h" // Para trim
|
||||
#include <string_view> // Para basic_string_view, string_view
|
||||
|
||||
#include "utils.h" // Para trim
|
||||
|
||||
// Constructor
|
||||
EnterName::EnterName()
|
||||
@@ -12,18 +13,15 @@ EnterName::EnterName()
|
||||
character_index_{0} {}
|
||||
|
||||
// Inicializa el objeto
|
||||
void EnterName::init(const std::string &name)
|
||||
{
|
||||
void EnterName::init(const std::string &name) {
|
||||
// No se pasa ningún nombre
|
||||
if (name.empty())
|
||||
{
|
||||
if (name.empty()) {
|
||||
name_ = "A";
|
||||
position_ = 0;
|
||||
position_overflow_ = false;
|
||||
}
|
||||
// Se pasa un nombre
|
||||
else
|
||||
{
|
||||
else {
|
||||
name_ = name;
|
||||
position_ = name_.length();
|
||||
position_overflow_ = position_ >= NAME_SIZE ? true : false;
|
||||
@@ -34,28 +32,22 @@ void EnterName::init(const std::string &name)
|
||||
}
|
||||
|
||||
// Incrementa la posición
|
||||
void EnterName::incPosition()
|
||||
{
|
||||
if (position_overflow_)
|
||||
{
|
||||
void EnterName::incPosition() {
|
||||
if (position_overflow_) {
|
||||
// Si ya estamos en overflow, no incrementamos más.
|
||||
return;
|
||||
}
|
||||
|
||||
++position_;
|
||||
|
||||
if (position_ >= NAME_SIZE)
|
||||
{
|
||||
position_ = NAME_SIZE; // Mantenemos en el índice máximo válido.
|
||||
position_overflow_ = true; // Activamos el flag de overflow.
|
||||
}
|
||||
else if (position_ > 0) // No es necesario verificar position_ < MAX_NAME_LENGHT
|
||||
if (position_ >= NAME_SIZE) {
|
||||
position_ = NAME_SIZE; // Mantenemos en el índice máximo válido.
|
||||
position_overflow_ = true; // Activamos el flag de overflow.
|
||||
} else if (position_ > 0) // No es necesario verificar position_ < MAX_NAME_LENGHT
|
||||
{
|
||||
// Copiamos el índice del carácter anterior si es posible.
|
||||
character_index_[position_] = character_index_[position_ - 1];
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
// Si position_ es 0, inicializamos el carácter actual.
|
||||
character_index_[position_] = 0;
|
||||
}
|
||||
@@ -64,36 +56,27 @@ void EnterName::incPosition()
|
||||
}
|
||||
|
||||
// Decrementa la posición
|
||||
void EnterName::decPosition()
|
||||
{
|
||||
if (position_overflow_)
|
||||
{
|
||||
void EnterName::decPosition() {
|
||||
if (position_overflow_) {
|
||||
// Si estaba en overflow, lo desactivamos y mantenemos position_ en el máximo.
|
||||
position_overflow_ = false;
|
||||
position_ = NAME_SIZE - 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (position_ > 0)
|
||||
{
|
||||
} else {
|
||||
if (position_ > 0) {
|
||||
--position_;
|
||||
|
||||
// Limpiamos el carácter siguiente si el índice es válido.
|
||||
if (position_ + 1 < NAME_SIZE)
|
||||
{
|
||||
if (position_ + 1 < NAME_SIZE) {
|
||||
character_index_[position_ + 1] = 0;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
// Si position_ es 0, aseguramos que no vaya a ser negativo y limpiamos el carácter actual.
|
||||
position_ = 0;
|
||||
// character_index_[position_] = 0;
|
||||
}
|
||||
|
||||
// Si position_ es menor que NAME_LENGHT, aseguramos que el overflow esté desactivado.
|
||||
if (position_ < NAME_SIZE)
|
||||
{
|
||||
if (position_ < NAME_SIZE) {
|
||||
position_overflow_ = false;
|
||||
}
|
||||
}
|
||||
@@ -102,71 +85,57 @@ void EnterName::decPosition()
|
||||
}
|
||||
|
||||
// Incrementa el índice
|
||||
void EnterName::incIndex()
|
||||
{
|
||||
if (position_overflow_)
|
||||
{
|
||||
void EnterName::incIndex() {
|
||||
if (position_overflow_) {
|
||||
return;
|
||||
}
|
||||
|
||||
++character_index_[position_];
|
||||
if (character_index_[position_] >= static_cast<int>(character_list_.size()))
|
||||
{
|
||||
if (character_index_[position_] >= static_cast<int>(character_list_.size())) {
|
||||
character_index_[position_] = 0;
|
||||
}
|
||||
updateNameFromCharacterIndex();
|
||||
}
|
||||
|
||||
// Decrementa el índice
|
||||
void EnterName::decIndex()
|
||||
{
|
||||
if (position_overflow_)
|
||||
{
|
||||
void EnterName::decIndex() {
|
||||
if (position_overflow_) {
|
||||
return;
|
||||
}
|
||||
|
||||
--character_index_[position_];
|
||||
if (character_index_[position_] < 0)
|
||||
{
|
||||
if (character_index_[position_] < 0) {
|
||||
character_index_[position_] = character_list_.size() - 1;
|
||||
}
|
||||
updateNameFromCharacterIndex();
|
||||
}
|
||||
|
||||
// Actualiza el nombre a partir de la lista de índices
|
||||
void EnterName::updateNameFromCharacterIndex()
|
||||
{
|
||||
void EnterName::updateNameFromCharacterIndex() {
|
||||
name_.clear();
|
||||
for (size_t i = 0; i < NAME_SIZE; ++i)
|
||||
{
|
||||
for (size_t i = 0; i < NAME_SIZE; ++i) {
|
||||
name_.push_back(character_list_[character_index_[i]]);
|
||||
}
|
||||
name_ = trim(name_);
|
||||
}
|
||||
|
||||
// Actualiza la variable
|
||||
void EnterName::initCharacterIndex(const std::string &name)
|
||||
{
|
||||
void EnterName::initCharacterIndex(const std::string &name) {
|
||||
// Rellena de espacios
|
||||
for (size_t i = 0; i < NAME_SIZE; ++i)
|
||||
{
|
||||
for (size_t i = 0; i < NAME_SIZE; ++i) {
|
||||
character_index_[i] = 0;
|
||||
}
|
||||
|
||||
// Coloca los índices en función de los caracteres que forman el nombre
|
||||
for (size_t i = 0; i < name.substr(0, NAME_SIZE).size(); ++i)
|
||||
{
|
||||
for (size_t i = 0; i < name.substr(0, NAME_SIZE).size(); ++i) {
|
||||
character_index_[i] = findIndex(name.at(i));
|
||||
}
|
||||
}
|
||||
|
||||
// Encuentra el indice de un caracter en "character_list_"
|
||||
int EnterName::findIndex(char character) const
|
||||
{
|
||||
for (size_t i = 0; i < character_list_.size(); ++i)
|
||||
{
|
||||
if (character == character_list_.at(i))
|
||||
{
|
||||
int EnterName::findIndex(char character) const {
|
||||
for (size_t i = 0; i < character_list_.size(); ++i) {
|
||||
if (character == character_list_.at(i)) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
@@ -174,18 +143,15 @@ int EnterName::findIndex(char character) const
|
||||
}
|
||||
|
||||
// Devuelve un nombre al azar
|
||||
std::string EnterName::getRandomName()
|
||||
{
|
||||
std::string EnterName::getRandomName() {
|
||||
static constexpr std::array<std::string_view, 8> NAMES = {
|
||||
"BAL1", "TABE", "DOC", "MON", "SAM1", "JORDI", "JDES", "PEPE"};
|
||||
return std::string(NAMES[rand() % NAMES.size()]);
|
||||
}
|
||||
// Obtiene el nombre final introducido
|
||||
std::string EnterName::getFinalName()
|
||||
{
|
||||
std::string EnterName::getFinalName() {
|
||||
auto name = trim(name_.substr(0, position_));
|
||||
if (name.empty())
|
||||
{
|
||||
if (name.empty()) {
|
||||
name = getRandomName();
|
||||
}
|
||||
name_ = name;
|
||||
|
||||
Reference in New Issue
Block a user