2
0

string.vala 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. * Copyright (c) 2012-2026 Daniele Bartolini et al.
  3. * SPDX-License-Identifier: GPL-3.0-or-later
  4. */
  5. namespace Crown
  6. {
  7. public static string print_max_decimals(double num, int max_decimals)
  8. {
  9. string formatted = "%.*f".printf(max_decimals, num);
  10. int len = formatted.length;
  11. if (max_decimals > 0) {
  12. // Trim trailing zeroes.
  13. while (len > 0 && formatted[len - 1] == '0')
  14. len--;
  15. // Remove trailing decimal point, if any.
  16. if (len > 0 && formatted[len - 1] == '.')
  17. len--;
  18. return formatted.substring(0, len);
  19. }
  20. // Strip the decimal point and anything after.
  21. int dot = formatted.index_of_char('.');
  22. return (dot >= 0) ? formatted.substring(0, dot) : formatted;
  23. }
  24. public static string camel_case(string str)
  25. {
  26. int len = str.length;
  27. GLib.StringBuilder sb = new GLib.StringBuilder.sized(len);
  28. bool capitalize = true;
  29. for (int i = 0; i < len; i++) {
  30. char c = str[i];
  31. if (c.isalnum()) {
  32. sb.append_c(capitalize ? c.toupper() : c.tolower());
  33. capitalize = false;
  34. } else {
  35. if (sb.len > 0 && sb.str[sb.len - 1] != ' ')
  36. sb.append_c(' ');
  37. capitalize = true;
  38. }
  39. }
  40. return sb.str;
  41. }
  42. } /* namespace Crown */