Merge pull request #210 from nathan/import-newlines

Fixed importing files with CRLF or CR line endings
This commit is contained in:
Shane M. Clements 2014-06-03 13:51:49 -06:00
commit 57ffd680df

View file

@ -140,9 +140,7 @@ public class ListWatcher extends Sprite {
}
function fileLoadHandler(event:Event):void {
var s:String = file.data.readUTFBytes(file.data.length);
var delimiter:String = '\n';
if (s.indexOf(delimiter) < 0) delimiter = '\r';
importLines(removeTrailingEmptyLines(s.split(delimiter)));
importLines(removeTrailingEmptyLines(s.split(/\r\n|[\r\n]/)));
}
var fileList:FileReferenceList = new FileReferenceList();
var file:FileReference;
@ -155,9 +153,7 @@ public class ListWatcher extends Sprite {
private function exportList():void {
var file:FileReference = new FileReference();
var s:String = '';
for each (var el:* in contents) s += el + '\n';
if (s.length > 0) s = s.slice(0, s.length - 1); // remove final '\n'
var s:String = contents.join('\n') + '\n';
file.save(s, listName + '.txt');
}
@ -171,9 +167,8 @@ public class ListWatcher extends Sprite {
//------------------------------
private function removeTrailingEmptyLines(lines:Array):Array {
var i:int = lines.length - 1;
while ((i > 0) && (lines[i].length == 0)) i--;
return lines.slice(0, i + 1);
while (lines.length && !lines[lines.length - 1]) lines.pop();
return lines;
}
private function importLines(lines:Array):void {