[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Guile-commits] 01/09: port_clear_stream_start_for_bom_write refactor
From: |
Andy Wingo |
Subject: |
[Guile-commits] 01/09: port_clear_stream_start_for_bom_write refactor |
Date: |
Wed, 1 Jun 2016 10:11:29 +0000 (UTC) |
wingo pushed a commit to branch master
in repository guile.
commit 2f836e2384623dabcadd18799c13965ddf3b6004
Author: Andy Wingo <address@hidden>
Date: Wed May 25 21:46:48 2016 +0200
port_clear_stream_start_for_bom_write refactor
* libguile/ports.c (port_clear_stream_start_for_bom_write): Rework to be
friendly to Scheme write implementations.
---
libguile/ports.c | 61 ++++++++++++++++++++++++++++++++++++++++++------------
1 file changed, 48 insertions(+), 13 deletions(-)
diff --git a/libguile/ports.c b/libguile/ports.c
index 3dd729d..5660984 100644
--- a/libguile/ports.c
+++ b/libguile/ports.c
@@ -2452,42 +2452,77 @@ SCM_DEFINE (scm_port_clear_stream_start_for_bom_read,
}
#undef FUNC_NAME
-static void
-port_clear_stream_start_for_bom_write (SCM port, enum bom_io_mode io_mode)
+SCM_INTERNAL SCM scm_port_clear_stream_start_for_bom_write (SCM port);
+SCM_DEFINE (scm_port_clear_stream_start_for_bom_write,
+ "port-clear-stream-start-for-bom-write", 1, 0, 0,
+ (SCM port),
+ "")
+#define FUNC_NAME s_scm_port_clear_stream_start_for_bom_write
{
- scm_t_port *pt = SCM_PORT (port);
+ scm_t_port *pt;
+
+ SCM_VALIDATE_PORT (1, port);
+ pt = SCM_PORT (port);
if (!pt->at_stream_start_for_bom_write)
- return;
+ return SCM_BOOL_F;
+
+ pt->at_stream_start_for_bom_write = 0;
+ if (pt->rw_random)
+ pt->at_stream_start_for_bom_read = 0;
/* Record that we're no longer at stream start. */
pt->at_stream_start_for_bom_write = 0;
if (pt->rw_random)
pt->at_stream_start_for_bom_read = 0;
- /* Write a BOM if appropriate. */
+ /* Return a BOM if appropriate. */
if (scm_is_eq (pt->encoding, sym_UTF_16))
{
SCM precise_encoding;
+ SCM bom = scm_c_make_bytevector (sizeof (scm_utf16be_bom));
scm_port_acquire_iconv_descriptors (port, NULL, NULL);
precise_encoding = pt->precise_encoding;
scm_port_release_iconv_descriptors (port);
- if (scm_is_eq (precise_encoding, sym_UTF_16LE))
- scm_c_write (port, scm_utf16le_bom, sizeof (scm_utf16le_bom));
- else
- scm_c_write (port, scm_utf16be_bom, sizeof (scm_utf16be_bom));
+ memcpy (SCM_BYTEVECTOR_CONTENTS (bom),
+ scm_is_eq (precise_encoding, sym_UTF_16LE)
+ ? scm_utf16le_bom : scm_utf16be_bom,
+ SCM_BYTEVECTOR_LENGTH (bom));
+ return bom;
}
else if (scm_is_eq (pt->encoding, sym_UTF_32))
{
SCM precise_encoding;
+ SCM bom = scm_c_make_bytevector (sizeof (scm_utf32be_bom));
scm_port_acquire_iconv_descriptors (port, NULL, NULL);
precise_encoding = pt->precise_encoding;
scm_port_release_iconv_descriptors (port);
- if (scm_is_eq (precise_encoding, sym_UTF_32LE))
- scm_c_write (port, scm_utf32le_bom, sizeof (scm_utf32le_bom));
- else
- scm_c_write (port, scm_utf32be_bom, sizeof (scm_utf32be_bom));
+ memcpy (SCM_BYTEVECTOR_CONTENTS (bom),
+ scm_is_eq (precise_encoding, sym_UTF_32LE)
+ ? scm_utf32le_bom : scm_utf32be_bom,
+ SCM_BYTEVECTOR_LENGTH (bom));
+ return bom;
}
+
+ return SCM_BOOL_F;
+}
+#undef FUNC_NAME
+
+static void
+port_clear_stream_start_for_bom_write (SCM port, enum bom_io_mode io_mode)
+{
+ scm_t_port *pt = SCM_PORT (port);
+ SCM bom;
+
+ /* Fast path. */
+ if (!pt->at_stream_start_for_bom_write)
+ return;
+
+ bom = scm_port_clear_stream_start_for_bom_write (port);
+
+ if (// io_mode == BOM_IO_TEXT &&
+ scm_is_true (bom))
+ scm_c_write_bytes (port, bom, 0, SCM_BYTEVECTOR_LENGTH (bom));
}
SCM
- [Guile-commits] branch master updated (47918f3 -> 9454068), Andy Wingo, 2016/06/01
- [Guile-commits] 06/09: Add scm_c_put_escaped_char, scm_c_can_put_char, Andy Wingo, 2016/06/01
- [Guile-commits] 05/09: iprin1 uses scm_c_put_string, Andy Wingo, 2016/06/01
- [Guile-commits] 01/09: port_clear_stream_start_for_bom_write refactor,
Andy Wingo <=
- [Guile-commits] 09/09: put-string in Scheme, Andy Wingo, 2016/06/01
- [Guile-commits] 02/09: Add port-line-buffered?, Andy Wingo, 2016/06/01
- [Guile-commits] 04/09: print_normal_symbol uses new port functions, Andy Wingo, 2016/06/01
- [Guile-commits] 08/09: Rework text encoding to be more Scheme-friendly, Andy Wingo, 2016/06/01
- [Guile-commits] 07/09: Simplify string, symbol, char display/write impls, Andy Wingo, 2016/06/01
- [Guile-commits] 03/09: Beginnings of supporting encoding text in ports.c, Andy Wingo, 2016/06/01