mirror of
https://github.com/simonask/libyaml-safer
synced 2024-11-26 05:10:20 +00:00
Convert yaml_emitter_flush() to member function
This commit is contained in:
parent
fec143f0c0
commit
c83b6835d8
3 changed files with 52 additions and 60 deletions
|
@ -6,8 +6,8 @@ use crate::macros::{
|
||||||
is_alpha, is_ascii, is_blank, is_blankz, is_bom, is_break, is_breakz, is_printable, is_space,
|
is_alpha, is_ascii, is_blank, is_blankz, is_bom, is_break, is_breakz, is_printable, is_space,
|
||||||
};
|
};
|
||||||
use crate::{
|
use crate::{
|
||||||
yaml_emitter_flush, Break, EmitterError, Encoding, Event, EventData, MappingStyle, ScalarStyle,
|
Break, EmitterError, Encoding, Event, EventData, MappingStyle, ScalarStyle, SequenceStyle,
|
||||||
SequenceStyle, TagDirective, VersionDirective, WriterError, OUTPUT_BUFFER_SIZE,
|
TagDirective, VersionDirective, WriterError, OUTPUT_BUFFER_SIZE,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// The emitter structure.
|
/// The emitter structure.
|
||||||
|
@ -92,7 +92,7 @@ fn FLUSH(emitter: &mut Emitter) -> Result<(), WriterError> {
|
||||||
if emitter.buffer.len() < OUTPUT_BUFFER_SIZE - 5 {
|
if emitter.buffer.len() < OUTPUT_BUFFER_SIZE - 5 {
|
||||||
Ok(())
|
Ok(())
|
||||||
} else {
|
} else {
|
||||||
yaml_emitter_flush(emitter)
|
emitter.flush()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -606,7 +606,7 @@ impl<'w> Emitter<'w> {
|
||||||
self.open_ended = 0;
|
self.open_ended = 0;
|
||||||
self.write_indent()?;
|
self.write_indent()?;
|
||||||
}
|
}
|
||||||
yaml_emitter_flush(self)?;
|
self.flush()?;
|
||||||
self.state = EmitterState::End;
|
self.state = EmitterState::End;
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
@ -634,7 +634,7 @@ impl<'w> Emitter<'w> {
|
||||||
} else if self.open_ended == 0 {
|
} else if self.open_ended == 0 {
|
||||||
self.open_ended = 1;
|
self.open_ended = 1;
|
||||||
}
|
}
|
||||||
yaml_emitter_flush(self)?;
|
self.flush()?;
|
||||||
self.state = EmitterState::DocumentStart;
|
self.state = EmitterState::DocumentStart;
|
||||||
self.tag_directives.clear();
|
self.tag_directives.clear();
|
||||||
return Ok(());
|
return Ok(());
|
||||||
|
@ -1794,6 +1794,53 @@ impl<'w> Emitter<'w> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Flush the accumulated characters to the output.
|
||||||
|
pub fn flush(&mut self) -> Result<(), WriterError> {
|
||||||
|
assert!((self.write_handler).is_some());
|
||||||
|
assert_ne!(self.encoding, Encoding::Any);
|
||||||
|
|
||||||
|
if self.buffer.is_empty() {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
if self.encoding == Encoding::Utf8 {
|
||||||
|
let to_emit = self.buffer.as_bytes();
|
||||||
|
self.write_handler
|
||||||
|
.as_mut()
|
||||||
|
.expect("non-null writer")
|
||||||
|
.write_all(to_emit)?;
|
||||||
|
self.buffer.clear();
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
let big_endian = match self.encoding {
|
||||||
|
Encoding::Any | Encoding::Utf8 => {
|
||||||
|
unreachable!("unhandled encoding")
|
||||||
|
}
|
||||||
|
Encoding::Utf16Le => false,
|
||||||
|
Encoding::Utf16Be => true,
|
||||||
|
};
|
||||||
|
|
||||||
|
for ch in self.buffer.encode_utf16() {
|
||||||
|
let bytes = if big_endian {
|
||||||
|
ch.to_be_bytes()
|
||||||
|
} else {
|
||||||
|
ch.to_le_bytes()
|
||||||
|
};
|
||||||
|
self.raw_buffer.extend(bytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
let to_emit = self.raw_buffer.as_slice();
|
||||||
|
|
||||||
|
self.write_handler
|
||||||
|
.as_mut()
|
||||||
|
.expect("non-null function pointer")
|
||||||
|
.write_all(to_emit)?;
|
||||||
|
self.buffer.clear();
|
||||||
|
self.raw_buffer.clear();
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn reset_anchors(&mut self) {
|
pub(crate) fn reset_anchors(&mut self) {
|
||||||
self.anchors.clear();
|
self.anchors.clear();
|
||||||
self.last_anchor_id = 0;
|
self.last_anchor_id = 0;
|
||||||
|
|
|
@ -35,7 +35,6 @@ mod parser;
|
||||||
mod reader;
|
mod reader;
|
||||||
mod scanner;
|
mod scanner;
|
||||||
mod token;
|
mod token;
|
||||||
mod writer;
|
|
||||||
|
|
||||||
pub use crate::document::*;
|
pub use crate::document::*;
|
||||||
pub use crate::emitter::*;
|
pub use crate::emitter::*;
|
||||||
|
@ -44,7 +43,6 @@ pub use crate::event::*;
|
||||||
pub use crate::parser::*;
|
pub use crate::parser::*;
|
||||||
pub use crate::scanner::*;
|
pub use crate::scanner::*;
|
||||||
pub use crate::token::*;
|
pub use crate::token::*;
|
||||||
pub use crate::writer::yaml_emitter_flush;
|
|
||||||
|
|
||||||
pub(crate) const INPUT_RAW_BUFFER_SIZE: usize = 16384;
|
pub(crate) const INPUT_RAW_BUFFER_SIZE: usize = 16384;
|
||||||
pub(crate) const INPUT_BUFFER_SIZE: usize = INPUT_RAW_BUFFER_SIZE;
|
pub(crate) const INPUT_BUFFER_SIZE: usize = INPUT_RAW_BUFFER_SIZE;
|
||||||
|
|
|
@ -1,53 +0,0 @@
|
||||||
use crate::{Emitter, Encoding, WriterError};
|
|
||||||
|
|
||||||
/// Flush the accumulated characters to the output.
|
|
||||||
pub fn yaml_emitter_flush(emitter: &mut Emitter) -> Result<(), WriterError> {
|
|
||||||
assert!((emitter.write_handler).is_some());
|
|
||||||
assert_ne!(emitter.encoding, Encoding::Any);
|
|
||||||
|
|
||||||
if emitter.buffer.is_empty() {
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: Support partial writes. These calls fail unless the writer is able
|
|
||||||
// to write absolutely everything in the buffer.
|
|
||||||
|
|
||||||
if emitter.encoding == Encoding::Utf8 {
|
|
||||||
let to_emit = emitter.buffer.as_bytes();
|
|
||||||
emitter
|
|
||||||
.write_handler
|
|
||||||
.as_mut()
|
|
||||||
.expect("non-null writer")
|
|
||||||
.write_all(to_emit)?;
|
|
||||||
emitter.buffer.clear();
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
|
|
||||||
let big_endian = match emitter.encoding {
|
|
||||||
Encoding::Any | Encoding::Utf8 => {
|
|
||||||
unreachable!("unhandled encoding")
|
|
||||||
}
|
|
||||||
Encoding::Utf16Le => false,
|
|
||||||
Encoding::Utf16Be => true,
|
|
||||||
};
|
|
||||||
|
|
||||||
for ch in emitter.buffer.encode_utf16() {
|
|
||||||
let bytes = if big_endian {
|
|
||||||
ch.to_be_bytes()
|
|
||||||
} else {
|
|
||||||
ch.to_le_bytes()
|
|
||||||
};
|
|
||||||
emitter.raw_buffer.extend(bytes);
|
|
||||||
}
|
|
||||||
|
|
||||||
let to_emit = emitter.raw_buffer.as_slice();
|
|
||||||
|
|
||||||
emitter
|
|
||||||
.write_handler
|
|
||||||
.as_mut()
|
|
||||||
.expect("non-null function pointer")
|
|
||||||
.write_all(to_emit)?;
|
|
||||||
emitter.buffer.clear();
|
|
||||||
emitter.raw_buffer.clear();
|
|
||||||
Ok(())
|
|
||||||
}
|
|
Loading…
Reference in a new issue