| 1 | /* |
| 2 | * Copyright (C) 2006 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
| 14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
| 17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #ifndef JSStringRef_h |
| 27 | #define JSStringRef_h |
| 28 | |
| 29 | #include <JavaScriptCore/JSValueRef.h> |
| 30 | |
| 31 | #ifndef __cplusplus |
| 32 | #include <stdbool.h> |
| 33 | #endif |
| 34 | #include <stddef.h> /* for size_t */ |
| 35 | |
| 36 | #ifdef __cplusplus |
| 37 | extern "C" { |
| 38 | #endif |
| 39 | |
| 40 | #if !defined(_NATIVE_WCHAR_T_DEFINED) /* MSVC */ \ |
| 41 | && (!defined(__WCHAR_MAX__) || (__WCHAR_MAX__ > 0xffffU)) /* ISO C/C++ */ \ |
| 42 | && (!defined(WCHAR_MAX) || (WCHAR_MAX > 0xffffU)) /* RVCT */ |
| 43 | /*! |
| 44 | @typedef JSChar |
| 45 | @abstract A UTF-16 code unit. One, or a sequence of two, can encode any Unicode |
| 46 | character. As with all scalar types, endianness depends on the underlying |
| 47 | architecture. |
| 48 | */ |
| 49 | typedef unsigned short JSChar; |
| 50 | #else |
| 51 | typedef wchar_t JSChar; |
| 52 | #endif |
| 53 | |
| 54 | /*! |
| 55 | @function |
| 56 | @abstract Creates a JavaScript string from a buffer of Unicode characters. |
| 57 | @param chars The buffer of Unicode characters to copy into the new JSString. |
| 58 | @param numChars The number of characters to copy from the buffer pointed to by chars. |
| 59 | @result A JSString containing chars. Ownership follows the Create Rule. |
| 60 | */ |
| 61 | JS_EXPORT JSStringRef JSStringCreateWithCharacters(const JSChar* chars, size_t numChars); |
| 62 | /*! |
| 63 | @function |
| 64 | @abstract Creates a JavaScript string from a null-terminated UTF8 string. |
| 65 | @param string The null-terminated UTF8 string to copy into the new JSString. |
| 66 | @result A JSString containing string. Ownership follows the Create Rule. |
| 67 | */ |
| 68 | JS_EXPORT JSStringRef JSStringCreateWithUTF8CString(const char* string); |
| 69 | |
| 70 | /*! |
| 71 | @function |
| 72 | @abstract Retains a JavaScript string. |
| 73 | @param string The JSString to retain. |
| 74 | @result A JSString that is the same as string. |
| 75 | */ |
| 76 | JS_EXPORT JSStringRef JSStringRetain(JSStringRef string); |
| 77 | /*! |
| 78 | @function |
| 79 | @abstract Releases a JavaScript string. |
| 80 | @param string The JSString to release. |
| 81 | */ |
| 82 | JS_EXPORT void JSStringRelease(JSStringRef string); |
| 83 | |
| 84 | /*! |
| 85 | @function |
| 86 | @abstract Returns the number of Unicode characters in a JavaScript string. |
| 87 | @param string The JSString whose length (in Unicode characters) you want to know. |
| 88 | @result The number of Unicode characters stored in string. |
| 89 | */ |
| 90 | JS_EXPORT size_t JSStringGetLength(JSStringRef string); |
| 91 | /*! |
| 92 | @function |
| 93 | @abstract Returns a pointer to the Unicode character buffer that |
| 94 | serves as the backing store for a JavaScript string. |
| 95 | @param string The JSString whose backing store you want to access. |
| 96 | @result A pointer to the Unicode character buffer that serves as string's |
| 97 | backing store, which will be deallocated when string is deallocated. |
| 98 | */ |
| 99 | JS_EXPORT const JSChar* JSStringGetCharactersPtr(JSStringRef string); |
| 100 | |
| 101 | /*! |
| 102 | @function |
| 103 | @abstract Returns the maximum number of bytes a JavaScript string will |
| 104 | take up if converted into a null-terminated UTF8 string. |
| 105 | @param string The JSString whose maximum converted size (in bytes) you |
| 106 | want to know. |
| 107 | @result The maximum number of bytes that could be required to convert string into a |
| 108 | null-terminated UTF8 string. The number of bytes that the conversion actually ends |
| 109 | up requiring could be less than this, but never more. |
| 110 | */ |
| 111 | JS_EXPORT size_t JSStringGetMaximumUTF8CStringSize(JSStringRef string); |
| 112 | /*! |
| 113 | @function |
| 114 | @abstract Converts a JavaScript string into a null-terminated UTF8 string, |
| 115 | and copies the result into an external byte buffer. |
| 116 | @param string The source JSString. |
| 117 | @param buffer The destination byte buffer into which to copy a null-terminated |
| 118 | UTF8 representation of string. On return, buffer contains a UTF8 string |
| 119 | representation of string. If bufferSize is too small, buffer will contain only |
| 120 | partial results. If buffer is not at least bufferSize bytes in size, |
| 121 | behavior is undefined. |
| 122 | @param bufferSize The size of the external buffer in bytes. |
| 123 | @result The number of bytes written into buffer (including the null-terminator byte). |
| 124 | */ |
| 125 | JS_EXPORT size_t JSStringGetUTF8CString(JSStringRef string, char* buffer, size_t bufferSize); |
| 126 | |
| 127 | /*! |
| 128 | @function |
| 129 | @abstract Tests whether two JavaScript strings match. |
| 130 | @param a The first JSString to test. |
| 131 | @param b The second JSString to test. |
| 132 | @result true if the two strings match, otherwise false. |
| 133 | */ |
| 134 | JS_EXPORT bool JSStringIsEqual(JSStringRef a, JSStringRef b); |
| 135 | /*! |
| 136 | @function |
| 137 | @abstract Tests whether a JavaScript string matches a null-terminated UTF8 string. |
| 138 | @param a The JSString to test. |
| 139 | @param b The null-terminated UTF8 string to test. |
| 140 | @result true if the two strings match, otherwise false. |
| 141 | */ |
| 142 | JS_EXPORT bool JSStringIsEqualToUTF8CString(JSStringRef a, const char* b); |
| 143 | |
| 144 | #ifdef __cplusplus |
| 145 | } |
| 146 | #endif |
| 147 | |
| 148 | #endif /* JSStringRef_h */ |
| 149 | |