floatybox

Float your way through perilous terrain in this endless side-scoller game.


floatybox

/

src

/

ob

/

string.cc

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
/*
                                    88888888
                                  888888888888
                                 88888888888888
                                8888888888888888
                               888888888888888888
                              888888  8888  888888
                              88888    88    88888
                              888888  8888  888888
                              88888888888888888888
                              88888888888888888888
                             8888888888888888888888
                          8888888888888888888888888888
                        88888888888888888888888888888888
                              88888888888888888888
                            888888888888888888888888
                           888888  8888888888  888888
                           888     8888  8888     888
                                   888    888

                                   OCTOBANANA

Licensed under the MIT License

Copyright (c) 2019 Brett Robinson <https://octobanana.com/>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#include "ob/string.hh"

#include <cstddef>

#include <string>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string_view>
#include <vector>
#include <limits>
#include <utility>
#include <optional>
#include <regex>

namespace OB::String
{

std::string plural(std::string const& str, std::size_t num)
{
  if (num == 1)
  {
    return str;
  }

  return str + "s";
}

std::string plural(std::string const& str, std::string const& end, std::size_t num)
{
  if (num == 1)
  {
    return str;
  }

  return str + end;
}

std::string file(std::string const& str)
{
  std::ifstream file {str};
  file.seekg(0, std::ios::end);
  std::size_t size (static_cast<std::size_t>(file.tellg()));
  std::string content (size, ' ');
  file.seekg(0);
  file.read(&content[0], static_cast<std::streamsize>(size));

  return content;
}

std::string replace(std::string str, std::string const& key, std::string const& val,
  std::size_t size)
{
  std::size_t pos {0};

  while (size-- > 0)
  {
    pos = str.find(key, pos);

    if (pos == std::string::npos)
    {
      break;
    }

    str.replace(pos, key.size(), val);
    pos += val.size();
  }

  return str;
}

std::pair<std::string, std::size_t> replace(std::string str, std::regex const& rx, std::function<std::string(std::smatch const&)> fn, std::size_t size)
{
  std::size_t pos {0};
  std::size_t count {0};
  std::string tmp {str};
  std::string val;
  std::smatch match;

  while ((size-- > 0) && std::regex_search(tmp, match, rx, std::regex_constants::match_not_null))
  {
    ++count;
    pos += match.prefix().str().size();
    val = fn(match);
    str.replace(pos, match.str().size(), val);
    pos += val.size();
    tmp = match.suffix();
  }

  return {str, count};
}

std::string format(std::string str, std::unordered_map<std::string, std::string> args)
{
  if (args.empty())
  {
    return str;
  }

  std::string res = str;
  std::size_t pos {0};
  std::smatch match;
  std::regex const rx {"\\{([^\\:]+?)\\}"};

  while (std::regex_search(res, match, rx, std::regex_constants::match_not_null))
  {
    std::string m {match[0]};
    std::string arg_pos {match[1]};
    pos += static_cast<std::size_t>(match.prefix().length());

    if (args.find(arg_pos) == args.end())
    {
      ++pos;
      res = res.substr(std::string(match.prefix()).size() + 1);

      continue;
    }

    str.replace(pos, m.size(), args[arg_pos]);
    pos += args[arg_pos].size();
    res = match.suffix();
  }

  return str;
}

std::string xformat(std::string str, std::unordered_map<std::string, std::string> args)
{
  if (args.empty())
  {
    return str;
  }

  std::string res = str;
  std::size_t pos {0};
  std::smatch match;
  std::regex const rx {"\\{(\\w+)(?:(:[^\\r]*?:\\1)?)\\}"};

  while (std::regex_search(res, match, rx, std::regex_constants::match_not_null))
  {
    bool is_simple {true};
    std::string m {match[0]};
    std::string first {match[1]};
    std::string second {match[2]};
    pos += std::string(match.prefix()).size();

    if (second.size() > 0)
    {
      is_simple = false;
    }

    if (args.find(first) == args.end())
    {
      pos += m.size();
      res = match.suffix();

      continue;
    }

    if (is_simple)
    {
      str.replace(pos, m.size(), args[first]);
      pos += args[first].size();
      res = match.suffix();
    }
    else
    {
      std::smatch match_complex;
      std::regex rx2 {"^:([*]{1})(.+)([a-z0-9]{1}):([^\\r]+):" + first + "$"};

      if (std::regex_match(second, match_complex, rx2, std::regex_constants::match_not_null))
      {
        std::string type {match_complex[1]};
        std::string delim {match_complex[2]};
        std::string index_char {match_complex[3]};
        std::string rstr {match_complex[4]};

        auto vec = String::split(args[first], delim);
        std::stringstream ss;

        for (auto const& e : vec)
        {
          ss << String::replace(rstr, "[" + index_char + "]", e);
        }

        auto nstr = String::xformat(ss.str(), args);
        str.replace(pos, m.size(), nstr);
        pos += nstr.size();
        res = match.suffix();
      }
      else
      {
        pos += m.size();
        res = match.suffix();
      }
    }
  }

  return str;
}

std::string to_string(double const val, int const pre)
{
  std::stringstream ss;
  ss << std::fixed << std::setprecision(pre) << val;

  return ss.str();
}

std::vector<std::string> split(std::string const& str,
  std::string const& delim, std::size_t size)
{
  std::vector<std::string> vtok;
  std::size_t start {0};
  auto end = str.find(delim);

  while ((size-- > 0) && (end != std::string::npos))
  {
    vtok.emplace_back(str.substr(start, end - start));
    start = end + delim.size();
    end = str.find(delim, start);
  }

  vtok.emplace_back(str.substr(start, end));

  return vtok;
}

std::vector<std::string_view> split_view(std::string_view str,
  std::string_view delim, std::size_t size)
{
  std::vector<std::string_view> vtok;
  std::size_t start {0};
  auto end = str.find(delim);

  while ((size-- > 0) && (end != std::string_view::npos))
  {
    vtok.emplace_back(str.data() + start, end - start);
    start = end + delim.size();
    end = str.find(delim, start);
  }

  vtok.emplace_back(str.data() + start, str.size() - start);

  return vtok;
}

std::string uppercase(std::string const& str)
{
  auto const to_upper = [](char& c)
  {
    if (c >= 'a' && c <= 'z')
    {
      c += 'A' - 'a';
    }

    return c;
  };

  std::string res {str};

  for (char& c : res)
  {
    c = to_upper(c);
  }

  return res;
}

std::string lowercase(std::string const& str)
{
  auto const to_lower = [](char& c)
  {
    if (c >= 'A' && c <= 'Z')
    {
      c += 'a' - 'A';
    }

    return c;
  };

  std::string res {str};

  for (char& c : res)
  {
    c = to_lower(c);
  }

  return res;
}

std::string trim(std::string str)
{
  auto start = str.find_first_not_of(" \t\n\r\f\v");

  if (start != std::string::npos)
  {
    auto end = str.find_last_not_of(" \t\n\r\f\v");
    str = str.substr(start, end - start + 1);

    return str;
  }

  return {};
}

bool assert_rx(std::string const& str, std::regex rx)
{
  std::smatch m;

  if (std::regex_match(str, m, rx, std::regex_constants::match_not_null))
  {
    return true;
  }

  return false;
}

std::optional<std::vector<std::string>> match(std::string const& str, std::regex rx)
{
  std::smatch m;

  if (std::regex_match(str, m, rx, std::regex_constants::match_not_null))
  {
    std::vector<std::string> v;

    for (auto const& e : m)
    {
      v.emplace_back(std::string(e));
    }

    return v;
  }

  return {};
}

std::string repeat(std::size_t const num, std::string const& str)
{
  if (num == 0)
  {
    return {};
  }

  if (num == 1)
  {
    return str;
  }

  std::string res;
  res.reserve(str.size() * num);

  for (std::size_t i {0}; i < num; ++i)
  {
    res += str;
  }

  return res;
}

bool starts_with(std::string const& str, std::string const& val)
{
  if (str.empty() || str.size() < val.size())
  {
    return false;
  }

  if (str.compare(0, val.size(), val) == 0)
  {
    return true;
  }

  return false;
}

std::string escape(std::string str)
{
  for (std::size_t pos = 0; (pos = str.find_first_of("\n\t\r\a\b\f\v\"\'\?", pos)); ++pos)
  {
    if (pos == std::string::npos)
    {
      break;
    }

    switch (str.at(pos))
    {
      case '\n':
      {
        str.replace(pos++, 1, "\\n");
        break;
      }
      case '\t':
      {
        str.replace(pos++, 1, "\\t");
        break;
      }
      case '\r':
      {
        str.replace(pos++, 1, "\\r");
        break;
      }
      case '\a':
      {
        str.replace(pos++, 1, "\\a");
        break;
      }
      case '\b':
      {
        str.replace(pos++, 1, "\\b");
        break;
      }
      case '\f':
      {
        str.replace(pos++, 1, "\\f");
        break;
      }
      case '\v':
      {
        str.replace(pos++, 1, "\\v");
        break;
      }
      case '\?':
      {
        str.replace(pos++, 1, "\\?");
        break;
      }
      case '\'':
      {
        str.replace(pos++, 1, "\\'");
        break;
      }
      case '"':
      {
        str.replace(pos++, 1, "\\\"");
        break;
      }
      default:
      {
        break;
      }
    }
  }

  return str;
}

std::string unescape(std::string str)
{
  for (std::size_t pos = 0; (pos = str.find("\\", pos)); ++pos)
  {
    if (pos == std::string::npos || pos + 1 == std::string::npos)
    {
      break;
    }

    switch (str.at(pos + 1))
    {
      case 'n':
      {
        str.replace(pos, 2, "\n");
        break;
      }
      case 't':
      {
        str.replace(pos, 2, "\t");
        break;
      }
      case 'r':
      {
        str.replace(pos, 2, "\r");
        break;
      }
      case 'a':
      {
        str.replace(pos, 2, "\a");
        break;
      }
      case 'b':
      {
        str.replace(pos, 2, "\b");
        break;
      }
      case 'f':
      {
        str.replace(pos, 2, "\f");
        break;
      }
      case 'v':
      {
        str.replace(pos, 2, "\v");
        break;
      }
      case '?':
      {
        str.replace(pos, 2, "\?");
        break;
      }
      case '\'':
      {
        str.replace(pos, 2, "'");
        break;
      }
      case '"':
      {
        str.replace(pos, 2, "\"");
        break;
      }
      default:
      {
        break;
      }
    }
  }

  return str;
}

std::size_t count(std::string const& str, std::string const& val)
{
  std::size_t pos {0};
  std::size_t count {0};

  for (;;)
  {
    pos = str.find(val, pos);

    if (pos == std::string::npos)
    {
      break;
    }

    ++count;
    ++pos;
  }

  return count;
}

std::size_t damerau_levenshtein(std::string const& lhs, std::string const& rhs,
  std::size_t const weight_insert, std::size_t const weight_substitute,
  std::size_t const weight_delete, std::size_t const weight_transpose)
{
  if (lhs == rhs)
  {
    return 0;
  }

  std::string_view lhsv {lhs};
  std::string_view rhsv {rhs};

  bool swapped {false};
  if (lhsv.size() > rhsv.size())
  {
    swapped = true;
    std::swap(lhsv, rhsv);
  }

  for (std::size_t i = 0; i < lhsv.size(); ++i)
  {
    if (lhsv.at(i) != rhsv.at(i))
    {
      if (i)
      {
        lhsv.substr(i);
        rhsv.substr(i);
      }

      break;
    }
  }

  for (std::size_t i = 0; i < lhsv.size(); ++i)
  {
    if (lhsv.at(lhsv.size() - 1 - i) != rhsv.at(rhsv.size() - 1 - i))
    {
      if (i)
      {
        lhsv.substr(0, lhsv.size() - 1 - i);
        rhsv.substr(0, rhsv.size() - 1 - i);
      }

      break;
    }
  }

  if (swapped)
  {
    std::swap(lhsv, rhsv);
  }

  if (lhsv.empty())
  {
    return rhsv.size() * weight_insert;
  }

  if (rhsv.empty())
  {
    return lhsv.size() * weight_delete;
  }

  std::vector<std::size_t> v0 (rhsv.size() + 1, 0);
  std::vector<std::size_t> v1 (rhsv.size() + 1, 0);
  std::vector<std::size_t> v2 (rhsv.size() + 1, 0);

  for (std::size_t i = 0; i <= rhsv.size(); ++i)
  {
    v1.at(i) = i * weight_insert;
  }

  for (std::size_t i = 0; i < lhsv.size(); ++i)
  {
    v2.at(0) = (i + 1) * weight_delete;
    for (std::size_t j = 0; j < rhsv.size(); j++)
    {
      v2.at(j + 1) = std::min(
        // deletion
        v1.at(j + 1) + weight_delete,
        std::min(
          // insertion
          v2.at(j) + weight_insert,
          // substitution
          v1.at(j) + (weight_substitute * (lhsv.at(i) != rhsv.at(j)))));

      if (i > 0 && j > 0 &&
        (lhsv.at(i - 1) == rhsv.at(j)) &&
        (lhsv.at(i) == rhsv.at(j - 1)))
      {
        v2.at(j + 1) = std::min(
          v0.at(j + 1),
          // transposition
          v0.at(j - 1) + weight_transpose);
      }
    }

    std::swap(v0, v1);
    std::swap(v1, v2);
  }

  return v1.at(rhsv.size());
}

} // namespace OB::String
Back to Top