@@ -77,7 +77,7 @@ defmodule File.Stream do
77
77
start_fun = fn ->
78
78
case :file . open ( path , read_modes ( modes ) ) do
79
79
{ :ok , device } ->
80
- if :trim_bom in modes , do: trim_bom ( device ) , else: device
80
+ if :trim_bom in modes , do: trim_bom ( device , raw ) |> elem ( 0 ) , else: device
81
81
82
82
{ :error , reason } ->
83
83
raise File.Error , reason: reason , action: "stream" , path: path
@@ -106,19 +106,24 @@ defmodule File.Stream do
106
106
end
107
107
end
108
108
109
- def count ( % { path: path , line_or_bytes: bytes } ) do
109
+ def count ( % { path: path , line_or_bytes: bytes , raw: true , modes: modes } ) do
110
110
case File . stat ( path ) do
111
111
{ :ok , % { size: 0 } } ->
112
112
{ :error , __MODULE__ }
113
113
114
114
{ :ok , % { size: size } } ->
115
- { :ok , div ( size , bytes ) + if ( rem ( size , bytes ) == 0 , do: 0 , else: 1 ) }
115
+ remainder = if rem ( size , bytes ) == 0 , do: 0 , else: 1
116
+ { :ok , div ( size , bytes ) + remainder - count_raw_bom ( path , modes ) }
116
117
117
118
{ :error , reason } ->
118
119
raise File.Error , reason: reason , action: "stream" , path: path
119
120
end
120
121
end
121
122
123
+ def count ( _stream ) do
124
+ { :error , __MODULE__ }
125
+ end
126
+
122
127
def member? ( _stream , _term ) do
123
128
{ :error , __MODULE__ }
124
129
end
@@ -127,10 +132,18 @@ defmodule File.Stream do
127
132
{ :error , __MODULE__ }
128
133
end
129
134
130
- defp trim_bom ( device ) do
131
- header = IO . binread ( device , 4 )
132
- { :ok , _new_pos } = :file . position ( device , bom_length ( header ) )
133
- device
135
+ defp count_raw_bom ( path , modes ) do
136
+ if :trim_bom in modes do
137
+ File . open! ( path , read_modes ( modes ) , & ( & 1 |> trim_bom ( true ) |> elem ( 1 ) ) )
138
+ else
139
+ 0
140
+ end
141
+ end
142
+
143
+ defp trim_bom ( device , raw ) do
144
+ header = if raw , do: IO . binread ( device , 4 ) , else: IO . read ( device , 1 )
145
+ { :ok , new_pos } = :file . position ( device , bom_length ( header ) )
146
+ { device , new_pos }
134
147
end
135
148
136
149
defp bom_length ( << 239 , 187 , 191 , _rest :: binary >> ) , do: 3
0 commit comments