Subject: Refactor WriteChars() and WriteBytes() into simple wrappers of a common routine Write().

From: dgp <[email-masked]>
Date: Thu, 30 Jan 2014 18:31:39 +0000

---
 generic/tclEncoding.c |  2 ++
 generic/tclIO.c       | 48 +++++++++++++++++++++++++++++++++++-------------
 generic/tclInt.h      |  2 ++
 3 files changed, 39 insertions(+), 13 deletions(-)

diff --git a/generic/tclEncoding.c b/generic/tclEncoding.c
index c2f1b4b..c303dd1 100644
--- a/generic/tclEncoding.c
+++ b/generic/tclEncoding.c
@@ -182,6 +182,7 @@ TCL_DECLARE_MUTEX(encodingMutex)
 
 static Tcl_Encoding defaultEncoding;
 static Tcl_Encoding systemEncoding;
+Tcl_Encoding tclIdentityEncoding;
 
 /*
  * The following variable is used in the sparse matrix code for a
@@ -566,6 +567,7 @@ TclInitEncodingSubsystem(void)
     type.clientData	= NULL;
 
     defaultEncoding	= Tcl_CreateEncoding(&type);
+    tclIdentityEncoding = Tcl_GetEncoding(NULL, type.encodingName);
     systemEncoding	= Tcl_GetEncoding(NULL, type.encodingName);
 
     type.encodingName	= "utf-8";
diff --git a/generic/tclIO.c b/generic/tclIO.c
index 4ae9ec0..78ad629 100644
--- a/generic/tclIO.c
+++ b/generic/tclIO.c
@@ -200,6 +200,7 @@ static int		FilterInputBytes(Channel *chanPtr,
 static int		FlushChannel(Tcl_Interp *interp, Channel *chanPtr,
 			    int calledFromAsyncFlush);
 static int		TclGetsObjBinary(Tcl_Channel chan, Tcl_Obj *objPtr);
+static Tcl_Encoding	GetBinaryEncoding();
 static void		FreeBinaryEncoding(ClientData clientData);
 static Tcl_HashTable *	GetChannelTable(Tcl_Interp *interp);
 static int		GetInput(Channel *chanPtr);
@@ -222,6 +223,8 @@ static int		TranslateInputEOL(ChannelState *statePtr, char *dst,
 static int		TranslateOutputEOL(ChannelState *statePtr, char *dst,
 			    const char *src, int *dstLenPtr, int *srcLenPtr);
 static void		UpdateInterest(Channel *chanPtr);
+static int		Write(Channel *chanPtr, const char *src,
+			    int srcLen, Tcl_Encoding encoding);
 static int		WriteBytes(Channel *chanPtr, const char *src,
 			    int srcLen);
 static int		WriteChars(Channel *chanPtr, const char *src,
@@ -3640,6 +3643,9 @@ WriteBytes(
     const char *src,		/* Bytes to write. */
     int srcLen)			/* Number of bytes to write. */
 {
+#if 1
+    return Write(chanPtr, src, srcLen, tclIdentityEncoding);
+#else
     ChannelState *statePtr = chanPtr->state;
 				/* State info for channel */
     ChannelBuffer *bufPtr;
@@ -3712,6 +3718,7 @@ WriteBytes(
 	sawLF = 0;
     }
     return total;
+#endif
 }
 
 /*
@@ -3736,16 +3743,16 @@ WriteBytes(
  */
 
 static int
-WriteChars(
+Write(
     Channel *chanPtr,		/* The channel to buffer output for. */
     const char *src,		/* UTF-8 string to write. */
-    int srcLen)			/* Length of UTF-8 string in bytes. */
+    int srcLen,			/* Length of UTF-8 string in bytes. */
+    Tcl_Encoding encoding)
 {
     ChannelState *statePtr = chanPtr->state;
 				/* State info for channel */
     char *nextNewLine = NULL;
     int endEncoding, saved = 0, total = 0, flushed = 0, needNlFlush = 0;
-    Tcl_Encoding encoding = statePtr->encoding;
 
     if (srcLen) {
         WillWrite(chanPtr);
@@ -3894,6 +3901,15 @@ WriteChars(
 
     return total;
 }
+
+static int
+WriteChars(
+    Channel *chanPtr,		/* The channel to buffer output for. */
+    const char *src,		/* UTF-8 string to write. */
+    int srcLen)			/* Length of UTF-8 string in bytes. */
+{
+    return Write(chanPtr, src, srcLen, chanPtr->state->encoding);
+}
 
 /*
  *---------------------------------------------------------------------------
@@ -4197,16 +4213,7 @@ Tcl_GetsObj(
      */
 
     if (encoding == NULL) {
-	ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
-
-	if (tsdPtr->binaryEncoding == NULL) {
-	    tsdPtr->binaryEncoding = Tcl_GetEncoding(NULL, "iso8859-1");
-	    Tcl_CreateThreadExitHandler(FreeBinaryEncoding, NULL);
-	}
-	encoding = tsdPtr->binaryEncoding;
-	if (encoding == NULL) {
-	    Tcl_Panic("attempted gets on binary channel where no iso8859-1 encoding available");
-	}
+	encoding = GetBinaryEncoding();
     }
 
     /*
@@ -4747,6 +4754,21 @@ FreeBinaryEncoding(
 	tsdPtr->binaryEncoding = NULL;
     }
 }
+
+static Tcl_Encoding
+GetBinaryEncoding()
+{
+    ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
+
+    if (tsdPtr->binaryEncoding == NULL) {
+	tsdPtr->binaryEncoding = Tcl_GetEncoding(NULL, "iso8859-1");
+	Tcl_CreateThreadExitHandler(FreeBinaryEncoding, NULL);
+    }
+    if (tsdPtr->binaryEncoding == NULL) {
+	Tcl_Panic("binary encoding is not available");
+    }
+    return tsdPtr->binaryEncoding;
+}
 
 /*
  *---------------------------------------------------------------------------
diff --git a/generic/tclInt.h b/generic/tclInt.h
index dc28b97..b45f8e3 100644
--- a/generic/tclInt.h
+++ b/generic/tclInt.h
@@ -2382,6 +2382,8 @@ MODULE_SCOPE char *tclMemDumpFileName;
 MODULE_SCOPE TclPlatformType tclPlatform;
 MODULE_SCOPE Tcl_NotifierProcs tclOriginalNotifier;
 
+MODULE_SCOPE Tcl_Encoding tclIdentityEncoding;
+
 /*
  * TIP #233 (Virtualized Time)
  * Data for the time hooks, if any.
-- 
cgit v0.12