<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://www.w3.org/Bugs/Public/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.4"
          urlbase="https://www.w3.org/Bugs/Public/"
          
          maintainer="sysbot+bugzilla@w3.org"
>

    <bug>
          <bug_id>23155</bug_id>
          
          <creation_ts>2013-09-04 16:32:35 +0000</creation_ts>
          <short_desc>Algorithm for BOM handling in TextDecoder.decode can be simplified</short_desc>
          <delta_ts>2014-03-28 11:21:41 +0000</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>WHATWG</product>
          <component>Encoding</component>
          <version>unspecified</version>
          <rep_platform>All</rep_platform>
          <op_sys>All</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>FIXED</resolution>
          
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords></keywords>
          <priority>P2</priority>
          <bug_severity>normal</bug_severity>
          <target_milestone>Unsorted</target_milestone>
          <dependson>16688</dependson>
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Joshua Bell">jsbell</reporter>
          <assigned_to name="Anne">annevk</assigned_to>
          <cc>mike</cc>
          
          <qa_contact>sideshowbarker+encodingspec</qa_contact>

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>92989</commentid>
    <comment_count>0</comment_count>
    <who name="Joshua Bell">jsbell</who>
    <bug_when>2013-09-04 16:32:35 +0000</bug_when>
    <thetext>The BOM handling algorithm is specified as removing lead bytes from the stream before invoking the encoder&apos;s decode algorithm. (Currently step 4)

This can be specified in an alternate fashion: after running the encoding&apos;s decoder algorithm: if BOM seen flag is unset, and the output stream is non-empty, set the BOM seen flag; if encoding is one of utf-8, utf-16le, utf-16be and the first code point of the output stream is U+FEFF then remove it from the stream.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>92990</commentid>
    <comment_count>1</comment_count>
    <who name="Joshua Bell">jsbell</who>
    <bug_when>2013-09-04 16:35:11 +0000</bug_when>
    <thetext>For symmetry with section 6 it may be better to leave it as-is, but I thought I&apos;d point it out.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>93003</commentid>
    <comment_count>2</comment_count>
    <who name="Anne">annevk</who>
    <bug_when>2013-09-04 20:08:02 +0000</bug_when>
    <thetext>That&apos;d require we clearly define the output stream.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>102939</commentid>
    <comment_count>3</comment_count>
    <who name="Anne">annevk</who>
    <bug_when>2014-03-26 18:36:37 +0000</bug_when>
    <thetext>Fixed as part of bug 16688.

https://github.com/whatwg/encoding/commit/dc8e4c10c9b4a91f188f3145c2e31ddec4d52a78

This is a massive change, review appreciated!</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>102941</commentid>
    <comment_count>4</comment_count>
    <who name="Joshua Bell">jsbell</who>
    <bug_when>2014-03-26 18:55:01 +0000</bug_when>
    <thetext>In 6 Decode and encode, step 3: if buffer doesn&apos;t match any lines in the byte-order table, buffer should be prepended to stream

Prior to that revision, this was handled by updating an offset only if the BOM matched.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>102942</commentid>
    <comment_count>5</comment_count>
    <who name="Anne">annevk</who>
    <bug_when>2014-03-26 18:59:44 +0000</bug_when>
    <thetext>We no longer have offsets or code point pointers. You can only read from a stream and put stuff back to it. There&apos;s no peek or skip... At least in the specification abstractions, you can implement them however you want of course.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>102943</commentid>
    <comment_count>6</comment_count>
    <who name="Joshua Bell">jsbell</who>
    <bug_when>2014-03-26 19:26:51 +0000</bug_when>
    <thetext>(In reply to Anne from comment #5)
&gt; We no longer have offsets or code point pointers. You can only read from a
&gt; stream and put stuff back to it. There&apos;s no peek or skip... At least in the
&gt; specification abstractions, you can implement them however you want of
&gt; course.

Right....

So step 2 reads 3 bytes from the stream. Step 3 checks if they match the BOM. If they don&apos;t match the BOM, as written those 3 bytes are dropped on the floor and unavailable in step 4.

So step 3 needs to also say the equivalent of &quot;otherwise, prepend buffer to stream&quot;</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>102996</commentid>
    <comment_count>7</comment_count>
    <who name="Anne">annevk</who>
    <bug_when>2014-03-27 12:24:44 +0000</bug_when>
    <thetext>My bad, fixed.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>103027</commentid>
    <comment_count>8</comment_count>
    <who name="Joshua Bell">jsbell</who>
    <bug_when>2014-03-27 16:47:13 +0000</bug_when>
    <thetext>(In reply to Anne from comment #7)
&gt; My bad, fixed.

That fix lgtm.

Another edge case: if stream is 0xFF 0xFE (that is, exactly two bytes), then step 3 reads two bytes then end-of-stream so buffer ends up being [ 0xFF, 0xFE ]. Step 4 matches utf-16le and BOM seen is set. Step 6 will then prepend the last byte of buffer to stream - so stream ends up as 0xFE. Oops.

I suppose we say that buffer ends up being [ 0xFF, 0xFE, end-of-stream ], so end-of-stream token gets prepended to stream?!?</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>103080</commentid>
    <comment_count>9</comment_count>
    <who name="Anne">annevk</who>
    <bug_when>2014-03-28 11:21:41 +0000</bug_when>
    <thetext>https://github.com/whatwg/encoding/commit/b891a81b34d17ae4da01eb4cad074d09bf843e09</thetext>
  </long_desc>
      
      

    </bug>

</bugzilla>