toUpperLatinOnly method

String toUpperLatinOnly()

Converts only the Latin alphabetic characters (a-z) in the string to uppercase, leaving other characters unchanged.

This method iterates through the string and converts any Latin lowercase letters to uppercase, while leaving numbers, symbols, whitespace, and non-Latin characters untouched.

Returns: A new string with Latin alphabetic characters converted to uppercase.

Example:

'latincafé123'.toUpperLatinOnly(); // Returns 'LATINcafé123' (only 'latin' part is uppercased)
'UPPERCASE'.toUpperLatinOnly();    // Returns 'UPPERCASE' (already uppercase)
''.toUpperLatinOnly();           // Returns ''

Implementation

// cspell: ignore latincafé123 uppercased
/// Example:
/// ```dart
/// 'latincafé123'.toUpperLatinOnly(); // Returns 'LATINcafé123' (only 'latin' part is uppercased)
/// 'UPPERCASE'.toUpperLatinOnly();    // Returns 'UPPERCASE' (already uppercase)
/// ''.toUpperLatinOnly();           // Returns ''
/// ```
String toUpperLatinOnly() {
  String result = '';

  final int codeUnitA = 'a'.codeUnitAt(0);
  final int codeUnitZ = 'z'.codeUnitAt(0);

  // don't iterate over runes because unicode / emoji == multiple runes
  for (int i = 0; i < length; i++) {
    final String s = this[i];
    if (s.codeUnitAt(0) >= codeUnitA && s.codeUnitAt(0) <= codeUnitZ) {
      result += s.toUpperCase();
    } else {
      result += s;
    }
  }

  // NOTE: If you want toUpperLatinOnly() to perform a more visually "correct" uppercasing of
  // accented Latin characters (transforming 'é' to 'É', 'à' to 'À', etc.), then you'll need to
  // modify the toUpperLatinOnly() method to include special handling for these characters. This
  // would likely involve:
  //
  // Creating a mapping of lowercase accented Latin characters to their uppercase counterparts.
  //You would need to identify the specific accented characters you want to handle.
  //
  // Checking for these lowercase accented characters in your loop.
  //
  // Replacing them with their uppercase equivalents from your mapping if found.
  //
  // For other characters, keep the existing toUpperCase() logic.
  return result;
}