Bug 538324 - Move ctypes into js/src. Part 3: Split out low-level JS 16-bit <--> 8-bit string conversion functions. r=jorendorff

This commit is contained in:
Dan Witte 2010-04-02 13:05:30 -07:00
parent 0ee7f0d0cc
commit feaa2c7898
2 changed files with 60 additions and 13 deletions

View File

@ -3515,20 +3515,26 @@ js_DeflateString(JSContext *cx, const jschar *chars, size_t nchars)
return bytes; return bytes;
} }
size_t
js_GetDeflatedStringLength(JSContext *cx, const jschar *chars, size_t nchars)
{
if (!js_CStringsAreUTF8)
return nchars;
return js_GetDeflatedUTF8StringLength(cx, chars, nchars);
}
/* /*
* May be called with null cx through js_GetStringBytes, see below. * May be called with null cx through js_GetStringBytes, see below.
*/ */
size_t size_t
js_GetDeflatedStringLength(JSContext *cx, const jschar *chars, size_t nchars) js_GetDeflatedUTF8StringLength(JSContext *cx, const jschar *chars, size_t nchars)
{ {
size_t nbytes; size_t nbytes;
const jschar *end; const jschar *end;
uintN c, c2; uintN c, c2;
char buffer[10]; char buffer[10];
if (!js_CStringsAreUTF8)
return nchars;
nbytes = nchars; nbytes = nchars;
for (end = chars + nchars; chars != end; chars++) { for (end = chars + nchars; chars != end; chars++) {
c = *chars; c = *chars;
@ -3566,10 +3572,7 @@ JSBool
js_DeflateStringToBuffer(JSContext *cx, const jschar *src, size_t srclen, js_DeflateStringToBuffer(JSContext *cx, const jschar *src, size_t srclen,
char *dst, size_t *dstlenp) char *dst, size_t *dstlenp)
{ {
size_t dstlen, i, origDstlen, utf8Len; size_t dstlen, i;
jschar c, c2;
uint32 v;
uint8 utf8buf[6];
dstlen = *dstlenp; dstlen = *dstlenp;
if (!js_CStringsAreUTF8) { if (!js_CStringsAreUTF8) {
@ -3588,6 +3591,19 @@ js_DeflateStringToBuffer(JSContext *cx, const jschar *src, size_t srclen,
return JS_TRUE; return JS_TRUE;
} }
return js_DeflateStringToUTF8Buffer(cx, src, srclen, dst, dstlenp);
}
JSBool
js_DeflateStringToUTF8Buffer(JSContext *cx, const jschar *src, size_t srclen,
char *dst, size_t *dstlenp)
{
size_t dstlen, i, origDstlen, utf8Len;
jschar c, c2;
uint32 v;
uint8 utf8buf[6];
dstlen = *dstlenp;
origDstlen = dstlen; origDstlen = dstlen;
while (srclen) { while (srclen) {
c = *src++; c = *src++;
@ -3644,8 +3660,7 @@ JSBool
js_InflateStringToBuffer(JSContext *cx, const char *src, size_t srclen, js_InflateStringToBuffer(JSContext *cx, const char *src, size_t srclen,
jschar *dst, size_t *dstlenp) jschar *dst, size_t *dstlenp)
{ {
size_t dstlen, i, origDstlen, offset, j, n; size_t dstlen, i;
uint32 v;
if (!js_CStringsAreUTF8) { if (!js_CStringsAreUTF8) {
if (dst) { if (dst) {
@ -3666,6 +3681,16 @@ js_InflateStringToBuffer(JSContext *cx, const char *src, size_t srclen,
return JS_TRUE; return JS_TRUE;
} }
return js_InflateUTF8StringToBuffer(cx, src, srclen, dst, dstlenp);
}
JSBool
js_InflateUTF8StringToBuffer(JSContext *cx, const char *src, size_t srclen,
jschar *dst, size_t *dstlenp)
{
size_t dstlen, origDstlen, offset, j, n;
uint32 v;
dstlen = dst ? *dstlenp : (size_t) -1; dstlen = dst ? *dstlenp : (size_t) -1;
origDstlen = dstlen; origDstlen = dstlen;
offset = 0; offset = 0;

View File

@ -617,29 +617,51 @@ js_DeflateString(JSContext *cx, const jschar *chars, size_t length);
* Inflate bytes to JS chars into a buffer. 'chars' must be large enough for * Inflate bytes to JS chars into a buffer. 'chars' must be large enough for
* 'length' jschars. The buffer is NOT null-terminated. The destination length * 'length' jschars. The buffer is NOT null-terminated. The destination length
* must be be initialized with the buffer size and will contain on return the * must be be initialized with the buffer size and will contain on return the
* number of copied chars. * number of copied chars. Conversion behavior depends on js_CStringsAreUTF8.
*/ */
extern JSBool extern JSBool
js_InflateStringToBuffer(JSContext *cx, const char *bytes, size_t length, js_InflateStringToBuffer(JSContext *cx, const char *bytes, size_t length,
jschar *chars, size_t *charsLength); jschar *chars, size_t *charsLength);
/* /*
* Get number of bytes in the deflated sequence of characters. * Same as js_InflateStringToBuffer, but always treats 'bytes' as UTF-8.
*/
extern JSBool
js_InflateUTF8StringToBuffer(JSContext *cx, const char *bytes, size_t length,
jschar *chars, size_t *charsLength);
/*
* Get number of bytes in the deflated sequence of characters. Behavior depends
* on js_CStringsAreUTF8.
*/ */
extern size_t extern size_t
js_GetDeflatedStringLength(JSContext *cx, const jschar *chars, js_GetDeflatedStringLength(JSContext *cx, const jschar *chars,
size_t charsLength); size_t charsLength);
/*
* Same as js_GetDeflatedStringLength, but always treats the result as UTF-8.
*/
extern size_t
js_GetDeflatedUTF8StringLength(JSContext *cx, const jschar *chars,
size_t charsLength);
/* /*
* Deflate JS chars to bytes into a buffer. 'bytes' must be large enough for * Deflate JS chars to bytes into a buffer. 'bytes' must be large enough for
* 'length chars. The buffer is NOT null-terminated. The destination length * 'length chars. The buffer is NOT null-terminated. The destination length
* must to be initialized with the buffer size and will contain on return the * must to be initialized with the buffer size and will contain on return the
* number of copied bytes. * number of copied bytes. Conversion behavior depends on js_CStringsAreUTF8.
*/ */
extern JSBool extern JSBool
js_DeflateStringToBuffer(JSContext *cx, const jschar *chars, js_DeflateStringToBuffer(JSContext *cx, const jschar *chars,
size_t charsLength, char *bytes, size_t *length); size_t charsLength, char *bytes, size_t *length);
/*
* Same as js_DeflateStringToBuffer, but always treats 'bytes' as UTF-8.
*/
extern JSBool
js_DeflateStringToUTF8Buffer(JSContext *cx, const jschar *chars,
size_t charsLength, char *bytes, size_t *length);
/* /*
* Find or create a deflated string cache entry for str that contains its * Find or create a deflated string cache entry for str that contains its
* characters chopped from Unicode code points into bytes. * characters chopped from Unicode code points into bytes.