Twitter: raymondcamden

Address: Lafayette, LA, USA

Mimicking fixNewLine in ColdFusion Script

02-10-2014 3,373 views ColdFusion 9 Comments

A few days ago a reader asked me if there was a way to mimic the "fixNewLine" attribute in ColdFusion script. I thought he meant addNewLine, the feature that handles adding a new line character for you when you write text to a file. Turns out this is a feature I've never even heard of. From the docs:

fixNewLine: yes: changes embedded line-ending characters in string variables to operating-system specific line endings.

no: does not change embedded line-ending characters in string variables.

I checked and there is not a way to do this via fileWrite or FileWriteLine. I whipped up the following little function that should have the same behavior.

function fixNewLine(s) {
	var isWindows = contains "windows";
	if(isWindows) {
		return rereplace(s, "\r\n|\n|\r","#chr(13)##chr(10)#", "all");
	} else {
		return rereplace(s, "\r\n|\n|\r","#chr(10)#", "all");

The code is based on a StackOverflow answer (I credited the link in the code) and just does a simple regex on the string based on the operating system. You could also modify this to force a style instead of sniffing the OS.

Any thoughts on this? I did some basic testing and it seems to work OK. If folks like this I'll put it up on CFLib.


  • Commented on 02-10-2014 at 11:10 AM
    Here's a better version:

       function fixNewLine(s)
          return s.replaceAll('\r(?!\n)','\n').replaceAll('\r','')

    It removes all carriage returns in all situations, because they're a waste of space and rarely actually required. The sooner they are forgotten as a quirk of computing history and stop causing unnecessary extra code, the better.
  • Commented on 02-10-2014 at 11:22 AM
    Better being a point of view. ;) In my case, I'd disagree since the point was to replicate the behavior - right or wrong.
  • Commented on 02-10-2014 at 11:43 AM
    I've been thinking about your encouragement to post into ColdFusion UI, the right way. and one of the reasons I'm hesitating is because I have a unique point of view on how to write everything in both ColdFusion and JavaScript. I'm sure that if I were to post a "chapter" in ColdFusion UI the right way, both you and Adam would ask "Why do you do it that way?" and would proceed to correct it.

    I touched on this as a comment to an earlier blog post of yours, so the irony is not lost.
    I guess what I'm saying is that I think it's great that you and Adam are publishing "The right way" according to your point of view, and I'm watching that github project closely.

    For me to show an example, I would want to have control over the editing.
    So: I guess, maybe, I need to change my attitude?
  • Commented on 02-10-2014 at 11:49 AM
    Phillip, these are valid questions, but can I ask why you didn't post it on the blog entry? This comment is really OT here and I'd suggest you post it there.
  • Commented on 02-10-2014 at 11:51 AM
    Oh, it's because you said that "Better being a point of view" in this blog post, but sure, I will post it to the other.
  • Commented on 02-10-2014 at 1:52 PM
    How about:

    function fixNewLine(s) {
       return rereplace(s, "\r\n|\n|\r", (( contains "windows") ? chr(13) : "") & chr(10), "all");

    Would that work?
  • Commented on 02-10-2014 at 3:08 PM
    Yes, but only if you want to be fancy and shit. ;)
  • Commented on 02-15-2014 at 6:48 PM
    What about?

    ls = createObject("java", "java.lang.System").getProperty("line.separator");
  • Commented on 02-17-2014 at 8:56 PM
    That doesn't do what the UDF does - it just tells you what the separator is.

Post Reply

Please refrain from posting large blocks of code as a comment. Use Pastebin or Gists instead. Text wrapped in asterisks (*) will be bold and text wrapped in underscores (_) will be italicized.

Leave this field empty