rStripHeader function
- String code
From the supplied code
(an R script file contents) strip the initial
copyright message from the script, keeping the first line as the script
title, and keeping all remaining lines of the script file.
Implementation
String rStripHeader(String code) {
// Keep the first line then strip everything down to the first line not
// starting with a hash.
List<String> lines = code.split('\n');
// Find the index of the first line that doesn't start with '#'
int index = 0;
while (index < lines.length && lines[index].trim().startsWith('#')) {
index++;
}
// Join the lines. For a single line the sublist processing duplicates the
// line prefixing it with #. So handle the sinle line edge case specially.
// TODO 20231102 gjw ACTUALLY I THINK THE PROBLEM IS WHEN THE FIRST LINE DOES
// NOT START WITH #. SHOULD TEST AND FIX THAT ONE UP.
String result = '\n${lines.first}';
if (lines.length > 1) {
result = "$result\n#${lines.sublist(index).join('\n')}";
}
return result;
}