List<String> (String text) { int startLine = 0; int i = 0; int n = text.length(); ArrayList<String> rc = new ArrayList<>(); while (i < n) { switch (text.charAt(i)) { case '\n' -> { i++; if (i < n && text.charAt(i) == '\r') { i++; } rc.add(text.substring(startLine, i)); startLine = i; } case '\r' -> { i++; if (i < n && text.charAt(i) == '\n') { i++; } rc.add(text.substring(startLine, i)); startLine = i; } default -> i++; } } if (startLine == text.length()) { // still add empty line or previous line wouldn't be treated as completed rc.add(""); } else { rc.add(text.substring(startLine, i)); } return rc; }