<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:media="http://search.yahoo.com/mrss/"
>

<channel>
	<title>isNewline &#8211; Wade Tregaskis</title>
	<atom:link href="https://wadetregaskis.com/tags/isnewline/feed/" rel="self" type="application/rss+xml" />
	<link>https://wadetregaskis.com</link>
	<description></description>
	<lastBuildDate>Tue, 17 Mar 2026 01:43:51 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://wadetregaskis.com/wp-content/uploads/2016/03/Stitch-512x512-1-256x256.png</url>
	<title>isNewline &#8211; Wade Tregaskis</title>
	<link>https://wadetregaskis.com</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">226351702</site>	<item>
		<title>&#8220;\r\n&#8221; is one Character in Swift</title>
		<link>https://wadetregaskis.com/rn-is-one-character-in-swift/</link>
					<comments>https://wadetregaskis.com/rn-is-one-character-in-swift/#respond</comments>
		
		<dc:creator><![CDATA[]]></dc:creator>
		<pubDate>Tue, 17 Mar 2026 01:43:49 +0000</pubDate>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[isNewline]]></category>
		<category><![CDATA[Swift]]></category>
		<category><![CDATA[Unicode]]></category>
		<guid isPermaLink="false">https://wadetregaskis.com/?p=8790</guid>

					<description><![CDATA[From the department of “how did I not realise this sooner?!”: Yes, Swift treats the two bytes &#8220;\r\n&#8221; as a single character.  This is actually super convenient a lot of the time, because it means algorithms that look for line breaks with isNewline just work, even on “Windows”-style text.  Otherwise, you’d have to explicitly look&#8230; <a class="read-more-link" href="https://wadetregaskis.com/rn-is-one-character-in-swift/" data-wpel-link="internal">Read more</a>]]></description>
										<content:encoded><![CDATA[
<p>From the department of “how did I not realise this sooner?!”:</p>



<div class="wp-block-kevinbatdorf-code-block-pro padding-disabled" data-code-block-pro-font-family="" style="font-size:.875rem;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><pre class="shiki light-plus" style="background-color: #FFFFFF" tabindex="0"><code><span class="line"><span style="color: #000000">  </span><span style="color: #098658">1</span><span style="color: #000000">&gt; </span><span style="color: #A31515">&quot;</span><span style="color: #EE0000">\r</span><span style="color: #A31515">&quot;</span><span style="color: #000000">.</span><span style="color: #001080">count</span></span>
<span class="line"><span style="color: #000000">$R0: </span><span style="color: #267F99">Int</span><span style="color: #000000"> = </span><span style="color: #098658">1</span></span>
<span class="line"><span style="color: #000000">  </span><span style="color: #098658">2</span><span style="color: #000000">&gt; </span><span style="color: #A31515">&quot;</span><span style="color: #EE0000">\n</span><span style="color: #A31515">&quot;</span><span style="color: #000000">.</span><span style="color: #001080">count</span><span style="color: #000000"> </span></span>
<span class="line"><span style="color: #000000">$R1: </span><span style="color: #267F99">Int</span><span style="color: #000000"> = </span><span style="color: #098658">1</span></span>
<span class="line"><span style="color: #000000">  </span><span style="color: #098658">3</span><span style="color: #000000">&gt; </span><span style="color: #A31515">&quot;</span><span style="color: #EE0000">\r\n</span><span style="color: #A31515">&quot;</span><span style="color: #000000">.</span><span style="color: #001080">count</span><span style="color: #000000"> </span></span>
<span class="line"><span style="color: #000000">$R2: </span><span style="color: #267F99">Int</span><span style="color: #000000"> = </span><span style="color: #098658">1</span></span></code></pre></div>



<p>Yes, Swift treats the two bytes &#8220;\r\n&#8221; as a <em>single</em> character.  This is actually super convenient a lot of the time, because it means algorithms that look for line breaks with <code>isNewline</code> just work, even on “Windows”-style text.  Otherwise, you’d have to explicitly look for two <code>isNewline</code> characters in sequence <em>and</em> check if they are exactly &#8220;\r\n&#8221;.</p>



<p>But it does lead to some potentially surprising side-effects, like:</p>



<div class="wp-block-kevinbatdorf-code-block-pro padding-disabled" data-code-block-pro-font-family="" style="font-size:.875rem;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><pre class="shiki light-plus" style="background-color: #FFFFFF" tabindex="0"><code><span class="line"><span style="color: #000000"> </span><span style="color: #098658">4</span><span style="color: #000000">&gt; x.</span><span style="color: #001080">unicodeScalars</span><span style="color: #000000">.</span><span style="color: #001080">count</span></span>
<span class="line"><span style="color: #000000">$R3: </span><span style="color: #267F99">Int</span><span style="color: #000000"> = </span><span style="color: #098658">2</span></span>
<span class="line"><span style="color: #000000"> </span><span style="color: #098658">5</span><span style="color: #000000">&gt; </span><span style="color: #267F99">Array</span><span style="color: #000000">(x.</span><span style="color: #001080">unicodeScalars</span><span style="color: #000000">)</span></span>
<span class="line"><span style="color: #000000">$R4: &#91;</span><span style="color: #267F99">String</span><span style="color: #000000">.</span><span style="color: #267F99">UnicodeScalarView</span><span style="color: #000000">.</span><span style="color: #267F99">Element</span><span style="color: #000000">&#93; = </span><span style="color: #098658">2</span><span style="color: #000000"> values {</span></span>
<span class="line"><span style="color: #000000">  &#91;</span><span style="color: #098658">0</span><span style="color: #000000">&#93; = U&#39;\r&#39;</span></span>
<span class="line"><span style="color: #000000">  &#91;</span><span style="color: #098658">1</span><span style="color: #000000">&#93; = U&#39;\n&#39;</span></span>
<span class="line"><span style="color: #000000">}</span></span></code></pre></div>



<p>This isn’t surprising if you’re pretty familiar with how Unicode actually works &#8211; starting with the difference between graphemes (approximately what Swift calls a Character) and &#8220;scalars&#8221;, but I suspect it’ll catch some people out.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://wadetregaskis.com/rn-is-one-character-in-swift/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">8790</post-id>	</item>
	</channel>
</rss>
