guile-commits
[Top][All Lists]
Advanced

[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



reply via email to

[Prev in Thread] Current Thread [Next in Thread]