JSStringRef.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. #ifndef JSStringRef_h
  26. #define JSStringRef_h
  27. #include <JavaScriptCore/JSValueRef.h>
  28. #ifndef __cplusplus
  29. #include <stdbool.h>
  30. #endif
  31. #include <stddef.h> /* for size_t */
  32. #ifdef __cplusplus
  33. extern "C" {
  34. #endif
  35. #if !defined(_NATIVE_WCHAR_T_DEFINED) /* MSVC */ \
  36. && (!defined(__WCHAR_MAX__) || (__WCHAR_MAX__ > 0xffffU)) /* ISO C/C++ */ \
  37. && (!defined(WCHAR_MAX) || (WCHAR_MAX > 0xffffU)) /* RVCT */
  38. /*!
  39. @typedef JSChar
  40. @abstract A UTF-16 code unit. One, or a sequence of two, can encode any Unicode
  41. character. As with all scalar types, endianness depends on the underlying
  42. architecture.
  43. */
  44. typedef unsigned short JSChar;
  45. #else
  46. typedef wchar_t JSChar;
  47. #endif
  48. /*!
  49. @function
  50. @abstract Creates a JavaScript string from a buffer of Unicode characters.
  51. @param chars The buffer of Unicode characters to copy into the new JSString.
  52. @param numChars The number of characters to copy from the buffer pointed to by chars.
  53. @result A JSString containing chars. Ownership follows the Create Rule.
  54. */
  55. JS_EXPORT JSStringRef JSStringCreateWithCharacters(const JSChar* chars, size_t numChars);
  56. /*!
  57. @function
  58. @abstract Creates a JavaScript string from a null-terminated UTF8 string.
  59. @param string The null-terminated UTF8 string to copy into the new JSString.
  60. @result A JSString containing string. Ownership follows the Create Rule.
  61. */
  62. JS_EXPORT JSStringRef JSStringCreateWithUTF8CString(const char* string);
  63. /*!
  64. @function
  65. @abstract Retains a JavaScript string.
  66. @param string The JSString to retain.
  67. @result A JSString that is the same as string.
  68. */
  69. JS_EXPORT JSStringRef JSStringRetain(JSStringRef string);
  70. /*!
  71. @function
  72. @abstract Releases a JavaScript string.
  73. @param string The JSString to release.
  74. */
  75. JS_EXPORT void JSStringRelease(JSStringRef string);
  76. /*!
  77. @function
  78. @abstract Returns the number of Unicode characters in a JavaScript string.
  79. @param string The JSString whose length (in Unicode characters) you want to know.
  80. @result The number of Unicode characters stored in string.
  81. */
  82. JS_EXPORT size_t JSStringGetLength(JSStringRef string);
  83. /*!
  84. @function
  85. @abstract Returns a pointer to the Unicode character buffer that
  86. serves as the backing store for a JavaScript string.
  87. @param string The JSString whose backing store you want to access.
  88. @result A pointer to the Unicode character buffer that serves as string's
  89. backing store, which will be deallocated when string is deallocated.
  90. */
  91. JS_EXPORT const JSChar* JSStringGetCharactersPtr(JSStringRef string);
  92. /*!
  93. @function
  94. @abstract Returns the maximum number of bytes a JavaScript string will
  95. take up if converted into a null-terminated UTF8 string.
  96. @param string The JSString whose maximum converted size (in bytes) you
  97. want to know.
  98. @result The maximum number of bytes that could be required to convert string into a
  99. null-terminated UTF8 string. The number of bytes that the conversion actually ends
  100. up requiring could be less than this, but never more.
  101. */
  102. JS_EXPORT size_t JSStringGetMaximumUTF8CStringSize(JSStringRef string);
  103. /*!
  104. @function
  105. @abstract Converts a JavaScript string into a null-terminated UTF8 string,
  106. and copies the result into an external byte buffer.
  107. @param string The source JSString.
  108. @param buffer The destination byte buffer into which to copy a null-terminated
  109. UTF8 representation of string. On return, buffer contains a UTF8 string
  110. representation of string. If bufferSize is too small, buffer will contain only
  111. partial results. If buffer is not at least bufferSize bytes in size,
  112. behavior is undefined.
  113. @param bufferSize The size of the external buffer in bytes.
  114. @result The number of bytes written into buffer (including the null-terminator byte).
  115. */
  116. JS_EXPORT size_t JSStringGetUTF8CString(JSStringRef string, char* buffer, size_t bufferSize);
  117. /*!
  118. @function
  119. @abstract Tests whether two JavaScript strings match.
  120. @param a The first JSString to test.
  121. @param b The second JSString to test.
  122. @result true if the two strings match, otherwise false.
  123. */
  124. JS_EXPORT bool JSStringIsEqual(JSStringRef a, JSStringRef b);
  125. /*!
  126. @function
  127. @abstract Tests whether a JavaScript string matches a null-terminated UTF8 string.
  128. @param a The JSString to test.
  129. @param b The null-terminated UTF8 string to test.
  130. @result true if the two strings match, otherwise false.
  131. */
  132. JS_EXPORT bool JSStringIsEqualToUTF8CString(JSStringRef a, const char* b);
  133. #ifdef __cplusplus
  134. }
  135. #endif
  136. #endif /* JSStringRef_h */