Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
mpd
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Иван Мажукин
mpd
Commits
36ff9919
Commit
36ff9919
authored
Aug 07, 2014
by
Max Kellermann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
TextInputStream: move code to ReadBufferedLine()
Look at the buffer first, before trying to read from the file. This reduces overhead because we don't refill the buffer after every line.
parent
59d38f87
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
45 additions
and
34 deletions
+45
-34
TextInputStream.cxx
src/input/TextInputStream.cxx
+42
-34
TextInputStream.hxx
src/input/TextInputStream.hxx
+3
-0
No files found.
src/input/TextInputStream.cxx
View file @
36ff9919
...
@@ -28,51 +28,59 @@
...
@@ -28,51 +28,59 @@
#include <string.h>
#include <string.h>
char
*
char
*
TextInputStream
::
ReadLine
()
TextInputStream
::
Read
Buffered
Line
()
{
{
char
*
src
,
*
p
;
auto
r
=
buffer
.
Read
();
char
*
newline
=
reinterpret_cast
<
char
*>
(
memchr
(
r
.
data
,
'\n'
,
r
.
size
));
do
{
if
(
newline
==
nullptr
)
size_t
nbytes
;
return
nullptr
;
auto
dest
=
buffer
.
Write
();
if
(
dest
.
size
>=
2
)
{
/* reserve one byte for the null terminator if
the last line is not terminated by a
newline character */
--
dest
.
size
;
Error
error
;
buffer
.
Consume
(
newline
+
1
-
r
.
data
);
nbytes
=
is
.
LockRead
(
dest
.
data
,
dest
.
size
,
error
);
if
(
nbytes
>
0
)
buffer
.
Append
(
nbytes
);
else
if
(
error
.
IsDefined
())
{
LogError
(
error
);
return
nullptr
;
}
}
else
nbytes
=
0
;
auto
src_p
=
buffer
.
Read
();
char
*
end
=
StripRight
(
r
.
data
,
newline
);
if
(
src_p
.
IsEmpty
())
*
end
=
0
;
return
nullptr
;
return
r
.
data
;
}
src
=
src_p
.
data
;
char
*
TextInputStream
::
ReadLine
()
{
char
*
line
=
ReadBufferedLine
();
if
(
line
!=
nullptr
)
return
line
;
p
=
reinterpret_cast
<
char
*>
(
memchr
(
src
,
'\n'
,
src_p
.
size
));
while
(
true
)
{
if
(
p
==
nullptr
&&
nbytes
==
0
)
{
auto
dest
=
buffer
.
Write
();
if
(
dest
.
size
<
2
)
{
/* end of file (or line too long): terminate
/* end of file (or line too long): terminate
the current line */
the current line */
dest
=
buffer
.
Write
();
assert
(
!
dest
.
IsEmpty
());
assert
(
!
dest
.
IsEmpty
());
dest
[
0
]
=
0
;
dest
[
0
]
=
0
;
line
=
buffer
.
Read
().
data
;
buffer
.
Clear
();
buffer
.
Clear
();
return
src
;
return
line
;
}
}
}
while
(
p
==
nullptr
);
buffer
.
Consume
(
p
-
src
+
1
);
/* reserve one byte for the null terminator if the
last line is not terminated by a newline
character */
--
dest
.
size
;
char
*
end
=
StripRight
(
src
,
p
);
Error
error
;
*
end
=
0
;
size_t
nbytes
=
is
.
LockRead
(
dest
.
data
,
dest
.
size
,
error
);
return
src
;
if
(
nbytes
>
0
)
buffer
.
Append
(
nbytes
);
else
if
(
error
.
IsDefined
())
{
LogError
(
error
);
return
nullptr
;
}
line
=
ReadBufferedLine
();
if
(
line
!=
nullptr
)
return
line
;
if
(
nbytes
==
0
)
return
nullptr
;
}
}
}
src/input/TextInputStream.hxx
View file @
36ff9919
...
@@ -47,6 +47,9 @@ public:
...
@@ -47,6 +47,9 @@ public:
* @return a pointer to the line, or nullptr on end-of-file or error
* @return a pointer to the line, or nullptr on end-of-file or error
*/
*/
char
*
ReadLine
();
char
*
ReadLine
();
private
:
char
*
ReadBufferedLine
();
};
};
#endif
#endif
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment