Diff
Not logged in

Differences From Artifact [8e2aa1cbcf]:

To Artifact [0803bdaf9e]:


178
179
180
181
182
183
184
185
186


187
188
189
190


191
192
193
194
195
196
197


198
199

200
201
202

203
204
205
206
207
208
209
210
211
212
213
214
215
216




217
218
219



220
221
222
223
224
225
226
178
179
180
181
182
183
184


185
186
187



188
189
190
191
192
193
194


195
196


197
198
199

200

201
202
203
204
205
206
207
208
209
210



211
212
213
214



215
216
217
218
219
220
221
222
223
224







-
-
+
+

-
-
-
+
+





-
-
+
+
-
-
+


-
+
-










-
-
-
+
+
+
+
-
-
-
+
+
+







}

/*
 *---------------------------------------------------------------------------
 *
 * Tcl_UniCharToUtf --
 *
 *	Store the given Tcl_UniChar as a sequence of UTF-8 bytes in the
 *	provided buffer. Equivalent to Plan 9 runetochar().
 *	Stores the given Tcl_UniChar as a sequence of UTF-8 bytes in the provided
 *	buffer. Equivalent to Plan 9 runetochar().
 *
 *	Special handling of Surrogate pairs is handled as follows:
 *	When this function is called for ch being a high surrogate,
 *	the first byte of the 4-byte UTF-8 sequence is produced and
 *	When this function is called and ch is a high surrogate,
 *	the first byte of the 4-byte UTF-8 sequence is produced, and
 *	the function returns 1. Calling the function again with a
 *	low surrogate, the remaining 3 bytes of the 4-byte UTF-8
 *	sequence is produced, and the function returns 3. The buffer
 *	is used to remember the high surrogate between the two calls.
 *
 *	If no low surrogate follows the high surrogate (which is actually
 *	illegal), this can be handled reasonably by calling Tcl_UniCharToUtf
 *	If no low surrogate follows the high surrogate (which is actually illegal),
 *	calling Tcl_UniCharToUtf again with ch being -1 produces a 3-byte UTF-8
 *	again with ch = -1. This will produce a 3-byte UTF-8 sequence
 *	representing the high surrogate.
 *	sequence representing the high surrogate.
 *
 * Results:
 *	The return values is the number of bytes in the buffer that were
 *	Returns the number of bytes populated in the buffer.
 *	consumed.
 *
 * Side effects:
 *	None.
 *
 *---------------------------------------------------------------------------
 */

#undef Tcl_UniCharToUtf
size_t
Tcl_UniCharToUtf(
    int ch,			/* The Tcl_UniChar to be stored in the
				 * buffer. Can be or'ed with flag TCL_COMBINE */
    char *buf)			/* Buffer in which the UTF-8 representation of
    int ch,	/* The Tcl_UniChar to be stored in the
		 * buffer. Can be or'ed with flag TCL_COMBINE
		 */
    char *buf)	/* Buffer in which the UTF-8 representation of
				 * the Tcl_UniChar is stored. Buffer must be
				 * large enough to hold the UTF-8 character
				 * (at most 4 bytes). */
		 * ch is stored. Must be large enough to hold the UTF-8
		 * character (at most 4 bytes).
		 */
{
#if TCL_UTF_MAX > 3
    int flags = ch;
#endif

    if (ch >= TCL_COMBINE) {
	ch &= (TCL_COMBINE - 1);
249
250
251
252
253
254
255




256

257
258
259
260
261
262
263
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266







+
+
+
+

+







			buf[2]  = (char) (0x80 | (0x3F & ch));
			buf[1] |= (char) (0x80 | (0x0F & (ch >> 6)));
			return 3;
		    }
		    /* Previous Tcl_UniChar was not a high surrogate, so just output */
		} else {
		    /* High surrogate */

		    /* Add 0x10000 to the raw number encoded in the surrogate
		     * pair in order to get the code point.
		    */
		    ch += 0x40;

		    /* Fill buffer with specific 3-byte (invalid) byte combination,
		       so following low surrogate can recognize it and combine */
		    buf[2] = (char) (        0x03 & ch);
		    buf[1] = (char) (0x80 | (0x3F & (ch >> 2)));
		    buf[0] = (char) (0xF0 | (0x07 & (ch >> 8)));
		    return 1;
		}