File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change @@ -147,3 +147,34 @@ function findNewlineIndex(
147147
148148 return null ;
149149}
150+
151+ export function findDoubleNewlineIndex ( buffer : Uint8Array ) : number {
152+ // This function searches the buffer for the end patterns (\r\r, \n\n, \r\n\r\n)
153+ // and returns the index right after the first occurrence of any pattern,
154+ // or -1 if none of the patterns are found.
155+ const newline = 0x0a ; // \n
156+ const carriage = 0x0d ; // \r
157+
158+ for ( let i = 0 ; i < buffer . length - 1 ; i ++ ) {
159+ if ( buffer [ i ] === newline && buffer [ i + 1 ] === newline ) {
160+ // \n\n
161+ return i + 2 ;
162+ }
163+ if ( buffer [ i ] === carriage && buffer [ i + 1 ] === carriage ) {
164+ // \r\r
165+ return i + 2 ;
166+ }
167+ if (
168+ buffer [ i ] === carriage &&
169+ buffer [ i + 1 ] === newline &&
170+ i + 3 < buffer . length &&
171+ buffer [ i + 2 ] === carriage &&
172+ buffer [ i + 3 ] === newline
173+ ) {
174+ // \r\n\r\n
175+ return i + 4 ;
176+ }
177+ }
178+
179+ return - 1 ;
180+ }
You can’t perform that action at this time.
0 commit comments