Browse Source

Delete and redirect internal tree docs (en-US/ja/zh-CN) (#13)

SphinxKnight 3 years ago
parent
commit
f08ff1f015

+ 6 - 5
files/en-us/_redirects.txt

@@ -2681,6 +2681,7 @@
 /en-US/docs/Mozilla/Developer_guide/Building_Firefox_with_Rust_code	/en-US/docs/Mozilla/Firefox/Building_Firefox_with_Rust_code
 /en-US/docs/Mozilla/Developer_guide/Using_the_VM	/en-US/docs/Archive/Mozilla/Using_the_VM
 /en-US/docs/Mozilla/Getting_started_with_chat	/en-US/docs/Archive/Mozilla/Getting_started_with_chat
+/en-US/docs/Mozilla/Tech/XPCOM/Guide/Internal_strings	https://firefox-source-docs.mozilla.org/xpcom/stringguide.html
 /en-US/docs/Mozilla_Application_Framework	/en-US/docs/Archive/Mozilla/Mozilla_Application_Framework
 /en-US/docs/Mozilla_Application_Framework_in_Detail	/en-US/docs/Archive/Mozilla/Mozilla_Application_Framework/Mozilla_Application_Framework_in_Detail
 /en-US/docs/Mozilla_Crash_Reporter	/en-US/docs/Mozilla/Projects/Crash_reporting
@@ -2848,8 +2849,8 @@
 /en-US/docs/Mozilla_event_reference/updateready	/en-US/docs/Archive/Events/updateready
 /en-US/docs/Mozilla_event_reference/userproximity	/en-US/docs/Archive/API/userproximity
 /en-US/docs/Mozilla_external_string_guide	/en-US/docs/Mozilla/Mozilla_external_string_guide
-/en-US/docs/Mozilla_internal_string_guide	/en-US/docs/Mozilla/Tech/XPCOM/Guide/Internal_strings
-/en-US/docs/Mozilla_internal_string_guide/Mozilla_internal_string_guide	/en-US/docs/Mozilla/Tech/XPCOM/Guide/Internal_strings
+/en-US/docs/Mozilla_internal_string_guide	https://firefox-source-docs.mozilla.org/xpcom/stringguide.html
+/en-US/docs/Mozilla_internal_string_guide/Mozilla_internal_string_guide	https://firefox-source-docs.mozilla.org/xpcom/stringguide.html
 /en-US/docs/Mozilla_project_presentations	/en-US/docs/Archive/Mozilla/Mozilla_project_presentations
 /en-US/docs/Mozinfo	/en-US/docs/Mozilla/Projects/mozinfo
 /en-US/docs/Mozmill	/en-US/docs/Mozilla/Projects/Mozmill
@@ -7469,7 +7470,7 @@
 /en-US/docs/XPCOM/Language_Bindings	/en-US/docs/Mozilla/Tech/XPCOM/Language_Bindings
 /en-US/docs/XPCOM/Making_cross-thread_calls_using_runnables	/en-US/docs/Mozilla/Tech/XPCOM/Guide/Cross-thread_calls_using_runnables
 /en-US/docs/XPCOM/Receiving_startup_notifications	/en-US/docs/Mozilla/Tech/XPCOM/Guide/Receiving_startup_notifications
-/en-US/docs/XPCOM/Strings	/en-US/docs/Mozilla/Tech/XPCOM/Guide/Internal_strings
+/en-US/docs/XPCOM/Strings	https://firefox-source-docs.mozilla.org/xpcom/stringguide.html
 /en-US/docs/XPCOM/XPCOM_Components	/en-US/docs/Mozilla/Tech/XPCOM/Guide/Creating_components
 /en-US/docs/XPCOM/XPCOM_changes_in_Gecko_1.9.3	/en-US/docs/Mozilla/Tech/XPCOM/Guide/Changes_in_Gecko_2.0
 /en-US/docs/XPCOM/XPCOM_changes_in_Gecko_2.0	/en-US/docs/Mozilla/Tech/XPCOM/Guide/Changes_in_Gecko_2.0
@@ -7478,7 +7479,7 @@
 /en-US/docs/XPCOM:Hashtables	/en-US/docs/Mozilla/Tech/XPCOM/Guide/Hashtables_detailed
 /en-US/docs/XPCOM:Inheriting_from_implementation_classes	/en-US/docs/Mozilla/Tech/XPCOM/Guide/Inheriting_from_implementation_classes
 /en-US/docs/XPCOM:Language_Bindings	/en-US/docs/Mozilla/Tech/XPCOM/Language_Bindings
-/en-US/docs/XPCOM:Strings	/en-US/docs/Mozilla/Tech/XPCOM/Guide/Internal_strings
+/en-US/docs/XPCOM:Strings	https://firefox-source-docs.mozilla.org/xpcom/stringguide.html
 /en-US/docs/XPCOM:XPCOM_Components	/en-US/docs/Mozilla/Tech/XPCOM/Guide/Creating_components
 /en-US/docs/XPCOMUtils.jsm	/en-US/docs/Mozilla/JavaScript_code_modules/XPCOMUtils.jsm
 /en-US/docs/XPCOM_ABI	/en-US/docs/Mozilla/Tech/XPCOM/XPCOM_ABI
@@ -8410,7 +8411,7 @@
 /en-US/docs/XPCOM_ownership_guidelines	/en-US/docs/Mozilla/Tech/XPCOM/XPCOM_ownership_guidelines
 /en-US/docs/XPCOM_primitive	/en-US/docs/Mozilla/Tech/XPCOM/Reference/Primitive
 /en-US/docs/XPCOM_primitives	/en-US/docs/Mozilla/Tech/XPCOM/Reference/Primitive
-/en-US/docs/XPCOM_string_guide	/en-US/docs/Mozilla/Tech/XPCOM/Guide/Internal_strings
+/en-US/docs/XPCOM_string_guide	https://firefox-source-docs.mozilla.org/xpcom/stringguide.html
 /en-US/docs/XPCOM_tasks	/en-US/docs/Mozilla/Tech/XPCOM/XPCOM_tasks
 /en-US/docs/XPCShell/Profiling	/en-US/docs/Mozilla/Tech/XPCOM/Language_bindings/XPConnect/xpcshell/Profiling
 /en-US/docs/XPCShell:Profiling	/en-US/docs/Mozilla/Tech/XPCOM/Language_bindings/XPConnect/xpcshell/Profiling

+ 0 - 43
files/en-us/_wikihistory.json

@@ -76772,49 +76772,6 @@
       "BenB"
     ]
   },
-  "Mozilla/Tech/XPCOM/Guide/Internal_strings": {
-    "modified": "2020-07-02T13:43:43.650Z",
-    "contributors": [
-      "sigiesec",
-      "wbamberg",
-      "Hsivonen",
-      "SphinxKnight",
-      "nnethercote",
-      "gfritzsche",
-      "GijsKruitbosch",
-      "Sheppy",
-      "mmcdonough",
-      "dhylands@mozilla.com",
-      "kscarfone",
-      "balah",
-      "Heycam",
-      "DBaron",
-      "jrmuizel",
-      "mustard",
-      "jlebar",
-      "jduell.mcbugs@gmail.com",
-      "bsmedberg",
-      "Mkmelin",
-      "Jorend",
-      "Nickolay",
-      "Ted_Mielczarek",
-      "Mgjbot",
-      "cbiesinger",
-      "Allanbonadio",
-      "Dougt",
-      "Dolske",
-      "Mook",
-      "DarinFisher",
-      "GavinSharp",
-      "NickolayBot",
-      "Sayrer",
-      "Waldo",
-      "Dria",
-      "Buffered",
-      "Tservo",
-      "Anonymous"
-    ]
-  },
   "Mozilla/Tech/XPCOM/Guide/Receiving_startup_notifications": {
     "modified": "2019-04-12T15:01:38.880Z",
     "contributors": [

+ 0 - 977
files/en-us/mozilla/tech/xpcom/guide/internal_strings/index.html

@@ -1,977 +0,0 @@
----
-title: Mozilla internal string guide
-slug: Mozilla/Tech/XPCOM/Guide/Internal_strings
-tags:
-  - Guide
-  - XPCOM
----
-<p><span class="seoSummary">Most of the Mozilla code uses a C++ class hierarchy to pass string data, rather than using raw pointers. This guide documents the string classes which are visible to code within the Mozilla codebase (code which is linked into libxul).</span></p>
-
-<h2 id="Introduction">Introduction</h2>
-
-<p>The string classes are a library of C++ classes which are used to manage buffers of wide (16-bit) and narrow (8-bit) character strings. The headers and implementation are in the <code><a href="https://dxr.mozilla.org/mozilla-central/source/xpcom/string" rel="custom">xpcom/string</a></code> directory. All strings are stored as a single contiguous buffer of characters.</p>
-
-<p>The 8-bit and 16-bit string classes have completely separate base classes, but share the same APIs. As a result, you cannot assign a 8-bit string to a 16-bit string without some kind of conversion helper class or routine. For the purpose of this document, we will refer to the 16-bit string classes in class documentation. Every 16-bit class has an equivalent 8-bit class:</p>
-
-<table style="width: 200px;">
- <caption>Naming convention for wide and narrow string classes</caption>
- <tbody>
-  <tr>
-   <td>Wide</td>
-   <td>Narrow</td>
-  </tr>
-  <tr>
-   <td>nsAString</td>
-   <td>nsACString</td>
-  </tr>
-  <tr>
-   <td>nsString</td>
-   <td>nsCString</td>
-  </tr>
-  <tr>
-   <td>nsAutoString</td>
-   <td>nsAutoCString</td>
-  </tr>
-  <tr>
-   <td colspan="2">etc...</td>
-  </tr>
- </tbody>
-</table>
-
-<p>The string classes distinguish, as part of the type hierarchy, between strings that must have a null-terminator at the end of their buffer (<code>ns[C]String</code>) and strings that are not required to have a null-terminator (<code>nsA[C]String</code>). <code>nsA[C]String</code> is the base of the string classes (since it imposes fewer requirements) and <code>ns[C]String</code> is a class derived from it. Functions taking strings as parameters should generally take one of these four types.</p>
-
-<p>In order to avoid unnecessary copying of string data (which can have significant performance cost), the string classes support different ownership models. All string classes support the following three ownership models dynamically:</p>
-
-<ul>
- <li>reference counted, copy-on-write, buffers (the default)</li>
- <li>adopted buffers (a buffer that the string class owns, but is not reference counted, because it came from somewhere else)</li>
- <li>dependent buffers, that is, an underlying buffer that the string class does not own, but that the caller that constructed the string guarantees will outlive the string instance</li>
-</ul>
-
-<p>In addition, there is a special string class, <code>ns[C]AutoString</code>, that <em>additionally</em> contains an internal 64-unit buffer (intended primarily for use on the stack), leading to a fourth ownership model:</p>
-
-<ul>
- <li>storage within an auto string's stack buffer</li>
-</ul>
-
-<p>Auto strings will prefer reference counting an existing reference-counted buffer over their stack buffer, but will otherwise use their stack buffer for anything that will fit in it.</p>
-
-<p>There are a number of additional string classes:</p>
-
-<ul>
- <li>Classes which exist primarily as constructors for the other types, particularly <code>nsDependent[C]String</code> and <code>nsDependent[C]Substring</code>. These types are really just convenient notation for constructing an <code>ns[C]S[ubs]tring</code> with a non-default ownership mode; they should not be thought of as different types.</li>
- <li><code>nsLiteral[C]String</code> which should rarely be constructed explicitly but usually through the <code>""_ns</code> and <code>u""_ns</code> user-defined string literals. <code>nsLiteral[C]String</code> is trivially constructible and destructible, and therefore does not emit construction/destruction code when stored in statics, as opposed to the other string classes.</li>
-</ul>
-
-<h2 id="The_Major_String_Classes">The Major String Classes</h2>
-
-<p>The list below describes the main base classes. Once you are familiar with them, see the appendix describing What Class to Use When.</p>
-
-<ul>
- <li><strong><code>nsAString</code>/<code>nsACString</code></strong>: the abstract base class for all strings. It provides an API for assignment, individual character access, basic manipulation of characters in the string, and string comparison. This class corresponds to the XPIDL <code>AString</code> parameter type. nsAString is not necessarily null-terminated.</li>
- <li><strong><code>nsString</code>/<code>nsCString</code></strong>: builds on <code>nsAString</code> by guaranteeing a null-terminated storage. This allows for a method (<code>.get()</code>) to access the underlying character buffer.</li>
-</ul>
-
-<p>The remainder of the string classes inherit from either <code>nsAString</code> or <code>nsString</code>. Thus, every string class is compatible with <code>nsAString</code>.</p>
-
-<p>Since every string derives from <code>nsAString</code> (or <code>nsACString</code>), they all share a simple API. Common read-only methods:</p>
-
-<ul>
- <li><strong><code>.Length()</code></strong> - the number of code units (bytes for 8-bit string classes and <code>char16_t</code>s for 16-bit string classes) in the string.</li>
- <li><strong><code>.IsEmpty()</code></strong> - the fastest way of determining if the string has any value. Use this instead of testing <code>string.Length</code> == 0</li>
- <li><strong><code>.Equals(string)</code></strong> - TRUE if the given string has the same value as the current string.</li>
-</ul>
-
-<p>Common methods that modify the string:</p>
-
-<ul>
- <li><strong><code>.Assign(string)</code></strong> - Assigns a new value to the string.</li>
- <li><strong><code>.Append(string)</code></strong> - Appends a value to the string.</li>
- <li><strong><code>.Insert(string, position)</code></strong> - Inserts the given string before the code unit at position.</li>
- <li><strong><code>.Truncate(length)</code></strong> - shortens the string to the given length.</li>
-</ul>
-
-<p>Complete documentation can be found in the <a href="#Appendix_B_-_nsAString_Reference">Appendix</a>.</p>
-
-<h3 id="Read-only_strings">Read-only strings</h3>
-
-<p>The <code>const</code> attribute on a string determines if the string is writable. If a string is defined as a <code>const nsAString</code> then the data in the string cannot be manipulated. If one tries to call a non-<code>const</code> method on a <code>const</code> string the compiler will flag this as an error at build time.</p>
-
-<p>For example:</p>
-
-<pre class="eval notranslate">void nsFoo::ReverseCharacters(nsAString&amp; str) {
-      ...
-     str.Assign(reversedStr); // modifies the string
-}
-</pre>
-
-<p>This should not compile, because you're assigning to a <code>const</code> class:</p>
-
-<pre class="eval notranslate">void nsFoo::ReverseCharacters(const nsAString&amp; str) {
-      ...
-     <strong>str.Assign(reversedStr);</strong>
-}
-</pre>
-
-<h3 id="As_function_parameters">As function parameters</h3>
-
-<p>For methods which are exposed across modules, use nsAString references to pass strings. For example:</p>
-
-<pre class="notranslate">// when passing a string to a method, use const nsAString&amp;
-nsFoo::PrintString(<strong>const nsAString &amp;str</strong>);
-
-// when getting a string from a method, use nsAString&amp;
-nsFoo::GetString(<strong>nsAString &amp;result</strong>);
-</pre>
-
-<p>The abstract classes are also sometimes used to store temporary references to objects. You can see both of these uses in <a href="#Common_Patterns">Common Patterns</a>, below.</p>
-
-<h2 id="The_Concrete_Classes_-_which_classes_to_use_when">The Concrete Classes - which classes to use when</h2>
-
-<p>The concrete classes are for use in code that actually needs to store string data. The most common uses of the concrete classes are as local variables, and members in classes or structs.</p>
-
-<p><img alt="nsAString_internal-graph.png" class="default internal" src="/@api/deki/files/2968/=nsAString_internal-graph.png" style="height: 443px; width: 732px;"></p>
-
-<p> The following is a list of the most common concrete classes. Once you are familiar with them, see the appendix describing <a href="#Appendix_A_-_What_class_to_use_when">What Class to Use When.</a></p>
-
-<p><img alt="" src="/@api/deki/pages/=en/nsAString_internal/files/=nsAString_internal-graph.png"></p>
-
-<ul>
- <li><code><strong>nsString / nsCString</strong></code>- a null-terminated string whose buffer is allocated on the heap. Destroys its buffer when the string object goes away.</li>
- <li><code><strong>nsAutoString / nsAutoCString</strong></code>- derived from <code>nsString</code>, a string which owns a 64 code unit buffer in the same storage space as the string itself. If a string less than 64 code units is assigned to an <code>nsAutoString</code>, then no extra storage will be allocated. For larger strings, a new buffer is allocated on the heap. If you want a number other than 64, use the templated types <code>nsAutoStringN</code> / <code>nsAutoCStringN</code>. (<code>nsAutoString</code> and <code>nsAutoCString</code> are just typedefs for <code>nsAutoStringN&lt;64&gt;</code> and <code>nsAutoCStringN&lt;64&gt;</code>, respectively.)</li>
- <li><code><strong>nsDependentString</strong></code>- derived from <code>nsString</code>, this string does <em>not</em> own its buffer. It is useful for converting a raw string (<code>const char16_t*</code> or <code>const char*</code>) into a class of type <code>nsAString</code>. Note that you must null-terminate buffers used by to nsDependentString. If you don't want to or can't null-terminate the buffer, use nsDependentSubstring.</li>
- <li><code><strong>nsPrintfCString</strong></code>- derived from <code>nsCString</code>, this string behaves like an <code>nsAutoCString</code>. The constructor takes parameters which allows it to construct a 8-bit string from a <code>printf</code>-style format string and parameter list.</li>
-</ul>
-
-<p>There are also a number of concrete classes that are created as a side-effect of helper routines, etc. You should avoid direct use of these classes. Let the string library create the class for you.</p>
-
-<ul>
- <li><code><strong>nsSubstringTuple</strong></code> - created via <a href="#String_Concatenation">string concatenation</a></li>
- <li><code><strong>nsDependentSubstring</strong></code> - created through <a href="#Substrings_.28string_fragments.29">Substring</a></li>
- <li><code><strong>nsPromiseFlatString</strong></code> - created through <code><strong><a href="#Raw_Character_Pointers">PromiseFlatString()</a></strong></code></li>
- <li><code><strong>nsLiteral[C]String</strong></code> - created through the <code>""_ns</code> and <code>u""_ns</code> user-defined literals</li>
-</ul>
-
-<p>Of course, there are times when it is necessary to reference these string classes in your code, but as a general rule they should be avoided.</p>
-
-<h2 id="Iterators">Iterators</h2>
-
-<p>Because Mozilla strings are always a single buffer, iteration over the characters in the string is done using raw pointers:</p>
-
-<pre class="notranslate">/**
- * Find whether there is a tab character in `data`
- */
-bool HasTab(const nsAString&amp; data)
-{
-  const <code>char16_t</code>* cur = data.BeginReading();
-  const <code>char16_t</code>* end = data.EndReading();
-
-  for (; cur &lt; end; ++cur) {
-    if (<code>char16_t</code>('\t') == *cur)
-      return true;
-  }
-  return false;
-}</pre>
-
-<p>Note that `end` points to the character after the end of the string buffer. It should never be dereferenced.</p>
-
-<p>Writing to a mutable string is also simple:</p>
-
-<pre class="notranslate">/**
- * Replace every tab character in `data` with a space.
- */
-void ReplaceTabs(nsAString&amp; data)
-{
-  <code>char16_t</code>* cur = data.BeginWriting();
-  <code>char16_t</code>* end = data.EndWriting();
-
-  for (; cur &lt; end; ++cur) {
-    if (<code>char16_t</code>('\t') == *cur)
-      *cur = <code>char16_t</code>(' ');
-  }
-}</pre>
-
-<p>You may change the length of a string via <code>SetLength()</code>.  Note that Iterators become invalid after changing the length of a string:</p>
-
-<pre class="eval notranslate">/**
- * Replace every tab character in `data` with four spaces.
- */
-void ReplaceTabs2(nsAString&amp; data)
-{
-  int len = data.Length();
-  <code>char16_t</code> *cur = data.BeginWriting();
-  <code>char16_t</code> *end = data.EndWriting();
-
-<span style="color: #ff0000;">  // Because `cur` may change during the loop, track the position
-  // within the string.
-  int pos = 0;</span>
-
-  while (cur &lt; end) {
-    if (<code>char16_t</code>('\t') != *cur) {
-      ++pos;
-      ++cur;
-    } else {
-      len += 3;
-      data.SetLength(len);
-
-      <span style="color: #ff0000;">// After SetLength, read `cur` and `end` again
-      cur = data.BeginWriting() + pos;
-      end = data.EndWriting();</span>
-
-      // move the remaining data over
-      if (pos &lt; len - 1)
-        memmove(cur + 4, cur + 1, (len - 1 - pos) * sizeof(<code>char16_t</code>));
-
-      // fill the tab with spaces
-      *cur = <code>char16_t</code>(' ');
-      *(cur + 1) = <code>char16_t</code>(' ');
-      *(cur + 2) = <code>char16_t</code>(' ');
-      *(cur + 3) = <code>char16_t</code>(' ');
-
-      pos += 4;
-      cur += 4;
-    }
-  }
-}
-</pre>
-
-<p>If a string buffer becomes smaller while writing it, use SetLength to inform the string class of the new size:</p>
-
-<pre class="notranslate">/**
- * Remove every tab character from `data`
- */
-void RemoveTabs(nsAString&amp; data)
-{
-  int len = data.Length();
-  <code>char16_t</code>* cur = data.BeginWriting();
-  <code>char16_t</code>* end = data.EndWriting();
-
-  while (cur &lt; end) {
-    if (<code>char16_t</code>('\t') == *cur) {
-      len -= 1;
-      end -= 1;
-      if (cur &lt; end)
-        memmove(cur, cur + 1, (end - cur) * sizeof(<code>char16_t</code>));
-    } else {
-      cur += 1;
-    }
-  }
-
-<span style="color: #ff0000;">  data.SetLength(len);</span>
-}
-</pre>
-
-<p>Note that using <code>BeginWriting()</code> to make a string <em>longer</em> is not OK. <code>BeginWriting()</code> must not be used to write past the logical length of the string indicated by <code>EndWriting()</code> or <code>Length()</code>. Calling <code>SetCapacity()</code> before <code>BeginWriting()</code> does not affect what the previous sentence says. To make the string longer, call <code>SetLength()</code> <em>before</em> <code>BeginWriting()</code> or use the <code>BulkWrite()</code> API described below.</p>
-
-<h2 id="Bulk_Write">Bulk Write</h2>
-
-<p><code>BulkWrite()</code> allows capacity-aware cache-friendly low-level writes to the string's buffer.</p>
-
-<p>Capacity-aware means that the caller is made aware of how the caller-requested buffer capacity was rounded up to mozjemalloc buckets. This is useful when initially requesting best-case buffer size without yet knowing the true size need. If the data that actually needs to be written is larger than the best-case estimate but still fits within the rounded-up capacity, there is no need to reallocate despite requesting the best-case capacity.</p>
-
-<p>Cache-friendly means that the zero terminator for C compatibility is written <em>after</em> the new content of the string has been written, so the result is a forward-only linear write access pattern instead of a non-linear back-and-forth sequence resulting from using <code>SetLength()</code> followed by <code>BeginWriting()</code>.</p>
-
-<p>Low-level means that writing via a raw pointer is possible as with <code>BeginWriting()</code>.</p>
-
-<p><code>BulkWrite()</code> takes four arguments: The new capacity (which may be rounded up), the number of code units at the beginning of the string to preserve (typically the old logical length), a boolean indicating whether reallocating a smaller buffer is OK if the requested capacity would fit in a buffer that's smaller than current one, and a reference to an <code>nsresult</code> for indicating failure on OOM. (Don't access the return value if the <code>nsresult</code> indicates failure. Unfortunately <code>mozilla::Result</code> is not versatile enough to be used here.)</p>
-
-<p><code>BulkWrite()</code> returns a <code>mozilla::BulkWriteHandle&lt;T&gt;</code>, where <code>T</code> is either <code>char</code> or <code>char16_t</code>. The actual writes are performed through this handle. You must not access the string except via the handle until you call <code>Finish()</code> on the handle in the success case or you let the handle go out of scope without calling <code>Finish()</code> in the failure case, in which case the destructor of the handle puts the string in a mostly harmless but consistent state (containing a single REPLACEMENT CHARACTER if a capacity greater than 0 was requested, or in the <code>char</code> case if the three-byte UTF-8 representation of the REPLACEMENT CHARACTER doesn't fit, an ASCII SUBSTITUTE).</p>
-
-<p><code>mozilla::BulkWriteHandle&lt;T&gt;</code> autoconverts to a writable <code>mozilla::Span&lt;T&gt;</code> and also provides explicit access to itself as <code>Span</code> (<code>AsSpan()</code>) or via component accessors named consistently with those on <code>Span</code>: <code>Elements()</code> and <code>Length()</code> the latter is not the logical length of the string but the writable length of the buffer. The buffer exposed via these methods includes the prefix that you may have requested to be preserved. It's up to you to skip past it so as to not overwrite it.</p>
-
-<p>If there's a need to request a different capacity before you are ready to call <code>Finish()</code>, you can call <code>RestartBulkWrite()</code> on the handle. It takes three arguments that match the first three arguments of <code>BulkWrite()</code>. It returns <code>mozilla::Result&lt;mozilla::Ok, nsresult&gt;</code> to indicate success or OOM. Calling <code>RestartBulkWrite()</code> invalidates previously-obtained span, raw pointer or length.</p>
-
-<p>Once you are done writing, call <code>Finish()</code>. It takes two arguments: the new logical length of the string (which must not exceed the capacity retuned by the <code>Length()</code> method of the handle) and a boolean indicating whether it's OK to attempt to reallocate a smaller buffer in case a smaller mozjemalloc bucket could accommodate the new logical length.</p>
-
-<h2 id="Helper_Classes_and_Functions">Helper Classes and Functions</h2>
-
-<h3 id="Converting_Cocoa_strings">Converting Cocoa strings</h3>
-
-<p>Use <code>mozilla::CopyCocoaStringToXPCOMString()</code> in <code>mozilla/MacStringHelpers.h</code> to convert Cocoa strings to XPCOM strings.</p>
-
-<h3 id="Searching_strings_-_looking_for_substrings_characters_etc.">Searching strings - looking for substrings, characters, etc.</h3>
-
-<p><code>FindInReadable()</code> is the replacement for the old <code>string.Find(..)</code>. The syntax is:</p>
-
-<pre class="eval notranslate">PRBool FindInReadable(const nsAString&amp; pattern,
-                      nsAString::const_iterator start, nsAString::const_iterator end,
-                      nsStringComparator&amp; aComparator = nsDefaultStringComparator());
-</pre>
-
-<p>To use this, <code>start</code> and <code>end</code> should point to the beginning and end of a string that you would like to search. If the search string is found, <code>start</code> and <code>end</code> will be adjusted to point to the beginning and end of the found pattern. The return value is PR_TRUE or PR_FALSE, indicating whether or not the string was found.</p>
-
-<p>An example:</p>
-
-<pre class="eval notranslate">const nsAString&amp; str = GetSomeString();
-nsAString::const_iterator start, end;
-
-str.BeginReading(start);
-str.EndReading(end);
-
-constexpr auto valuePrefix = u"value="_ns;
-
-if (FindInReadable(valuePrefix, start, end)) {
-    // end now points to the character after the pattern
-    valueStart = end;
-
-}
-</pre>
-
-<h3 id="Checking_for_Memory_Allocation_failure">Checking for Memory Allocation failure</h3>
-
-<p>The String classes now use infallible memory allocation, so you do not need to check for success when allocating/resizing "normal" strings. </p>
-
-<p>Most of the functions that modify Strings (Assign(), SetLength(), etc) also have a version that takes a "mozilla::fallible_t" parameter.  These versions return 'false' instead of aborting if allocation fails .  Use them when creating/allocating Strings which may be very large, and which the program could recover from if the allocation fails.</p>
-
-<h3 id="Getting_a_char_*_buffer_from_a_String">Getting a char * buffer from a String</h3>
-
-<p>You can access a String's internal buffer using the <a href="/en-US/docs/Mozilla/Tech/XPCOM/Guide/Internal_strings#Iterators">iterator methods</a>. The String retains ownership over the buffer at all times.</p>
-
-<p>In other words, there is no way to "grab" the internal char * from a String, i.e. have the string "forget" about it and hand off ownership to other code.  Sorry.</p>
-
-<p>If you wish to make a copy of a String into a new character buffer (<code><code>char16_t</code>*</code>/<code>char*</code>), the preferred way is to allocate it with one of the following methods:</p>
-
-<ul>
- <li><code><strong><code>char16_t</code>* ToNewUnicode(<em>nsAString&amp;</em>)</strong></code> - Allocates a <code>char16_t</code><code>*</code>buffer from an <code>nsAString</code>.</li>
- <li><code><strong>char *ToNewCString(<em>nsACString&amp;</em>)</strong></code> - Allocates a <code>char*</code>buffer from an <code>nsACString</code>. Note that this method will also work on nsAStrings, but it will do an implicit <a href="#Lossy_Conversion">lossy conversion</a>. This function should only be used if the input is known to be strictly ASCII. Often a conversion to UTF-8 is more appropriate. See <code><strong>ToNewUTF8String</strong></code> below.</li>
- <li><code><strong>char* ToNewUTF8String(<em>nsAString&amp;</em>)</strong></code> - Allocates a new <code>char*</code> buffer containing the UTF-8 encoded version of the given nsAString. See <a href="#Unicode_Conversion_ns.2ACString_vs._ns.2AString">Unicode Conversion</a> for more details and for better ways that don't require you to manage the memory yourself.</li>
-</ul>
-
-<p>These methods return a buffer allocated using XPCOM's allocator instead of the traditional allocator (<code>malloc</code>, etc.). Outside of libxul you should use <code>NS_Free</code> to deallocate the result when you no longer need it, inside libxul <code>free()</code> is preferred..</p>
-
-<h3 id="Substrings_string_fragments">Substrings (string fragments)</h3>
-
-<p>It is very simple to refer to a substring of an existing string without actually allocating new space and copying the characters into that substring. <code>Substring()</code> is the preferred method to create a reference to such a string.</p>
-
-<pre class="eval notranslate">void ProcessString(const nsAString&amp; str) {
-    const nsAString&amp; firstFive = Substring(str, 0, 5); // from index 0, length 5
-    // firstFive is now a string representing the first 5 characters
-}
-</pre>
-
-<h2 id="Unicode_Conversion_ns*CString_vs._ns*String">Unicode Conversion ns*CString vs. ns*String</h2>
-
-<p>Strings can be <em>stored</em> in two basic formats: 8-bit code unit (byte/<code>char</code>) strings, or 16-bit code unit (<code>char16_t</code>) strings. Any string class with a capital "C" in the classname contains 8-bit bytes. These classes include <code>nsCString</code>, <code>nsDependentCString</code>, and so forth. Any string class <em>without</em> the "C" contains 16-bit code units.</p>
-
-<p>A 8-bit string can be in one of many character encodings while a 16-bit string is always in potentially-invalid UTF-16. (You can make a 16-bit string guaranteed-valid UTF-16 by passing it to <code>EnsureUTF16Validity()</code>.) The most common encodings are:</p>
-
-<ul>
- <li>ASCII - 7-bit encoding for basic English-only strings. Each ASCII value is stored in exactly one byte in the array with the most-significant 8th bit set to zero.</li>
- <li><a class="external" href="http://www.unicode.org/glossary/#UCS_2">UCS2</a> - 16-bit encoding for a <em>subset</em> of Unicode, <a class="external" href="http://www.unicode.org/glossary/#BMP">BMP</a>. The Unicode value of a character stored in UCS2 is stored in exactly one 16-bit <code>char16_t</code> in a string class.</li>
- <li><a class="external" href="http://www.faqs.org/rfcs/rfc3629.html">UTF-8</a> - 8-bit encoding for Unicode characters. Each Unicode characters is stored in up to 4 bytes in a string class. UTF-8 is capable of representing the entire Unicode character repertoire, and it efficiently maps to <a class="external" href="http://www.unicode.org/glossary/#UTF_32">UTF-32</a>. (Gtk and Rust natively use UTF-8.)</li>
- <li><a class="external" href="http://www.unicode.org/glossary/#UTF_16">UTF-16</a> - 16-bit encoding for Unicode storage, backwards compatible with UCS2. The Unicode value of a character stored in UTF-16 may require <em>one or two</em> 16-bit <code>char16_t</code>s in a string class. The contents of <code>nsAString</code> always has to be regarded as in this encoding instead of UCS2. UTF-16 is capable of representing the entire Unicode character repertoire, and it efficiently maps to UTF-32. (Win32 W APIs and Mac OS X natively use UTF-16.)</li>
- <li>Latin1 - 8-bit encoding for the first 256 Unicode code points. Used for HTTP headers and for size-optimized storage in text node and SpiderMonkey strings. Latin1 converts to UTF-16 by zero-extending each byte to a 16-bit code unit. Note that this kind of "Latin1" is not available for encoding HTML, CSS, JS, etc. Specifying <code>charset=latin1</code> means the same as <code>charset=windows-1252</code>. Windows-1252 is a similar but different encoding used for interchange.</li>
-</ul>
-
-<p>In addition, there exist multiple other (legacy) encodings. The Web-relevant ones are defined in the <a href="https://encoding.spec.whatwg.org/">Encoding Standard</a>. Conversions from these encodings to UTF-8 and UTF-16 are provided by <a href="https://searchfox.org/mozilla-central/source/intl/Encoding.h#109">mozilla::Encoding</a>. Additonally, on Windows the are some rare cases (e.g. drag&amp;drop) where it's necessary to call a system API with data encoded in the Windows locale-dependent legacy encoding instead of UTF-16. In those rare cases, use <code>MultiByteToWideChar</code>/<code>WideCharToMultiByte</code> from kernel32.dll. Do not use <code>iconv</code> on *nix. We only support UTF-8-encoded file paths on *nix, non-path Gtk strings are always UTF-8 and Cocoa and Java strings are always UTF-16.</p>
-
-<p>When working with existing code, it is important to examine the current usage of the strings that you are manipulating, to determine the correct conversion mechanism.</p>
-
-<p>When writing new code, it can be confusing to know which storage class and encoding is the most appropriate. There is no single answer to this question, but the important points are:</p>
-
-<ul>
- <li><strong>Surprisingly many strings are very often just ASCII.</strong> ASCII is a subset of UTF-8 and is, therefore, efficient to represent as UTF-8. Representing ASCII as UTF-16 bad both for memory usage and cache locality.</li>
- <li><strong>Rust strongly prefers UTF-8.</strong> If your C++ code is interacting with Rust code, using UTF-8 in nsACString and merely validating it when converting to Rust strings is more efficient than using nsAString on the C++ side.</li>
- <li><strong>Networking code prefers 8-bit strings.</strong> Networking code tends to use 8-bit strings: either with UTF-8 or Latin1 (byte value is the Unicode scalar value) semantics.</li>
- <li><strong>JS and DOM prefer UTF-16.</strong> Most Gecko code uses UTF-16 for compatibility with JS strings and DOM string which are potentially-invalid UTF-16. However, both DOM text nodes and JS strings store strings that only contain code points below U+0100 as Latin1 (byte value is the Unicode scalar value).</li>
- <li><strong>Windows and Cocoa use UTF-16.</strong> Windows system APIs take UTF-16. Cocoa NSString is UTF-16.</li>
- <li><strong>Gtk uses UTF-8.</strong> Gtk APIs take UTF-8 for non-file paths. In the Gecko case, we support only UTF-8 file paths outside Windows, so all Gtk strings are UTF-8 for our purposes though file paths received from Gtk may not be valid UTF-8.</li>
-</ul>
-
-<p>To assist with ASCII, Latin1, UTF-8, and UTF-16 conversions, there are some helper methods and classes. Some of these classes look like functions, because they are most often used as temporary objects on the stack.</p>
-
-<h3 id="Short_zero-terminated_ASCII_strings">Short zero-terminated ASCII strings</h3>
-
-<p>If you have a short zero-terminated string that you are certain is always ASCII, use these special-case methods instead of the conversions described in the later sections.</p>
-
-<ul>
- <li>If you are assigning an ASCII literal to an <code>nsACString</code>, use <code>AssignLiteral()</code>.</li>
- <li>If you are assigning a literal to an <code>nsAString</code>, use <code>AssignLiteral()</code> and make the literal a <code>u""</code> literal. If the literal has to be a <code>""</code> literal (as opposed to <code>u""</code>) and is ASCII, still use <code>AppendLiteral()</code>, but be aware that this involves a run-time inflation.</li>
- <li>If you are assigning a zero-terminated ASCII string that's not a literal from the compiler's point of view at the call site and you don't know the length of the string either (e.g. because it was looked up from an array of literals of varying lengths), use <code>AssignASCII()</code>.</li>
-</ul>
-
-<h3 id="UTF-8_UTF-16_conversion">UTF-8 / UTF-16 conversion</h3>
-
-<p><code><strong>NS_ConvertUTF8toUTF16(<em>const nsACString&amp;</em>)</strong></code> - a <code>nsAutoString</code> subclass that converts a UTF-8 encoded <code>nsACString</code> or <code>const char*</code> to a 16-bit UTF-16 string. If you need a <code>const <code>char16_t</code>*</code> buffer, you can use the <code>.get()</code> method. For example:</p>
-
-<pre class="eval notranslate">/* signature: void HandleUnicodeString(const nsAString&amp; str); */
-object-&gt;HandleUnicodeString(<strong>NS_ConvertUTF8toUTF16</strong>(utf8String));
-
-/* signature: void HandleUnicodeBuffer(const <code>char16_t</code>* str); */
-object-&gt;HandleUnicodeBuffer(<strong>NS_ConvertUTF8toUTF16</strong>(utf8String).get());
-</pre>
-
-<p><code><strong>NS_ConvertUTF16toUTF8(<em>const nsAString&amp;</em>)</strong></code> - a <code>nsAutoCString</code> which converts a 16-bit UTF-16 string (<code>nsAString</code>) to a UTF-8 encoded string. As above, you can use <code>.get()</code> to access a <code>const char*</code> buffer.</p>
-
-<pre class="eval notranslate">/* signature: void HandleUTF8String(const nsACString&amp; str); */
-object-&gt;HandleUTF8String(<strong>NS_ConvertUTF16toUTF8</strong>(utf16String));
-
-/* signature: void HandleUTF8Buffer(const char* str); */
-object-&gt;HandleUTF8Buffer(<strong>NS_ConvertUTF16toUTF8</strong>(utf16String).get());
-</pre>
-
-<p><code><strong>CopyUTF8toUTF16(<em>const nsACString&amp;, nsAString&amp;</em>)</strong></code> - converts and copies:</p>
-
-<pre class="eval notranslate">// return a UTF-16 value
-void Foo::GetUnicodeValue(nsAString&amp; result) {
-    <strong>CopyUTF8toUTF16</strong>(mLocalUTF8Value, result);
- }
-</pre>
-
-<p><code><strong>AppendUTF8toUTF16(<em>const nsACString&amp;, nsAString&amp;</em>)</strong></code> - converts and appends:</p>
-
-<pre class="eval notranslate">// return a UTF-16 value
-void Foo::GetUnicodeValue(nsAString&amp; result) {
-    result.AssignLiteral("prefix:");
-    <strong>AppendUTF8toUTF16</strong>(mLocalUTF8Value, result);
-}
-</pre>
-
-<p><br>
- <code><strong>UTF8ToNewUnicode(<em>const nsACString&amp;, PRUint32* aUTF16Count = nsnull</em>)</strong></code> - (avoid if possible) allocates and converts (the optional parameter will contain the number of 16-byte units upon return, if non-null):</p>
-
-<pre class="eval notranslate">void Foo::GetUTF16Value(<code>char16_t</code>** result) {
-    *result = <strong>UTF8ToNewUnicode</strong>(mLocalUTF8Value);
-}
-</pre>
-
-<p><br>
- <code><strong>CopyUTF16toUTF8(<em>const nsAString&amp;, nsACString&amp;</em>)</strong></code> - converts and copies:</p>
-
-<pre class="eval notranslate">// return a UTF-8 value
-void Foo::GetUTF8Value(nsACString&amp; result) {
-    <strong>CopyUTF16toUTF8</strong>(mLocalUTF16Value, result);
-}
-</pre>
-
-<p><code><strong>AppendUTF16toUTF8(<em>const nsAString&amp;, nsACString&amp;</em>)</strong></code> - converts and appends:</p>
-
-<pre class="eval notranslate">// return a UTF-8 value
-void Foo::GetUnicodeValue(nsACString&amp; result) {
-    result.AssignLiteral("prefix:");
-    <strong>AppendUTF16toUTF8</strong>(mLocalUTF16Value, result);
-}
-</pre>
-
-<p><code><strong>ToNewUTF8String(<em>const nsAString&amp;</em>)</strong></code> - (avoid if possible) allocates and converts:</p>
-
-<pre class="eval notranslate">void Foo::GetUTF8Value(char** result) {
-    *result = <strong>ToNewUTF8String</strong>(mLocalUTF16Value);
-}
-</pre>
-
-<h3 id="Latin1_UTF-16_Conversion">Latin1 / UTF-16 Conversion</h3>
-
-<p>The following should only be used when you can guarantee that the original string is ASCII or Latin1 (in the sense that the byte value is the Unicode scalar value; not in the windows-1252 sense). These helpers are very similar to the UTF-8 / UTF-16 conversion helpers above.</p>
-
-<h4 id="UTF-16_to_Latin1_converters">UTF-16 to Latin1 converters</h4>
-
-<p>These converters are <em><strong>very dangerous</strong></em> because they <em><strong>lose information</strong></em> during the conversion process. You should <em><strong>avoid UTF-16 to Latin1 conversions</strong></em> unless your strings are guaranteed to be Latin1 or ASCII. (In the future, these conversions may start asserting in debug builds that their input is in the permissible range.) If the input is actually in the Latin1 range, each 16-bit code unit in narrowed to an 8-bit byte by removing the high half. Unicode code points above U+00FF result in garbage whose nature must not be relied upon. (In the future the nature of the garbage will be CPU architecture-dependent.) If you want to <code>printf()</code> something and don't care what happens to non-ASCII, please convert to UTF-8 instead.</p>
-
-<ul>
- <li><code><strong>NS_LossyConvertUTF16toASCII(<em>nsAString</em>)</strong></code> - a <code>nsAutoCString</code> which holds a temporary buffer containing the Latin1 value of the string.</li>
- <li><code><strong>LossyCopyUTF16toASCII(<em>nsAString, nsACString</em>)</strong></code> - does an in-place conversion from UTF-16 into an Latin1 string object.</li>
- <li><code><strong>LossyAppendUTF16toASCII(<em>nsAString, nsACString</em>)</strong></code> - appends an UTF-16 string to an Latin1 string.</li>
- <li><code><strong>ToNewCString(<em>nsAString</em>)</strong></code> - (avoid if ) allocates a new zero-terminated Latin1 <code>char*</code> string.</li>
-</ul>
-
-<h4 id="Latin1_to_UTF-16_converters">Latin1 to UTF-16 converters</h4>
-
-<p>These converters are <em><strong>very dangerous</strong></em> because they will <em><strong>produce wrong results for non-ASCII UTF-8 or windows-1252 input</strong></em> into a meaningless UTF-16 string. You should <em><strong>avoid ASCII to UTF-16 conversions</strong></em> unless your strings are guaranteed to be ASCII or Latin1 in the sense of the byte value being the Unicode scalar value. Every byte is zero-extended into a 16-bit code unit.</p>
-
-<p>It is correct to use these on most HTTP header values, but <em><strong>it's always wrong to use these on HTTP response bodies!</strong></em> (Use <code>mozilla::Encoding</code> to deal with response bodies.)</p>
-
-<ul>
- <li><code><strong>NS_ConvertASCIItoUTF16(<em>nsACString</em>)</strong></code> - a <code>nsAutoString</code> which holds a temporary buffer containing the value of the Latin1 to UTF-16 conversion.</li>
- <li><code><strong>CopyASCIItoUTF16(<em>nsACString, nsAString</em>)</strong></code> - does an in-place conversion from Latin1 to UTF-16.</li>
- <li><code><strong>AppendASCIItoUTF16(<em>nsACString, nsAString</em>)</strong></code> - appends a Latin1 string to a UTF-16 string.</li>
- <li><code><strong>ToNewUnicode(<em>nsACString</em>)</strong></code> - (Avoid if possible) Creates a new zero-terminated <code><code>char16_t</code>*</code> string which contains the value of the Latin1 to UTF-16 conversion.</li>
-</ul>
-
-<h3 id="Comparing_ns*Strings_with_C_strings">Comparing ns*Strings with C strings</h3>
-
-<p>You can compare ns*Strings with C strings by converting the ns*String to a C string, or by comparing directly against a C String.</p>
-
-<ul>
- <li><code><strong>PromiseFlatCString(<em>nsACString</em>).get()</strong></code> - creates a temporary <code>char *</code> out of a nsACString. This can be compared to a C String using C functions.</li>
- <li><code><strong><em>ns*String</em>.EqualsASCII(<em>const char *</em>)</strong></code> - compares with an ascii C string.</li>
- <li><code><strong><em>ns*String</em>.EqualsLiteral</strong></code> - compares with a string literal.</li>
-</ul>
-
-<h2 id="Common_Patterns">Common Patterns</h2>
-
-<h3 id="Callee-allocated_Parameters">Callee-allocated Parameters</h3>
-
-<p>Many APIs result in a method allocating a buffer in order to return strings to its caller. This can be tricky because the caller has to remember to free the string when they have finished using it. Fortunately, the <code>getter_Copies()</code> function makes this very easy.</p>
-
-<p>A method may look like this:</p>
-
-<pre class="eval notranslate">void GetValue(<code>char16_t</code>** aValue)
-{
-    *aValue = ToNewUnicode(foo);
-}
-</pre>
-
-<p>Without <code>getter_Copies()</code>, the caller would need to free the string:</p>
-
-<pre class="notranslate">{
-    <code>char16_t</code>* val;
-    GetValue(&amp;val);
-
-    if (someCondition) {
-        // don't forget to free the value!
-        NS_Free(val);
-        return NS_ERROR_FAILURE;
-    }
-
-    ...
-    // and later, still don't forget to free!
-    NS_Free(val);
-}
-</pre>
-
-<p>With <code>getter_copies()</code> you never have to worry about this. You can just use <code>getter_Copies()</code> to wrap a string class argument, and the class will remember to free the buffer when it goes out of scope:</p>
-
-<pre class="notranslate">{
-    nsString val;
-    GetValue(getter_Copies(val));
-
-    // val will free itself here
-    if (someCondition)
-        return NS_ERROR_FAILURE;
-    ...
-    // and later, still nothing to free
-}
-</pre>
-
-<p>The resulting code is much simpler, and easy to read.</p>
-
-<h3 id="Literal_Strings">Literal Strings</h3>
-
-<p>A <em>literal string</em> is a raw string value that is written in some C++ code. For example, in the statement <code>printf("Hello World\n");</code> the value <code>"Hello World\n"</code> is a literal string. It is often necessary to insert literal string values when an <code>nsAString</code> or <code>nsACString</code> is required. Two user-defined literals are provided that implicitly convert to <code>const nsString&amp;</code> resp. <code>const nsCString&amp;</code>:</p>
-
-<ul>
- <li><code>""_ns</code> for 8-bit literals, converting implicitly to <code>const nsCString&amp;</code></li>
- <li><code>u""_ns</code> for 16-bit literals, converting implicitly to <code>const nsString&amp;</code></li>
-</ul>
-
-<p>The benefits of the user-defined literals may seem unclear, given that <code>nsDependentCString</code> will also wrap a string value in an <code>nsCString</code>. The advantage of the user-defined literals is that the length of these strings is calculated at compile time, so the string does not need to be scanned at runtime to determine its length.</p>
-
-<p>Nowadays, all supported platforms have 16-bit literals using u""_ns, so we longer need to rely on macros for portability.</p>
-
-<pre class="notranslate">// call Init(const <code>char16_t</code>*) - bad signature, will need to do runtime length calculation inside
-Init(L"start value"); // bad - L"..." is not portable!
-Init(NS_ConvertASCIItoUTF16("start value").get()); // bad - runtime ASCII-&gt;UTF-16 conversion!
-Init(u"start value"); // less bad, portable and no runtime conversion
-
-// call Init(const nsAString&amp;)
-Init(nsDependentString(L"start value")); // bad - not portable!
-Init(NS_ConvertASCIItoUTF16("start value")); // bad - runtime ASCII-&gt;UTF-16 conversion!
-
-// call Init(const nsACString&amp;)
-Init(nsDependentCString("start value")); // bad - length determined at runtime
-</pre>
-
-<p>Here are some examples of proper usage of the literals (both standard and user-defined):</p>
-
-<pre class="notranslate">// call Init(const nsLiteralString&amp;) - enforces that it's only called with literals
-Init(u"start value"_ns);
-
-// call Init(const nsAString&amp;)
-Init(u"start value"_ns);
-
-// call Init(const nsACString&amp;)
-Init("start value"_ns);
-</pre>
-
-<p>In case a literal is defined via a macro, you can just convert it to <code>nsLiteralString</code> or <code>nsLiteralCString</code> using their constructor. You could consider not using a macro at all but a named <code>constexpr</code> constant instead.</p>
-
-<p>In some cases, an 8-bit literal is defined via a macro, either within code or from the environment, but it can't be changed or is used both as an 8-bit and a 16-bit string. In these cases, you can use the <code>NS_LITERAL_STRING_FROM_CSTRING </code>macro to construct a <code>nsLiteralString </code>and do the conversion at compile-time.</p>
-
-<h3 id="String_Concatenation">String Concatenation</h3>
-
-<p>Strings can be concatenated together using the + operator. The resulting string is a <code>const nsSubstringTuple</code> object. The resulting object can be treated and referenced similarly to a <code>nsAString</code> object. Concatenation <em>does not copy the substrings</em>. The strings are only copied when the concatenation is assigned into another string object. The <code>nsSubstringTuple</code> object holds pointers to the original strings. Therefore, the <code>nsSubstringTuple</code> object is dependent on all of its substrings, meaning that their lifetime must be at least as long as the <code>nsSubstringTuple</code> object.</p>
-
-<p>For example, you can use the value of two strings and pass their concatenation on to another function which takes an <code>const nsAString&amp;:</code></p>
-
-<pre class="eval notranslate">void HandleTwoStrings(const nsAString&amp; one, const nsAString&amp; two) {
-    // call HandleString(const nsAString&amp;)
-    HandleString(one + two);
-}
-</pre>
-
-<p>NOTE: The two strings are implicitly combined into a temporary <code>nsString</code> in this case, and the temporary string is passed into <code>HandleString</code>. If <code>HandleString</code> assigns its input into another <code>nsString</code>, then the string buffer will be shared in this case negating the cost of the intermediate temporary. You can concatenate N strings and store the result in a temporary variable:</p>
-
-<pre class="eval notranslate">constexpr auto start = u"start "_ns;
-constexpr auto middle = u"middle "_ns;
-constexpr auto end = u"end"_ns;
-// create a string with 3 dependent fragments - no copying involved!
-nsString combinedString = start + middle + end;
-
-// call void HandleString(const nsAString&amp;);
-HandleString(combinedString);
-</pre>
-
-<p>It is safe to concatenate user-defined literals because the temporary <code>nsLiteral[C]String </code>objects will live as long as the temporary concatenation object (of type <code>nsSubstringTuple</code>).</p>
-
-<pre class="eval notranslate">// call HandlePage(const nsAString&amp;);
-// safe because the concatenated-string will live as long as its substrings
-HandlePage(u"start "_ns + u"end"_ns);
-</pre>
-
-<h3 id="Local_variables">Local variables</h3>
-
-<p>Local variables within a function are usually stored on the stack. The <code>nsAutoString/nsAutoCString</code> classes are derivatives of the <code>nsString/nsCString classes</code>. They own a 64-character buffer allocated in the same storage space as the string itself. If the <code>nsAutoString</code> is allocated on the stack, then it has at its disposal a 64-character stack buffer. This allows the implementation to avoid allocating extra memory when dealing with small strings. <code>nsAutoStringN</code>/<code>nsAutoCStringN</code> are more general alternatives that let you choose the number of characters in the inline buffer.</p>
-
-<pre class="eval notranslate">...
-nsAutoString value;
-GetValue(value); // if the result is less than 64 code units,
-                 // then this just saved us an allocation
-...
-</pre>
-
-<h3 id="Member_variables">Member variables</h3>
-
-<p>In general, you should use the concrete classes <code>nsString</code> and <code>nsCString</code> for member variables.</p>
-
-<pre class="eval notranslate">class Foo {
-    ...
-    // these store UTF-8 and UTF-16 values respectively
-    nsCString mLocalName;
-    nsString mTitle;
-};
-</pre>
-
-<p>Note that the strings are declared directly in the class, not as pointers to strings. Don't do this:</p>
-
-<pre class="notranslate">class Foo {
-public:
-    Foo() {
-        mLocalName = new nsCString();
-        mTitle = new nsString();
-    }
-    ~Foo() { delete mLocalName; delete mTitle; }
-
-private:
-    // these store UTF-8 and UTF-16 values respectively
-    nsCString* mLocalName;
-    nsString*  mTitle;
-};
-</pre>
-
-<p>The above code may appear to save the cost of the string objects, but <code>nsString/nsCString</code> are small objects - the overhead of the allocation outweighs the few bytes you'd save by keeping a pointer.</p>
-
-<p>Another common incorrect pattern is to use <code>nsAutoString/nsAutoCString</code> for member variables. As described in <a href="#Local_variables">Local Variables</a>, these classes have a built in buffer that make them very large. This means that if you include them in a class, they bloat the class by 64 bytes (<code>nsAutoCString</code>) or 128 bytes (<code>nsAutoString</code>).</p>
-
-<p>An example:</p>
-
-<pre class="notranslate">class Foo {
-    ...
-
-    // bloats 'Foo' by 128 bytes!
-    nsAutoString mLocalName;
-};
-</pre>
-
-<h3 id="Raw_Character_Pointers">Raw Character Pointers</h3>
-
-<p><code>PromiseFlatString()</code> and <code>PromiseFlatCString() </code>can be used to create a temporary buffer which holds a null-terminated buffer containing the same value as the source string. <code>PromiseFlatString()</code> will create a temporary buffer if necessary. This is most often used in order to pass an <code>nsAString</code> to an API which requires a null-terminated string.</p>
-
-<p>In the following example, an <code>nsAString</code> is combined with a literal string, and the result is passed to an API which requires a simple character buffer.</p>
-
-<pre class="eval notranslate">// Modify the URL and pass to AddPage(const <code>char16_t</code>* url)
-void AddModifiedPage(const nsAString&amp; url) {
-    constexpr auto httpPrefix = u<span class="nowiki">"http://"</span>_ns;
-    const nsAString&amp; modifiedURL = httpPrefix + url;
-
-    // creates a temporary buffer
-    AddPage(PromiseFlatString(modifiedURL).get());
-}
-</pre>
-
-<p><code>PromiseFlatString()</code> is smart when handed a string that is already null-terminated. It avoids creating the temporary buffer in such cases.</p>
-
-<pre class="eval notranslate">// Modify the URL and pass to AddPage(const <code>char16_t</code>* url)
-void AddModifiedPage(const nsAString&amp; url, PRBool addPrefix) {
-    if (addPrefix) {
-        // MUST create a temporary buffer - string is multi-fragmented
-        constexpr auto httpPrefix = u<span class="nowiki">"http://"</span>_ns;
-        AddPage(PromiseFlatString(httpPrefix + modifiedURL));
-    } else {
-        // MIGHT create a temporary buffer, does a runtime check
-        AddPage(PromiseFlatString(url).get());
-    }
-}
-</pre>
-
-<h3 id="printf_and_a_UTF-16_string"><code>printf</code> and a UTF-16 string</h3>
-
-<p>For debugging, it's useful to <code>printf</code> a UTF-16 string (<code>nsString</code>, <code>nsAutoString</code>, etc). To do this usually requires converting it to an 8-bit string, because that's what printf expects. Use:</p>
-
-<pre class="eval notranslate">printf("%s\n", NS_ConvertUTF16toUTF8(yourString).get());
-</pre>
-
-<h3 id="Sequence_of_appends_without_reallocating">Sequence of appends without reallocating</h3>
-
-<p><code>SetCapacity()</code> allows you to give the string a hint of the future string length caused by a sequence of appends (excluding appends that convert between UTF-16 and UTF-8 in either direction) in order to avoid multiple allocations during the sequence of appends. However, the <em>other</em> allocation-avoidance features of XPCOM strings interact badly with <code>SetCapacity()</code> making it something of a footgun.</p>
-
-<p><code>SetCapacity()</code> is appropriate to use before a sequence of multiple operations from the following list (without operations that are not on the list between the <code>SetCapacity()</code> call and operations from the list):</p>
-
-<ul>
- <li><code>Append()</code></li>
- <li><code>AppendASCII()</code></li>
- <li><code>AppendLiteral()</code></li>
- <li><code>AppendPrintf()</code></li>
- <li><code>AppendInt()</code></li>
- <li><code>AppendFloat()</code></li>
- <li><code>LossyAppendUTF16toASCII()</code></li>
- <li><code>AppendASCIItoUTF16()</code></li>
-</ul>
-
-<p><em><strong>DO NOT</strong></em> call <code>SetCapacity()</code> if the subsequent operations on the string do not meet the criteria above. Operations that undo the benefits of <code>SetCapacity()</code> include but are not limited to:</p>
-
-<ul>
- <li><code>SetLength()</code></li>
- <li><code>Truncate()</code></li>
- <li><code>Assign()</code></li>
- <li><code>AssignLiteral()</code></li>
- <li><code>Adopt()</code></li>
- <li><code>CopyASCIItoUTF16()</code></li>
- <li><code>LossyCopyUTF16toASCII()</code></li>
- <li><code>AppendUTF16toUTF8()</code></li>
- <li><code>AppendUTF8toUTF16()</code></li>
- <li><code>CopyUTF16toUTF8()</code></li>
- <li><code>CopyUTF8toUTF16()</code></li>
-</ul>
-
-<p>If your string is an <code>nsAuto[C]String</code> and you are calling <code>SetCapacity()</code> with a constant N, please instead declare the string as <code>nsAuto[C]StringN&lt;</code>N+1<code>&gt;</code> without calling <code>SetCapacity()</code> (while being mindful of not using such a large N as to overflow the run-time stack).</p>
-
-<p>There is no need to include room for the null terminator: it is the job of the string class.</p>
-
-<p>Note: Calling <code>SetCapacity()</code> does not give you permission to use the pointer obtained from <code>BeginWriting()</code> to write past the current length (as returned by <code>Length()</code>) of the string. Please use either <code>BulkWrite()</code> or <code>SetLength()</code> instead.</p>
-
-<h2 id="IDL">IDL</h2>
-
-<p>The string library is also available through IDL. By declaring attributes and methods using the specially defined IDL types, string classes are used as parameters to the corresponding methods.</p>
-
-<h3 id="IDL_String_types">IDL String types</h3>
-
-<p>The C++ signatures follow the abstract-type convention described above, such that all method parameters are based on the <a href="#The_Abstract_Classes">abstract classes</a>. The following table describes the purpose of each string type in IDL.</p>
-
-<table class="standard-table">
- <tbody>
-  <tr>
-   <th class="header">IDL type</th>
-   <th class="header">C++ Type</th>
-   <th class="header">Purpose</th>
-  </tr>
-  <tr>
-   <td><code>string</code></td>
-   <td><code>char*</code></td>
-   <td>Raw character pointer to ASCII (7-bit) string, no string classes used. High bit is not guaranteed across XPConnect boundaries.</td>
-  </tr>
-  <tr>
-   <td><code>wstring</code></td>
-   <td><code><code>char16_t</code>*</code></td>
-   <td>Raw character pointer to UTF-16 string, no string classes used.</td>
-  </tr>
-  <tr>
-   <td><code>AString</code></td>
-   <td><code>nsAString</code></td>
-   <td>UTF-16 string.</td>
-  </tr>
-  <tr>
-   <td><code>ACString</code></td>
-   <td><code>nsACString</code></td>
-   <td>8-bit string. All bits are preserved across XPConnect boundaries.</td>
-  </tr>
-  <tr>
-   <td><code>AUTF8String</code></td>
-   <td><code>nsACString</code></td>
-   <td>UTF-8 string. Converted to UTF-16 as necessary when value is used across XPConnect boundaries.</td>
-  </tr>
-  <tr>
-   <td><code>DOMString</code></td>
-   <td><code>nsAString</code></td>
-   <td>UTF-16 string type used in the DOM. The same as <code>AString</code> with a few odd XPConnect exceptions: When the special JavaScript value <code>null</code> is passed to a <code>DOMString</code> parameter of an XPCOM method, it becomes a void <code>DOMString</code>. The special JavaScript value <code>undefined</code> becomes the string <code>"undefined"</code>.</td>
-  </tr>
- </tbody>
-</table>
-
-<h3 id="C_Signatures">C++ Signatures</h3>
-
-<p>In IDL, <code>in</code> parameters are read-only, and the C++ signatures for <code>*String</code> parameters follows the above guidelines by using <code>const nsAString&amp;</code> for these parameters. <code>out</code> and <code>inout</code> parameters are defined simply as <code>nsAString</code> so that the callee can write to them.</p>
-
-<table class="standard-table">
- <tbody>
-  <tr>
-   <th class="header">IDL</th>
-   <th class="header">C++</th>
-  </tr>
-  <tr>
-   <td>
-    <pre class="eval notranslate">
-interface nsIFoo : nsISupports {
-
-    attribute AString utf16String;
-
-
-
-
-    AUTF8String getValue(in ACString key);
-
-};
-</pre>
-   </td>
-   <td>
-    <pre class="eval notranslate">
-class nsIFoo : public nsISupports {
-
-     NS_IMETHOD GetUtf16String(nsAString&amp;
-                               aResult) = 0;
-     NS_IMETHOD SetUtf16String(const nsAString&amp;
-                              aValue) = 0;
-
-     NS_IMETHOD GetValue(const nsACString&amp; aKey,
-                     nsACString&amp; aResult) = 0;
-};
-</pre>
-   </td>
-  </tr>
- </tbody>
-</table>
-
-<p>In the above example, <code>utf16String</code> is treated as a UTF-16 string. The implementation of <code>GetUtf16String()</code> will use <code>aResult.Assign</code> to "return" the value. In <code>SetUtf16String()</code> the value of the string can be used through a variety of methods including <a href="#Iterators">Iterators</a>, <code><a href="#Raw_Character_Pointers">PromiseFlatString</a></code>, and assignment to other strings.</p>
-
-<p>In <code>GetValue()</code>, the first parameter, <code>aKey</code>, is treated as a raw sequence of 8-bit values. Any non-ASCII characters in <code>aKey</code> will be preserved when crossing XPConnect boundaries. The implementation of <code>GetValue()</code> will assign a UTF-8 encoded 8-bit string into <code>aResult</code>. If the <code>this</code> method is called across XPConnect boundaries, such as from a script, then the result will be decoded from UTF-8 into UTF-16 and used as a Unicode value.</p>
-
-<h3 id="Choosing_a_string_type">Choosing a string type</h3>
-
-<p>It can be difficult to determine the correct string type to use for IDL. The following points should help determine the appropriate string type.</p>
-
-<ul>
- <li>Using string classes may avoid new memory allocation for <code>out</code> parameters. For example, if the caller is using an <code>nsAutoString</code> to receive the value for an <code>out</code> parameter, (defined in C++ as simply <code>nsAString&amp;</code> then assignment of short (less than 64-characters) values to an <code>out</code> parameter will only copy the value into the <code>nsAutoString</code>'s buffer. Moreover, using the string classes allows for sharing of string buffers. In many cases, assigning from one string object to another avoids copying in favor of simply incrementing a reference count.</li>
- <li><code>in</code> strings using string classes often have their length pre-calculated. This can be a performance win.</li>
- <li>In cases where a raw-character buffer is required, <code>string</code> and <code>wstring</code> provide faster access than <code>PromiseFlatString</code>.</li>
- <li>UTF-8 strings defined with <code>AUTF8String</code> may need to be decoded when crossing XPConnect boundaries. This can be a performance hit. On the other hand, UTF-8 strings take up less space for strings that are commonly ASCII.</li>
- <li>UTF-16 strings defined with <code>wstring</code> or <code>AString</code> are fast when the unicode value is required. However, if the value is more often ASCII, then half of the storage space of the underlying string may be wasted.</li>
-</ul>
-
-<h2 id="String_Guidelines">String Guidelines</h2>
-
-<p>Follow these simple rules in your code to keep your fellow developers, reviewers, and users happy.</p>
-
-<ul>
- <li>Use the most abstract string class that you can. Usually this is:
-  <ul>
-   <li><code><a href="#The_Abstract_Classes">nsAString</a></code> for function parameters</li>
-   <li><code><a href="#The_Concrete_Classes_-_which_classes_to_use_when">nsString</a></code> for member variables</li>
-   <li><a href="#The_Concrete_Classes_-_which_classes_to_use_when"><code>nsAutoString</code> </a> for local (stack-based) variables</li>
-  </ul>
- </li>
- <li>Use the <code>""_ns</code> and <code>u""_ns</code> user-defined literals to represent literal strings (e.g. <code>"foo"_ns</code>) as nsAString-compatible objects.</li>
- <li>Use <a href="#String_Concatenation">string concatenation</a> (i.e. the "+" operator) when combining strings.</li>
- <li>Use <code><a href="#Raw_Character_Pointers">nsDependentString</a></code> when you have a raw character pointer that you need to convert to an nsAString-compatible string.</li>
- <li>Use <code><a href="#Substrings_.28string_fragments.29">Substring()</a></code> to extract fragments of existing strings.</li>
- <li>Use <a href="#Iterators">iterators</a> to parse and extract string fragments.</li>
-</ul>
-
-<h2 id="Appendix_A_-_What_class_to_use_when">Appendix A - What class to use when</h2>
-
-<p>This table provides a quick reference for what classes you should be using.</p>
-
-<table class="standard-table">
- <tbody>
-  <tr>
-   <th class="header">Context</th>
-   <th class="header">class</th>
-   <th class="header">Notes</th>
-  </tr>
-  <tr>
-   <td>Local Variables</td>
-   <td><code>nsAutoString<br>
-    nsAutoCString</code></td>
-   <td></td>
-  </tr>
-  <tr>
-   <td>Class Member Variables</td>
-   <td><code>nsString<br>
-    nsCString</code></td>
-   <td></td>
-  </tr>
-  <tr>
-   <td>Method Parameter types</td>
-   <td><code>nsAString<br>
-    nsACString</code></td>
-   <td>Use abstract classes for parameters. Use <code>const nsAString&amp;</code> for "in" parameters and <code>nsAString&amp;</code> for "out" parameters.</td>
-  </tr>
-  <tr>
-   <td>Retrieving "out" string/wstrings</td>
-   <td><code>nsString<br>
-    nsCString</code></td>
-   <td>Use <code>getter_Copies()</code>. Similar to <code>nsString / nsCString</code>.</td>
-  </tr>
-  <tr>
-   <td>Wrapping character buffers</td>
-   <td><code>nsDependentString<br>
-    nsDependentCString</code></td>
-   <td>Wrap <code>const char* / const <code>char16_t</code>*</code> buffers.</td>
-  </tr>
-  <tr>
-   <td>Literal strings</td>
-   <td><code>nsLiteralString<br>
-    nsLiteralCString</code></td>
-   <td>Similar to <code>nsDependent[C]String</code>, but pre-calculates length at build time.</td>
-  </tr>
- </tbody>
-</table>
-
-<h2 id="Appendix_B_-_nsAString_Reference">Appendix B - nsAString Reference</h2>
-
-<p>Read-only methods.</p>
-
-<ul>
- <li><code><strong>Length()</strong></code></li>
- <li><code><strong>IsEmpty()</strong></code></li>
- <li><code><strong>IsVoid()</strong></code> - XPConnect will convert void nsAStrings to JavaScript <code>null</code>.</li>
- <li><code><strong>BeginReading(<em>iterator</em>)</strong></code></li>
- <li><code><strong>EndReading(<em>iterator</em>)</strong></code></li>
- <li><code><strong>Equals(<em>string[, comparator]</em>)</strong></code></li>
- <li><code><strong>First()</strong></code></li>
- <li><code><strong>Last()</strong></code></li>
- <li><code><strong>CountChar()</strong></code></li>
- <li><code><strong>Left(<em>outstring, length</em>)</strong></code></li>
- <li><code><strong>Mid(<em>outstring, position, length</em>)</strong></code></li>
- <li><code><strong>Right(<em>outstring, length</em>)</strong></code></li>
- <li><code><strong>FindChar(<em>character</em>)</strong></code></li>
-</ul>
-
-<p>Methods that modify the string.</p>
-
-<ul>
- <li><code><strong>Assign(<em>string</em>)</strong></code></li>
- <li><code><strong>Append(<em>string</em>)</strong></code></li>
- <li><code><strong>Insert(<em>string</em>)</strong></code></li>
- <li><code><strong>Cut(<em>start, length</em>)</strong></code></li>
- <li><code><strong>Replace(<em>start, length, string</em>)</strong></code></li>
- <li><code><strong>Truncate(<em>length</em>)</strong></code></li>
- <li><code><strong>SetIsVoid(<em>true</em>)</strong></code> - Make it null. XPConnect will convert void nsAStrings to JavaScript <code>null</code>.</li>
- <li><code><strong>BeginWriting(<em>iterator</em>)</strong></code></li>
- <li><code><strong>EndWriting(<em>iterator</em>)</strong></code></li>
- <li><code><strong>SetCapacity()</strong></code> - Inform the string about buffer size need before a sequence of calls to  Append() or converting appends that convert between UTF-16 and Latin1 in either direction. (Don't use if you use appends that convert between UTF-16 and UTF-8 in either direction.) Calling this method does not give you permission to use <code>BeginWriting()</code> to write past the logical length of the string. Use <code>SetLength()</code> or <code>BulkWrite()</code> as appropriate.</li>
-</ul>
-
-<div class="originaldocinfo">
-<h2 id="Original_Document_Information">Original Document Information</h2>
-
-<ul>
- <li>Author: <a class="link-mailto" href="mailto:alecf@flett.org">Alec Flett</a></li>
- <li>Copyright Information: Portions of this content are © 1998–2007 by individual mozilla.org contributors; content available under a Creative Commons license | <a class="external" href="http://www.mozilla.org/foundation/licensing/website-content.html">Details</a>.</li>
- <li>Thanks to David Baron for <a class="external" href="http://dbaron.org/mozilla/coding-practices">actual docs</a>,</li>
- <li>Peter Annema for lots of direction</li>
- <li>Myk Melez for some more docs</li>
- <li>David Bradley for a diagram</li>
- <li>Revised by Darin Fisher for Mozilla 1.7</li>
- <li>Revised by Jungshik Shin to clarify character encoding issues</li>
-</ul>
-</div>

+ 0 - 970
files/en-us/mozilla/tech/xpcom/guide/internal_strings/raw.html

@@ -1,970 +0,0 @@
-<p><span class="seoSummary">Most of the Mozilla code uses a C++ class hierarchy to pass string data, rather than using raw pointers. This guide documents the string classes which are visible to code within the Mozilla codebase (code which is linked into libxul).</span></p>
-
-<h2 id="Introduction">Introduction</h2>
-
-<p>The string classes are a library of C++ classes which are used to manage buffers of wide (16-bit) and narrow (8-bit) character strings. The headers and implementation are in the <code><a href="https://dxr.mozilla.org/mozilla-central/source/xpcom/string" rel="custom">xpcom/string</a></code> directory. All strings are stored as a single contiguous buffer of characters.</p>
-
-<p>The 8-bit and 16-bit string classes have completely separate base classes, but share the same APIs. As a result, you cannot assign a 8-bit string to a 16-bit string without some kind of conversion helper class or routine. For the purpose of this document, we will refer to the 16-bit string classes in class documentation. Every 16-bit class has an equivalent 8-bit class:</p>
-
-<table style="width: 200px;">
- <caption>Naming convention for wide and narrow string classes</caption>
- <tbody>
-  <tr>
-   <td>Wide</td>
-   <td>Narrow</td>
-  </tr>
-  <tr>
-   <td>nsAString</td>
-   <td>nsACString</td>
-  </tr>
-  <tr>
-   <td>nsString</td>
-   <td>nsCString</td>
-  </tr>
-  <tr>
-   <td>nsAutoString</td>
-   <td>nsAutoCString</td>
-  </tr>
-  <tr>
-   <td colspan="2">etc...</td>
-  </tr>
- </tbody>
-</table>
-
-<p>The string classes distinguish, as part of the type hierarchy, between strings that must have a null-terminator at the end of their buffer (<code>ns[C]String</code>) and strings that are not required to have a null-terminator (<code>nsA[C]String</code>). <code>nsA[C]String</code> is the base of the string classes (since it imposes fewer requirements) and <code>ns[C]String</code> is a class derived from it. Functions taking strings as parameters should generally take one of these four types.</p>
-
-<p>In order to avoid unnecessary copying of string data (which can have significant performance cost), the string classes support different ownership models. All string classes support the following three ownership models dynamically:</p>
-
-<ul>
- <li>reference counted, copy-on-write, buffers (the default)</li>
- <li>adopted buffers (a buffer that the string class owns, but is not reference counted, because it came from somewhere else)</li>
- <li>dependent buffers, that is, an underlying buffer that the string class does not own, but that the caller that constructed the string guarantees will outlive the string instance</li>
-</ul>
-
-<p>In addition, there is a special string class, <code>ns[C]AutoString</code>, that <em>additionally</em> contains an internal 64-unit buffer (intended primarily for use on the stack), leading to a fourth ownership model:</p>
-
-<ul>
- <li>storage within an auto string's stack buffer</li>
-</ul>
-
-<p>Auto strings will prefer reference counting an existing reference-counted buffer over their stack buffer, but will otherwise use their stack buffer for anything that will fit in it.</p>
-
-<p>There are a number of additional string classes:</p>
-
-<ul>
- <li>Classes which exist primarily as constructors for the other types, particularly <code>nsDependent[C]String</code> and <code>nsDependent[C]Substring</code>. These types are really just convenient notation for constructing an <code>ns[C]S[ubs]tring</code> with a non-default ownership mode; they should not be thought of as different types.</li>
- <li><code>nsLiteral[C]String</code> which should rarely be constructed explicitly but usually through the <code>""_ns</code> and <code>u""_ns</code> user-defined string literals. <code>nsLiteral[C]String</code> is trivially constructible and destructible, and therefore does not emit construction/destruction code when stored in statics, as opposed to the other string classes.</li>
-</ul>
-
-<h2 id="The_Major_String_Classes">The Major String Classes</h2>
-
-<p>The list below describes the main base classes. Once you are familiar with them, see the appendix describing What Class to Use When.</p>
-
-<ul>
- <li><strong><code>nsAString</code>/<code>nsACString</code></strong>: the abstract base class for all strings. It provides an API for assignment, individual character access, basic manipulation of characters in the string, and string comparison. This class corresponds to the XPIDL <code>AString</code> parameter type. nsAString is not necessarily null-terminated.</li>
- <li><strong><code>nsString</code>/<code>nsCString</code></strong>: builds on <code>nsAString</code> by guaranteeing a null-terminated storage. This allows for a method (<code>.get()</code>) to access the underlying character buffer.</li>
-</ul>
-
-<p>The remainder of the string classes inherit from either <code>nsAString</code> or <code>nsString</code>. Thus, every string class is compatible with <code>nsAString</code>.</p>
-
-<p>Since every string derives from <code>nsAString</code> (or <code>nsACString</code>), they all share a simple API. Common read-only methods:</p>
-
-<ul>
- <li><strong><code>.Length()</code></strong> - the number of code units (bytes for 8-bit string classes and <code>char16_t</code>s for 16-bit string classes) in the string.</li>
- <li><strong><code>.IsEmpty()</code></strong> - the fastest way of determining if the string has any value. Use this instead of testing <code>string.Length</code> == 0</li>
- <li><strong><code>.Equals(string)</code></strong> - TRUE if the given string has the same value as the current string.</li>
-</ul>
-
-<p>Common methods that modify the string:</p>
-
-<ul>
- <li><strong><code>.Assign(string)</code></strong> - Assigns a new value to the string.</li>
- <li><strong><code>.Append(string)</code></strong> - Appends a value to the string.</li>
- <li><strong><code>.Insert(string, position)</code></strong> - Inserts the given string before the code unit at position.</li>
- <li><strong><code>.Truncate(length)</code></strong> - shortens the string to the given length.</li>
-</ul>
-
-<p>Complete documentation can be found in the <a href="#Appendix_B_-_nsAString_Reference">Appendix</a>.</p>
-
-<h3 id="Read-only_strings">Read-only strings</h3>
-
-<p>The <code>const</code> attribute on a string determines if the string is writable. If a string is defined as a <code>const nsAString</code> then the data in the string cannot be manipulated. If one tries to call a non-<code>const</code> method on a <code>const</code> string the compiler will flag this as an error at build time.</p>
-
-<p>For example:</p>
-
-<pre class="eval notranslate">void nsFoo::ReverseCharacters(nsAString&amp; str) {
-      ...
-     str.Assign(reversedStr); // modifies the string
-}
-</pre>
-
-<p>This should not compile, because you're assigning to a <code>const</code> class:</p>
-
-<pre class="eval notranslate">void nsFoo::ReverseCharacters(const nsAString&amp; str) {
-      ...
-     <strong>str.Assign(reversedStr);</strong>
-}
-</pre>
-
-<h3 id="As_function_parameters">As function parameters</h3>
-
-<p>For methods which are exposed across modules, use nsAString references to pass strings. For example:</p>
-
-<pre class="notranslate">// when passing a string to a method, use const nsAString&amp;
-nsFoo::PrintString(<strong>const nsAString &amp;str</strong>);
-
-// when getting a string from a method, use nsAString&amp;
-nsFoo::GetString(<strong>nsAString &amp;result</strong>);
-</pre>
-
-<p>The abstract classes are also sometimes used to store temporary references to objects. You can see both of these uses in <a href="#Common_Patterns">Common Patterns</a>, below.</p>
-
-<h2 id="The_Concrete_Classes_-_which_classes_to_use_when">The Concrete Classes - which classes to use when</h2>
-
-<p>The concrete classes are for use in code that actually needs to store string data. The most common uses of the concrete classes are as local variables, and members in classes or structs.</p>
-
-<p><img alt="nsAString_internal-graph.png" class="default internal" src="/@api/deki/files/2968/=nsAString_internal-graph.png" style="height: 443px; width: 732px;"></p>
-
-<p> The following is a list of the most common concrete classes. Once you are familiar with them, see the appendix describing <a href="#Appendix_A_-_What_class_to_use_when">What Class to Use When.</a></p>
-
-<p><img alt="" src="/@api/deki/pages/=en/nsAString_internal/files/=nsAString_internal-graph.png"></p>
-
-<ul>
- <li><code><strong>nsString / nsCString</strong></code>- a null-terminated string whose buffer is allocated on the heap. Destroys its buffer when the string object goes away.</li>
- <li><code><strong>nsAutoString / nsAutoCString</strong></code>- derived from <code>nsString</code>, a string which owns a 64 code unit buffer in the same storage space as the string itself. If a string less than 64 code units is assigned to an <code>nsAutoString</code>, then no extra storage will be allocated. For larger strings, a new buffer is allocated on the heap. If you want a number other than 64, use the templated types <code>nsAutoStringN</code> / <code>nsAutoCStringN</code>. (<code>nsAutoString</code> and <code>nsAutoCString</code> are just typedefs for <code>nsAutoStringN&lt;64&gt;</code> and <code>nsAutoCStringN&lt;64&gt;</code>, respectively.)</li>
- <li><code><strong>nsDependentString</strong></code>- derived from <code>nsString</code>, this string does <em>not</em> own its buffer. It is useful for converting a raw string (<code>const char16_t*</code> or <code>const char*</code>) into a class of type <code>nsAString</code>. Note that you must null-terminate buffers used by to nsDependentString. If you don't want to or can't null-terminate the buffer, use nsDependentSubstring.</li>
- <li><code><strong>nsPrintfCString</strong></code>- derived from <code>nsCString</code>, this string behaves like an <code>nsAutoCString</code>. The constructor takes parameters which allows it to construct a 8-bit string from a <code>printf</code>-style format string and parameter list.</li>
-</ul>
-
-<p>There are also a number of concrete classes that are created as a side-effect of helper routines, etc. You should avoid direct use of these classes. Let the string library create the class for you.</p>
-
-<ul>
- <li><code><strong>nsSubstringTuple</strong></code> - created via <a href="#String_Concatenation">string concatenation</a></li>
- <li><code><strong>nsDependentSubstring</strong></code> - created through <a href="#Substrings_.28string_fragments.29">Substring</a></li>
- <li><code><strong>nsPromiseFlatString</strong></code> - created through <code><strong><a href="#Raw_Character_Pointers">PromiseFlatString()</a></strong></code></li>
- <li><code><strong>nsLiteral[C]String</strong></code> - created through the <code>""_ns</code> and <code>u""_ns</code> user-defined literals</li>
-</ul>
-
-<p>Of course, there are times when it is necessary to reference these string classes in your code, but as a general rule they should be avoided.</p>
-
-<h2 id="Iterators">Iterators</h2>
-
-<p>Because Mozilla strings are always a single buffer, iteration over the characters in the string is done using raw pointers:</p>
-
-<pre class="notranslate">/**
- * Find whether there is a tab character in `data`
- */
-bool HasTab(const nsAString&amp; data)
-{
-  const <code>char16_t</code>* cur = data.BeginReading();
-  const <code>char16_t</code>* end = data.EndReading();
-
-  for (; cur &lt; end; ++cur) {
-    if (<code>char16_t</code>('\t') == *cur)
-      return true;
-  }
-  return false;
-}</pre>
-
-<p>Note that `end` points to the character after the end of the string buffer. It should never be dereferenced.</p>
-
-<p>Writing to a mutable string is also simple:</p>
-
-<pre class="notranslate">/**
- * Replace every tab character in `data` with a space.
- */
-void ReplaceTabs(nsAString&amp; data)
-{
-  <code>char16_t</code>* cur = data.BeginWriting();
-  <code>char16_t</code>* end = data.EndWriting();
-
-  for (; cur &lt; end; ++cur) {
-    if (<code>char16_t</code>('\t') == *cur)
-      *cur = <code>char16_t</code>(' ');
-  }
-}</pre>
-
-<p>You may change the length of a string via <code>SetLength()</code>.  Note that Iterators become invalid after changing the length of a string:</p>
-
-<pre class="eval notranslate">/**
- * Replace every tab character in `data` with four spaces.
- */
-void ReplaceTabs2(nsAString&amp; data)
-{
-  int len = data.Length();
-  <code>char16_t</code> *cur = data.BeginWriting();
-  <code>char16_t</code> *end = data.EndWriting();
-
-<span style="color: #ff0000;">  // Because `cur` may change during the loop, track the position
-  // within the string.
-  int pos = 0;</span>
-
-  while (cur &lt; end) {
-    if (<code>char16_t</code>('\t') != *cur) {
-      ++pos;
-      ++cur;
-    } else {
-      len += 3;
-      data.SetLength(len);
-
-      <span style="color: #ff0000;">// After SetLength, read `cur` and `end` again
-      cur = data.BeginWriting() + pos;
-      end = data.EndWriting();</span>
-
-      // move the remaining data over
-      if (pos &lt; len - 1)
-        memmove(cur + 4, cur + 1, (len - 1 - pos) * sizeof(<code>char16_t</code>));
-
-      // fill the tab with spaces
-      *cur = <code>char16_t</code>(' ');
-      *(cur + 1) = <code>char16_t</code>(' ');
-      *(cur + 2) = <code>char16_t</code>(' ');
-      *(cur + 3) = <code>char16_t</code>(' ');
-
-      pos += 4;
-      cur += 4;
-    }
-  }
-}
-</pre>
-
-<p>If a string buffer becomes smaller while writing it, use SetLength to inform the string class of the new size:</p>
-
-<pre class="notranslate">/**
- * Remove every tab character from `data`
- */
-void RemoveTabs(nsAString&amp; data)
-{
-  int len = data.Length();
-  <code>char16_t</code>* cur = data.BeginWriting();
-  <code>char16_t</code>* end = data.EndWriting();
-
-  while (cur &lt; end) {
-    if (<code>char16_t</code>('\t') == *cur) {
-      len -= 1;
-      end -= 1;
-      if (cur &lt; end)
-        memmove(cur, cur + 1, (end - cur) * sizeof(<code>char16_t</code>));
-    } else {
-      cur += 1;
-    }
-  }
-
-<span style="color: #ff0000;">  data.SetLength(len);</span>
-}
-</pre>
-
-<p>Note that using <code>BeginWriting()</code> to make a string <em>longer</em> is not OK. <code>BeginWriting()</code> must not be used to write past the logical length of the string indicated by <code>EndWriting()</code> or <code>Length()</code>. Calling <code>SetCapacity()</code> before <code>BeginWriting()</code> does not affect what the previous sentence says. To make the string longer, call <code>SetLength()</code> <em>before</em> <code>BeginWriting()</code> or use the <code>BulkWrite()</code> API described below.</p>
-
-<h2 id="Bulk_Write">Bulk Write</h2>
-
-<p><code>BulkWrite()</code> allows capacity-aware cache-friendly low-level writes to the string's buffer.</p>
-
-<p>Capacity-aware means that the caller is made aware of how the caller-requested buffer capacity was rounded up to mozjemalloc buckets. This is useful when initially requesting best-case buffer size without yet knowing the true size need. If the data that actually needs to be written is larger than the best-case estimate but still fits within the rounded-up capacity, there is no need to reallocate despite requesting the best-case capacity.</p>
-
-<p>Cache-friendly means that the zero terminator for C compatibility is written <em>after</em> the new content of the string has been written, so the result is a forward-only linear write access pattern instead of a non-linear back-and-forth sequence resulting from using <code>SetLength()</code> followed by <code>BeginWriting()</code>.</p>
-
-<p>Low-level means that writing via a raw pointer is possible as with <code>BeginWriting()</code>.</p>
-
-<p><code>BulkWrite()</code> takes four arguments: The new capacity (which may be rounded up), the number of code units at the beginning of the string to preserve (typically the old logical length), a boolean indicating whether reallocating a smaller buffer is OK if the requested capacity would fit in a buffer that's smaller than current one, and a reference to an <code>nsresult</code> for indicating failure on OOM. (Don't access the return value if the <code>nsresult</code> indicates failure. Unfortunately <code>mozilla::Result</code> is not versatile enough to be used here.)</p>
-
-<p><code>BulkWrite()</code> returns a <code>mozilla::BulkWriteHandle&lt;T&gt;</code>, where <code>T</code> is either <code>char</code> or <code>char16_t</code>. The actual writes are performed through this handle. You must not access the string except via the handle until you call <code>Finish()</code> on the handle in the success case or you let the handle go out of scope without calling <code>Finish()</code> in the failure case, in which case the destructor of the handle puts the string in a mostly harmless but consistent state (containing a single REPLACEMENT CHARACTER if a capacity greater than 0 was requested, or in the <code>char</code> case if the three-byte UTF-8 representation of the REPLACEMENT CHARACTER doesn't fit, an ASCII SUBSTITUTE).</p>
-
-<p><code>mozilla::BulkWriteHandle&lt;T&gt;</code> autoconverts to a writable <code>mozilla::Span&lt;T&gt;</code> and also provides explicit access to itself as <code>Span</code> (<code>AsSpan()</code>) or via component accessors named consistently with those on <code>Span</code>: <code>Elements()</code> and <code>Length()</code> the latter is not the logical length of the string but the writable length of the buffer. The buffer exposed via these methods includes the prefix that you may have requested to be preserved. It's up to you to skip past it so as to not overwrite it.</p>
-
-<p>If there's a need to request a different capacity before you are ready to call <code>Finish()</code>, you can call <code>RestartBulkWrite()</code> on the handle. It takes three arguments that match the first three arguments of <code>BulkWrite()</code>. It returns <code>mozilla::Result&lt;mozilla::Ok, nsresult&gt;</code> to indicate success or OOM. Calling <code>RestartBulkWrite()</code> invalidates previously-obtained span, raw pointer or length.</p>
-
-<p>Once you are done writing, call <code>Finish()</code>. It takes two arguments: the new logical length of the string (which must not exceed the capacity retuned by the <code>Length()</code> method of the handle) and a boolean indicating whether it's OK to attempt to reallocate a smaller buffer in case a smaller mozjemalloc bucket could accommodate the new logical length.</p>
-
-<h2 id="Helper_Classes_and_Functions">Helper Classes and Functions</h2>
-
-<h3 id="Converting_Cocoa_strings">Converting Cocoa strings</h3>
-
-<p>Use <code>mozilla::CopyCocoaStringToXPCOMString()</code> in <code>mozilla/MacStringHelpers.h</code> to convert Cocoa strings to XPCOM strings.</p>
-
-<h3 id="Searching_strings_-_looking_for_substrings_characters_etc.">Searching strings - looking for substrings, characters, etc.</h3>
-
-<p><code>FindInReadable()</code> is the replacement for the old <code>string.Find(..)</code>. The syntax is:</p>
-
-<pre class="eval notranslate">PRBool FindInReadable(const nsAString&amp; pattern,
-                      nsAString::const_iterator start, nsAString::const_iterator end,
-                      nsStringComparator&amp; aComparator = nsDefaultStringComparator());
-</pre>
-
-<p>To use this, <code>start</code> and <code>end</code> should point to the beginning and end of a string that you would like to search. If the search string is found, <code>start</code> and <code>end</code> will be adjusted to point to the beginning and end of the found pattern. The return value is PR_TRUE or PR_FALSE, indicating whether or not the string was found.</p>
-
-<p>An example:</p>
-
-<pre class="eval notranslate">const nsAString&amp; str = GetSomeString();
-nsAString::const_iterator start, end;
-
-str.BeginReading(start);
-str.EndReading(end);
-
-constexpr auto valuePrefix = u"value="_ns;
-
-if (FindInReadable(valuePrefix, start, end)) {
-    // end now points to the character after the pattern
-    valueStart = end;
-
-}
-</pre>
-
-<h3 id="Checking_for_Memory_Allocation_failure">Checking for Memory Allocation failure</h3>
-
-<p>The String classes now use infallible memory allocation, so you do not need to check for success when allocating/resizing "normal" strings. </p>
-
-<p>Most of the functions that modify Strings (Assign(), SetLength(), etc) also have a version that takes a "mozilla::fallible_t" parameter.  These versions return 'false' instead of aborting if allocation fails .  Use them when creating/allocating Strings which may be very large, and which the program could recover from if the allocation fails.</p>
-
-<h3 id="Getting_a_char_*_buffer_from_a_String">Getting a char * buffer from a String</h3>
-
-<p>You can access a String's internal buffer using the <a href="/en-US/docs/Mozilla/Tech/XPCOM/Guide/Internal_strings#Iterators">iterator methods</a>. The String retains ownership over the buffer at all times.</p>
-
-<p>In other words, there is no way to "grab" the internal char * from a String, i.e. have the string "forget" about it and hand off ownership to other code.  Sorry.</p>
-
-<p>If you wish to make a copy of a String into a new character buffer (<code><code>char16_t</code>*</code>/<code>char*</code>), the preferred way is to allocate it with one of the following methods:</p>
-
-<ul>
- <li><code><strong><code>char16_t</code>* ToNewUnicode(<em>nsAString&amp;</em>)</strong></code> - Allocates a <code>char16_t</code><code>*</code>buffer from an <code>nsAString</code>.</li>
- <li><code><strong>char *ToNewCString(<em>nsACString&amp;</em>)</strong></code> - Allocates a <code>char*</code>buffer from an <code>nsACString</code>. Note that this method will also work on nsAStrings, but it will do an implicit <a href="#Lossy_Conversion">lossy conversion</a>. This function should only be used if the input is known to be strictly ASCII. Often a conversion to UTF-8 is more appropriate. See <code><strong>ToNewUTF8String</strong></code> below.</li>
- <li><code><strong>char* ToNewUTF8String(<em>nsAString&amp;</em>)</strong></code> - Allocates a new <code>char*</code> buffer containing the UTF-8 encoded version of the given nsAString. See <a href="#Unicode_Conversion_ns.2ACString_vs._ns.2AString">Unicode Conversion</a> for more details and for better ways that don't require you to manage the memory yourself.</li>
-</ul>
-
-<p>These methods return a buffer allocated using XPCOM's allocator instead of the traditional allocator (<code>malloc</code>, etc.). Outside of libxul you should use <code>NS_Free</code> to deallocate the result when you no longer need it, inside libxul <code>free()</code> is preferred..</p>
-
-<h3 id="Substrings_string_fragments">Substrings (string fragments)</h3>
-
-<p>It is very simple to refer to a substring of an existing string without actually allocating new space and copying the characters into that substring. <code>Substring()</code> is the preferred method to create a reference to such a string.</p>
-
-<pre class="eval notranslate">void ProcessString(const nsAString&amp; str) {
-    const nsAString&amp; firstFive = Substring(str, 0, 5); // from index 0, length 5
-    // firstFive is now a string representing the first 5 characters
-}
-</pre>
-
-<h2 id="Unicode_Conversion_ns*CString_vs._ns*String">Unicode Conversion ns*CString vs. ns*String</h2>
-
-<p>Strings can be <em>stored</em> in two basic formats: 8-bit code unit (byte/<code>char</code>) strings, or 16-bit code unit (<code>char16_t</code>) strings. Any string class with a capital "C" in the classname contains 8-bit bytes. These classes include <code>nsCString</code>, <code>nsDependentCString</code>, and so forth. Any string class <em>without</em> the "C" contains 16-bit code units.</p>
-
-<p>A 8-bit string can be in one of many character encodings while a 16-bit string is always in potentially-invalid UTF-16. (You can make a 16-bit string guaranteed-valid UTF-16 by passing it to <code>EnsureUTF16Validity()</code>.) The most common encodings are:</p>
-
-<ul>
- <li>ASCII - 7-bit encoding for basic English-only strings. Each ASCII value is stored in exactly one byte in the array with the most-significant 8th bit set to zero.</li>
- <li><a class="external" href="http://www.unicode.org/glossary/#UCS_2">UCS2</a> - 16-bit encoding for a <em>subset</em> of Unicode, <a class="external" href="http://www.unicode.org/glossary/#BMP">BMP</a>. The Unicode value of a character stored in UCS2 is stored in exactly one 16-bit <code>char16_t</code> in a string class.</li>
- <li><a class="external" href="http://www.faqs.org/rfcs/rfc3629.html">UTF-8</a> - 8-bit encoding for Unicode characters. Each Unicode characters is stored in up to 4 bytes in a string class. UTF-8 is capable of representing the entire Unicode character repertoire, and it efficiently maps to <a class="external" href="http://www.unicode.org/glossary/#UTF_32">UTF-32</a>. (Gtk and Rust natively use UTF-8.)</li>
- <li><a class="external" href="http://www.unicode.org/glossary/#UTF_16">UTF-16</a> - 16-bit encoding for Unicode storage, backwards compatible with UCS2. The Unicode value of a character stored in UTF-16 may require <em>one or two</em> 16-bit <code>char16_t</code>s in a string class. The contents of <code>nsAString</code> always has to be regarded as in this encoding instead of UCS2. UTF-16 is capable of representing the entire Unicode character repertoire, and it efficiently maps to UTF-32. (Win32 W APIs and Mac OS X natively use UTF-16.)</li>
- <li>Latin1 - 8-bit encoding for the first 256 Unicode code points. Used for HTTP headers and for size-optimized storage in text node and SpiderMonkey strings. Latin1 converts to UTF-16 by zero-extending each byte to a 16-bit code unit. Note that this kind of "Latin1" is not available for encoding HTML, CSS, JS, etc. Specifying <code>charset=latin1</code> means the same as <code>charset=windows-1252</code>. Windows-1252 is a similar but different encoding used for interchange.</li>
-</ul>
-
-<p>In addition, there exist multiple other (legacy) encodings. The Web-relevant ones are defined in the <a href="https://encoding.spec.whatwg.org/">Encoding Standard</a>. Conversions from these encodings to UTF-8 and UTF-16 are provided by <a href="https://searchfox.org/mozilla-central/source/intl/Encoding.h#109">mozilla::Encoding</a>. Additonally, on Windows the are some rare cases (e.g. drag&amp;drop) where it's necessary to call a system API with data encoded in the Windows locale-dependent legacy encoding instead of UTF-16. In those rare cases, use <code>MultiByteToWideChar</code>/<code>WideCharToMultiByte</code> from kernel32.dll. Do not use <code>iconv</code> on *nix. We only support UTF-8-encoded file paths on *nix, non-path Gtk strings are always UTF-8 and Cocoa and Java strings are always UTF-16.</p>
-
-<p>When working with existing code, it is important to examine the current usage of the strings that you are manipulating, to determine the correct conversion mechanism.</p>
-
-<p>When writing new code, it can be confusing to know which storage class and encoding is the most appropriate. There is no single answer to this question, but the important points are:</p>
-
-<ul>
- <li><strong>Surprisingly many strings are very often just ASCII.</strong> ASCII is a subset of UTF-8 and is, therefore, efficient to represent as UTF-8. Representing ASCII as UTF-16 bad both for memory usage and cache locality.</li>
- <li><strong>Rust strongly prefers UTF-8.</strong> If your C++ code is interacting with Rust code, using UTF-8 in nsACString and merely validating it when converting to Rust strings is more efficient than using nsAString on the C++ side.</li>
- <li><strong>Networking code prefers 8-bit strings.</strong> Networking code tends to use 8-bit strings: either with UTF-8 or Latin1 (byte value is the Unicode scalar value) semantics.</li>
- <li><strong>JS and DOM prefer UTF-16.</strong> Most Gecko code uses UTF-16 for compatibility with JS strings and DOM string which are potentially-invalid UTF-16. However, both DOM text nodes and JS strings store strings that only contain code points below U+0100 as Latin1 (byte value is the Unicode scalar value).</li>
- <li><strong>Windows and Cocoa use UTF-16.</strong> Windows system APIs take UTF-16. Cocoa NSString is UTF-16.</li>
- <li><strong>Gtk uses UTF-8.</strong> Gtk APIs take UTF-8 for non-file paths. In the Gecko case, we support only UTF-8 file paths outside Windows, so all Gtk strings are UTF-8 for our purposes though file paths received from Gtk may not be valid UTF-8.</li>
-</ul>
-
-<p>To assist with ASCII, Latin1, UTF-8, and UTF-16 conversions, there are some helper methods and classes. Some of these classes look like functions, because they are most often used as temporary objects on the stack.</p>
-
-<h3 id="Short_zero-terminated_ASCII_strings">Short zero-terminated ASCII strings</h3>
-
-<p>If you have a short zero-terminated string that you are certain is always ASCII, use these special-case methods instead of the conversions described in the later sections.</p>
-
-<ul>
- <li>If you are assigning an ASCII literal to an <code>nsACString</code>, use <code>AssignLiteral()</code>.</li>
- <li>If you are assigning a literal to an <code>nsAString</code>, use <code>AssignLiteral()</code> and make the literal a <code>u""</code> literal. If the literal has to be a <code>""</code> literal (as opposed to <code>u""</code>) and is ASCII, still use <code>AppendLiteral()</code>, but be aware that this involves a run-time inflation.</li>
- <li>If you are assigning a zero-terminated ASCII string that's not a literal from the compiler's point of view at the call site and you don't know the length of the string either (e.g. because it was looked up from an array of literals of varying lengths), use <code>AssignASCII()</code>.</li>
-</ul>
-
-<h3 id="UTF-8_UTF-16_conversion">UTF-8 / UTF-16 conversion</h3>
-
-<p><code><strong>NS_ConvertUTF8toUTF16(<em>const nsACString&amp;</em>)</strong></code> - a <code>nsAutoString</code> subclass that converts a UTF-8 encoded <code>nsACString</code> or <code>const char*</code> to a 16-bit UTF-16 string. If you need a <code>const <code>char16_t</code>*</code> buffer, you can use the <code>.get()</code> method. For example:</p>
-
-<pre class="eval notranslate">/* signature: void HandleUnicodeString(const nsAString&amp; str); */
-object-&gt;HandleUnicodeString(<strong>NS_ConvertUTF8toUTF16</strong>(utf8String));
-
-/* signature: void HandleUnicodeBuffer(const <code>char16_t</code>* str); */
-object-&gt;HandleUnicodeBuffer(<strong>NS_ConvertUTF8toUTF16</strong>(utf8String).get());
-</pre>
-
-<p><code><strong>NS_ConvertUTF16toUTF8(<em>const nsAString&amp;</em>)</strong></code> - a <code>nsAutoCString</code> which converts a 16-bit UTF-16 string (<code>nsAString</code>) to a UTF-8 encoded string. As above, you can use <code>.get()</code> to access a <code>const char*</code> buffer.</p>
-
-<pre class="eval notranslate">/* signature: void HandleUTF8String(const nsACString&amp; str); */
-object-&gt;HandleUTF8String(<strong>NS_ConvertUTF16toUTF8</strong>(utf16String));
-
-/* signature: void HandleUTF8Buffer(const char* str); */
-object-&gt;HandleUTF8Buffer(<strong>NS_ConvertUTF16toUTF8</strong>(utf16String).get());
-</pre>
-
-<p><code><strong>CopyUTF8toUTF16(<em>const nsACString&amp;, nsAString&amp;</em>)</strong></code> - converts and copies:</p>
-
-<pre class="eval notranslate">// return a UTF-16 value
-void Foo::GetUnicodeValue(nsAString&amp; result) {
-    <strong>CopyUTF8toUTF16</strong>(mLocalUTF8Value, result);
- }
-</pre>
-
-<p><code><strong>AppendUTF8toUTF16(<em>const nsACString&amp;, nsAString&amp;</em>)</strong></code> - converts and appends:</p>
-
-<pre class="eval notranslate">// return a UTF-16 value
-void Foo::GetUnicodeValue(nsAString&amp; result) {
-    result.AssignLiteral("prefix:");
-    <strong>AppendUTF8toUTF16</strong>(mLocalUTF8Value, result);
-}
-</pre>
-
-<p><br>
- <code><strong>UTF8ToNewUnicode(<em>const nsACString&amp;, PRUint32* aUTF16Count = nsnull</em>)</strong></code> - (avoid if possible) allocates and converts (the optional parameter will contain the number of 16-byte units upon return, if non-null):</p>
-
-<pre class="eval notranslate">void Foo::GetUTF16Value(<code>char16_t</code>** result) {
-    *result = <strong>UTF8ToNewUnicode</strong>(mLocalUTF8Value);
-}
-</pre>
-
-<p><br>
- <code><strong>CopyUTF16toUTF8(<em>const nsAString&amp;, nsACString&amp;</em>)</strong></code> - converts and copies:</p>
-
-<pre class="eval notranslate">// return a UTF-8 value
-void Foo::GetUTF8Value(nsACString&amp; result) {
-    <strong>CopyUTF16toUTF8</strong>(mLocalUTF16Value, result);
-}
-</pre>
-
-<p><code><strong>AppendUTF16toUTF8(<em>const nsAString&amp;, nsACString&amp;</em>)</strong></code> - converts and appends:</p>
-
-<pre class="eval notranslate">// return a UTF-8 value
-void Foo::GetUnicodeValue(nsACString&amp; result) {
-    result.AssignLiteral("prefix:");
-    <strong>AppendUTF16toUTF8</strong>(mLocalUTF16Value, result);
-}
-</pre>
-
-<p><code><strong>ToNewUTF8String(<em>const nsAString&amp;</em>)</strong></code> - (avoid if possible) allocates and converts:</p>
-
-<pre class="eval notranslate">void Foo::GetUTF8Value(char** result) {
-    *result = <strong>ToNewUTF8String</strong>(mLocalUTF16Value);
-}
-</pre>
-
-<h3 id="Latin1_UTF-16_Conversion">Latin1 / UTF-16 Conversion</h3>
-
-<p>The following should only be used when you can guarantee that the original string is ASCII or Latin1 (in the sense that the byte value is the Unicode scalar value; not in the windows-1252 sense). These helpers are very similar to the UTF-8 / UTF-16 conversion helpers above.</p>
-
-<h4 id="UTF-16_to_Latin1_converters">UTF-16 to Latin1 converters</h4>
-
-<p>These converters are <em><strong>very dangerous</strong></em> because they <em><strong>lose information</strong></em> during the conversion process. You should <em><strong>avoid UTF-16 to Latin1 conversions</strong></em> unless your strings are guaranteed to be Latin1 or ASCII. (In the future, these conversions may start asserting in debug builds that their input is in the permissible range.) If the input is actually in the Latin1 range, each 16-bit code unit in narrowed to an 8-bit byte by removing the high half. Unicode code points above U+00FF result in garbage whose nature must not be relied upon. (In the future the nature of the garbage will be CPU architecture-dependent.) If you want to <code>printf()</code> something and don't care what happens to non-ASCII, please convert to UTF-8 instead.</p>
-
-<ul>
- <li><code><strong>NS_LossyConvertUTF16toASCII(<em>nsAString</em>)</strong></code> - a <code>nsAutoCString</code> which holds a temporary buffer containing the Latin1 value of the string.</li>
- <li><code><strong>LossyCopyUTF16toASCII(<em>nsAString, nsACString</em>)</strong></code> - does an in-place conversion from UTF-16 into an Latin1 string object.</li>
- <li><code><strong>LossyAppendUTF16toASCII(<em>nsAString, nsACString</em>)</strong></code> - appends an UTF-16 string to an Latin1 string.</li>
- <li><code><strong>ToNewCString(<em>nsAString</em>)</strong></code> - (avoid if ) allocates a new zero-terminated Latin1 <code>char*</code> string.</li>
-</ul>
-
-<h4 id="Latin1_to_UTF-16_converters">Latin1 to UTF-16 converters</h4>
-
-<p>These converters are <em><strong>very dangerous</strong></em> because they will <em><strong>produce wrong results for non-ASCII UTF-8 or windows-1252 input</strong></em> into a meaningless UTF-16 string. You should <em><strong>avoid ASCII to UTF-16 conversions</strong></em> unless your strings are guaranteed to be ASCII or Latin1 in the sense of the byte value being the Unicode scalar value. Every byte is zero-extended into a 16-bit code unit.</p>
-
-<p>It is correct to use these on most HTTP header values, but <em><strong>it's always wrong to use these on HTTP response bodies!</strong></em> (Use <code>mozilla::Encoding</code> to deal with response bodies.)</p>
-
-<ul>
- <li><code><strong>NS_ConvertASCIItoUTF16(<em>nsACString</em>)</strong></code> - a <code>nsAutoString</code> which holds a temporary buffer containing the value of the Latin1 to UTF-16 conversion.</li>
- <li><code><strong>CopyASCIItoUTF16(<em>nsACString, nsAString</em>)</strong></code> - does an in-place conversion from Latin1 to UTF-16.</li>
- <li><code><strong>AppendASCIItoUTF16(<em>nsACString, nsAString</em>)</strong></code> - appends a Latin1 string to a UTF-16 string.</li>
- <li><code><strong>ToNewUnicode(<em>nsACString</em>)</strong></code> - (Avoid if possible) Creates a new zero-terminated <code><code>char16_t</code>*</code> string which contains the value of the Latin1 to UTF-16 conversion.</li>
-</ul>
-
-<h3 id="Comparing_ns*Strings_with_C_strings">Comparing ns*Strings with C strings</h3>
-
-<p>You can compare ns*Strings with C strings by converting the ns*String to a C string, or by comparing directly against a C String.</p>
-
-<ul>
- <li><code><strong>PromiseFlatCString(<em>nsACString</em>).get()</strong></code> - creates a temporary <code>char *</code> out of a nsACString. This can be compared to a C String using C functions.</li>
- <li><code><strong><em>ns*String</em>.EqualsASCII(<em>const char *</em>)</strong></code> - compares with an ascii C string.</li>
- <li><code><strong><em>ns*String</em>.EqualsLiteral</strong></code> - compares with a string literal.</li>
-</ul>
-
-<h2 id="Common_Patterns">Common Patterns</h2>
-
-<h3 id="Callee-allocated_Parameters">Callee-allocated Parameters</h3>
-
-<p>Many APIs result in a method allocating a buffer in order to return strings to its caller. This can be tricky because the caller has to remember to free the string when they have finished using it. Fortunately, the <code>getter_Copies()</code> function makes this very easy.</p>
-
-<p>A method may look like this:</p>
-
-<pre class="eval notranslate">void GetValue(<code>char16_t</code>** aValue)
-{
-    *aValue = ToNewUnicode(foo);
-}
-</pre>
-
-<p>Without <code>getter_Copies()</code>, the caller would need to free the string:</p>
-
-<pre class="notranslate">{
-    <code>char16_t</code>* val;
-    GetValue(&amp;val);
-
-    if (someCondition) {
-        // don't forget to free the value!
-        NS_Free(val);
-        return NS_ERROR_FAILURE;
-    }
-
-    ...
-    // and later, still don't forget to free!
-    NS_Free(val);
-}
-</pre>
-
-<p>With <code>getter_copies()</code> you never have to worry about this. You can just use <code>getter_Copies()</code> to wrap a string class argument, and the class will remember to free the buffer when it goes out of scope:</p>
-
-<pre class="notranslate">{
-    nsString val;
-    GetValue(getter_Copies(val));
-
-    // val will free itself here
-    if (someCondition)
-        return NS_ERROR_FAILURE;
-    ...
-    // and later, still nothing to free
-}
-</pre>
-
-<p>The resulting code is much simpler, and easy to read.</p>
-
-<h3 id="Literal_Strings">Literal Strings</h3>
-
-<p>A <em>literal string</em> is a raw string value that is written in some C++ code. For example, in the statement <code>printf("Hello World\n");</code> the value <code>"Hello World\n"</code> is a literal string. It is often necessary to insert literal string values when an <code>nsAString</code> or <code>nsACString</code> is required. Two user-defined literals are provided that implicitly convert to <code>const nsString&amp;</code> resp. <code>const nsCString&amp;</code>:</p>
-
-<ul>
- <li><code>""_ns</code> for 8-bit literals, converting implicitly to <code>const nsCString&amp;</code></li>
- <li><code>u""_ns</code> for 16-bit literals, converting implicitly to <code>const nsString&amp;</code></li>
-</ul>
-
-<p>The benefits of the user-defined literals may seem unclear, given that <code>nsDependentCString</code> will also wrap a string value in an <code>nsCString</code>. The advantage of the user-defined literals is that the length of these strings is calculated at compile time, so the string does not need to be scanned at runtime to determine its length.</p>
-
-<p>Nowadays, all supported platforms have 16-bit literals using u""_ns, so we longer need to rely on macros for portability.</p>
-
-<pre class="notranslate">// call Init(const <code>char16_t</code>*) - bad signature, will need to do runtime length calculation inside
-Init(L"start value"); // bad - L"..." is not portable!
-Init(NS_ConvertASCIItoUTF16("start value").get()); // bad - runtime ASCII-&gt;UTF-16 conversion!
-Init(u"start value"); // less bad, portable and no runtime conversion
-
-// call Init(const nsAString&amp;)
-Init(nsDependentString(L"start value")); // bad - not portable!
-Init(NS_ConvertASCIItoUTF16("start value")); // bad - runtime ASCII-&gt;UTF-16 conversion!
-
-// call Init(const nsACString&amp;)
-Init(nsDependentCString("start value")); // bad - length determined at runtime
-</pre>
-
-<p>Here are some examples of proper usage of the literals (both standard and user-defined):</p>
-
-<pre class="notranslate">// call Init(const nsLiteralString&amp;) - enforces that it's only called with literals
-Init(u"start value"_ns);
-
-// call Init(const nsAString&amp;)
-Init(u"start value"_ns);
-
-// call Init(const nsACString&amp;)
-Init("start value"_ns);
-</pre>
-
-<p>In case a literal is defined via a macro, you can just convert it to <code>nsLiteralString</code> or <code>nsLiteralCString</code> using their constructor. You could consider not using a macro at all but a named <code>constexpr</code> constant instead.</p>
-
-<p>In some cases, an 8-bit literal is defined via a macro, either within code or from the environment, but it can't be changed or is used both as an 8-bit and a 16-bit string. In these cases, you can use the <code>NS_LITERAL_STRING_FROM_CSTRING </code>macro to construct a <code>nsLiteralString </code>and do the conversion at compile-time.</p>
-
-<h3 id="String_Concatenation">String Concatenation</h3>
-
-<p>Strings can be concatenated together using the + operator. The resulting string is a <code>const nsSubstringTuple</code> object. The resulting object can be treated and referenced similarly to a <code>nsAString</code> object. Concatenation <em>does not copy the substrings</em>. The strings are only copied when the concatenation is assigned into another string object. The <code>nsSubstringTuple</code> object holds pointers to the original strings. Therefore, the <code>nsSubstringTuple</code> object is dependent on all of its substrings, meaning that their lifetime must be at least as long as the <code>nsSubstringTuple</code> object.</p>
-
-<p>For example, you can use the value of two strings and pass their concatenation on to another function which takes an <code>const nsAString&amp;:</code></p>
-
-<pre class="eval notranslate">void HandleTwoStrings(const nsAString&amp; one, const nsAString&amp; two) {
-    // call HandleString(const nsAString&amp;)
-    HandleString(one + two);
-}
-</pre>
-
-<p>NOTE: The two strings are implicitly combined into a temporary <code>nsString</code> in this case, and the temporary string is passed into <code>HandleString</code>. If <code>HandleString</code> assigns its input into another <code>nsString</code>, then the string buffer will be shared in this case negating the cost of the intermediate temporary. You can concatenate N strings and store the result in a temporary variable:</p>
-
-<pre class="eval notranslate">constexpr auto start = u"start "_ns;
-constexpr auto middle = u"middle "_ns;
-constexpr auto end = u"end"_ns;
-// create a string with 3 dependent fragments - no copying involved!
-nsString combinedString = start + middle + end;
-
-// call void HandleString(const nsAString&amp;);
-HandleString(combinedString);
-</pre>
-
-<p>It is safe to concatenate user-defined literals because the temporary <code>nsLiteral[C]String </code>objects will live as long as the temporary concatenation object (of type <code>nsSubstringTuple</code>).</p>
-
-<pre class="eval notranslate">// call HandlePage(const nsAString&amp;);
-// safe because the concatenated-string will live as long as its substrings
-HandlePage(u"start "_ns + u"end"_ns);
-</pre>
-
-<h3 id="Local_variables">Local variables</h3>
-
-<p>Local variables within a function are usually stored on the stack. The <code>nsAutoString/nsAutoCString</code> classes are derivatives of the <code>nsString/nsCString classes</code>. They own a 64-character buffer allocated in the same storage space as the string itself. If the <code>nsAutoString</code> is allocated on the stack, then it has at its disposal a 64-character stack buffer. This allows the implementation to avoid allocating extra memory when dealing with small strings. <code>nsAutoStringN</code>/<code>nsAutoCStringN</code> are more general alternatives that let you choose the number of characters in the inline buffer.</p>
-
-<pre class="eval notranslate">...
-nsAutoString value;
-GetValue(value); // if the result is less than 64 code units,
-                 // then this just saved us an allocation
-...
-</pre>
-
-<h3 id="Member_variables">Member variables</h3>
-
-<p>In general, you should use the concrete classes <code>nsString</code> and <code>nsCString</code> for member variables.</p>
-
-<pre class="eval notranslate">class Foo {
-    ...
-    // these store UTF-8 and UTF-16 values respectively
-    nsCString mLocalName;
-    nsString mTitle;
-};
-</pre>
-
-<p>Note that the strings are declared directly in the class, not as pointers to strings. Don't do this:</p>
-
-<pre class="notranslate">class Foo {
-public:
-    Foo() {
-        mLocalName = new nsCString();
-        mTitle = new nsString();
-    }
-    ~Foo() { delete mLocalName; delete mTitle; }
-
-private:
-    // these store UTF-8 and UTF-16 values respectively
-    nsCString* mLocalName;
-    nsString*  mTitle;
-};
-</pre>
-
-<p>The above code may appear to save the cost of the string objects, but <code>nsString/nsCString</code> are small objects - the overhead of the allocation outweighs the few bytes you'd save by keeping a pointer.</p>
-
-<p>Another common incorrect pattern is to use <code>nsAutoString/nsAutoCString</code> for member variables. As described in <a href="#Local_variables">Local Variables</a>, these classes have a built in buffer that make them very large. This means that if you include them in a class, they bloat the class by 64 bytes (<code>nsAutoCString</code>) or 128 bytes (<code>nsAutoString</code>).</p>
-
-<p>An example:</p>
-
-<pre class="notranslate">class Foo {
-    ...
-
-    // bloats 'Foo' by 128 bytes!
-    nsAutoString mLocalName;
-};
-</pre>
-
-<h3 id="Raw_Character_Pointers">Raw Character Pointers</h3>
-
-<p><code>PromiseFlatString()</code> and <code>PromiseFlatCString() </code>can be used to create a temporary buffer which holds a null-terminated buffer containing the same value as the source string. <code>PromiseFlatString()</code> will create a temporary buffer if necessary. This is most often used in order to pass an <code>nsAString</code> to an API which requires a null-terminated string.</p>
-
-<p>In the following example, an <code>nsAString</code> is combined with a literal string, and the result is passed to an API which requires a simple character buffer.</p>
-
-<pre class="eval notranslate">// Modify the URL and pass to AddPage(const <code>char16_t</code>* url)
-void AddModifiedPage(const nsAString&amp; url) {
-    constexpr auto httpPrefix = u<span class="nowiki">"http://"</span>_ns;
-    const nsAString&amp; modifiedURL = httpPrefix + url;
-
-    // creates a temporary buffer
-    AddPage(PromiseFlatString(modifiedURL).get());
-}
-</pre>
-
-<p><code>PromiseFlatString()</code> is smart when handed a string that is already null-terminated. It avoids creating the temporary buffer in such cases.</p>
-
-<pre class="eval notranslate">// Modify the URL and pass to AddPage(const <code>char16_t</code>* url)
-void AddModifiedPage(const nsAString&amp; url, PRBool addPrefix) {
-    if (addPrefix) {
-        // MUST create a temporary buffer - string is multi-fragmented
-        constexpr auto httpPrefix = u<span class="nowiki">"http://"</span>_ns;
-        AddPage(PromiseFlatString(httpPrefix + modifiedURL));
-    } else {
-        // MIGHT create a temporary buffer, does a runtime check
-        AddPage(PromiseFlatString(url).get());
-    }
-}
-</pre>
-
-<h3 id="printf_and_a_UTF-16_string"><code>printf</code> and a UTF-16 string</h3>
-
-<p>For debugging, it's useful to <code>printf</code> a UTF-16 string (<code>nsString</code>, <code>nsAutoString</code>, etc). To do this usually requires converting it to an 8-bit string, because that's what printf expects. Use:</p>
-
-<pre class="eval notranslate">printf("%s\n", NS_ConvertUTF16toUTF8(yourString).get());
-</pre>
-
-<h3 id="Sequence_of_appends_without_reallocating">Sequence of appends without reallocating</h3>
-
-<p><code>SetCapacity()</code> allows you to give the string a hint of the future string length caused by a sequence of appends (excluding appends that convert between UTF-16 and UTF-8 in either direction) in order to avoid multiple allocations during the sequence of appends. However, the <em>other</em> allocation-avoidance features of XPCOM strings interact badly with <code>SetCapacity()</code> making it something of a footgun.</p>
-
-<p><code>SetCapacity()</code> is appropriate to use before a sequence of multiple operations from the following list (without operations that are not on the list between the <code>SetCapacity()</code> call and operations from the list):</p>
-
-<ul>
- <li><code>Append()</code></li>
- <li><code>AppendASCII()</code></li>
- <li><code>AppendLiteral()</code></li>
- <li><code>AppendPrintf()</code></li>
- <li><code>AppendInt()</code></li>
- <li><code>AppendFloat()</code></li>
- <li><code>LossyAppendUTF16toASCII()</code></li>
- <li><code>AppendASCIItoUTF16()</code></li>
-</ul>
-
-<p><em><strong>DO NOT</strong></em> call <code>SetCapacity()</code> if the subsequent operations on the string do not meet the criteria above. Operations that undo the benefits of <code>SetCapacity()</code> include but are not limited to:</p>
-
-<ul>
- <li><code>SetLength()</code></li>
- <li><code>Truncate()</code></li>
- <li><code>Assign()</code></li>
- <li><code>AssignLiteral()</code></li>
- <li><code>Adopt()</code></li>
- <li><code>CopyASCIItoUTF16()</code></li>
- <li><code>LossyCopyUTF16toASCII()</code></li>
- <li><code>AppendUTF16toUTF8()</code></li>
- <li><code>AppendUTF8toUTF16()</code></li>
- <li><code>CopyUTF16toUTF8()</code></li>
- <li><code>CopyUTF8toUTF16()</code></li>
-</ul>
-
-<p>If your string is an <code>nsAuto[C]String</code> and you are calling <code>SetCapacity()</code> with a constant N, please instead declare the string as <code>nsAuto[C]StringN&lt;</code>N+1<code>&gt;</code> without calling <code>SetCapacity()</code> (while being mindful of not using such a large N as to overflow the run-time stack).</p>
-
-<p>There is no need to include room for the null terminator: it is the job of the string class.</p>
-
-<p>Note: Calling <code>SetCapacity()</code> does not give you permission to use the pointer obtained from <code>BeginWriting()</code> to write past the current length (as returned by <code>Length()</code>) of the string. Please use either <code>BulkWrite()</code> or <code>SetLength()</code> instead.</p>
-
-<h2 id="IDL">IDL</h2>
-
-<p>The string library is also available through IDL. By declaring attributes and methods using the specially defined IDL types, string classes are used as parameters to the corresponding methods.</p>
-
-<h3 id="IDL_String_types">IDL String types</h3>
-
-<p>The C++ signatures follow the abstract-type convention described above, such that all method parameters are based on the <a href="#The_Abstract_Classes">abstract classes</a>. The following table describes the purpose of each string type in IDL.</p>
-
-<table class="standard-table">
- <tbody>
-  <tr>
-   <th class="header">IDL type</th>
-   <th class="header">C++ Type</th>
-   <th class="header">Purpose</th>
-  </tr>
-  <tr>
-   <td><code>string</code></td>
-   <td><code>char*</code></td>
-   <td>Raw character pointer to ASCII (7-bit) string, no string classes used. High bit is not guaranteed across XPConnect boundaries.</td>
-  </tr>
-  <tr>
-   <td><code>wstring</code></td>
-   <td><code><code>char16_t</code>*</code></td>
-   <td>Raw character pointer to UTF-16 string, no string classes used.</td>
-  </tr>
-  <tr>
-   <td><code>AString</code></td>
-   <td><code>nsAString</code></td>
-   <td>UTF-16 string.</td>
-  </tr>
-  <tr>
-   <td><code>ACString</code></td>
-   <td><code>nsACString</code></td>
-   <td>8-bit string. All bits are preserved across XPConnect boundaries.</td>
-  </tr>
-  <tr>
-   <td><code>AUTF8String</code></td>
-   <td><code>nsACString</code></td>
-   <td>UTF-8 string. Converted to UTF-16 as necessary when value is used across XPConnect boundaries.</td>
-  </tr>
-  <tr>
-   <td><code>DOMString</code></td>
-   <td><code>nsAString</code></td>
-   <td>UTF-16 string type used in the DOM. The same as <code>AString</code> with a few odd XPConnect exceptions: When the special JavaScript value <code>null</code> is passed to a <code>DOMString</code> parameter of an XPCOM method, it becomes a void <code>DOMString</code>. The special JavaScript value <code>undefined</code> becomes the string <code>"undefined"</code>.</td>
-  </tr>
- </tbody>
-</table>
-
-<h3 id="C_Signatures">C++ Signatures</h3>
-
-<p>In IDL, <code>in</code> parameters are read-only, and the C++ signatures for <code>*String</code> parameters follows the above guidelines by using <code>const nsAString&amp;</code> for these parameters. <code>out</code> and <code>inout</code> parameters are defined simply as <code>nsAString</code> so that the callee can write to them.</p>
-
-<table class="standard-table">
- <tbody>
-  <tr>
-   <th class="header">IDL</th>
-   <th class="header">C++</th>
-  </tr>
-  <tr>
-   <td>
-    <pre class="eval notranslate">
-interface nsIFoo : nsISupports {
-
-    attribute AString utf16String;
-
-
-
-
-    AUTF8String getValue(in ACString key);
-
-};
-</pre>
-   </td>
-   <td>
-    <pre class="eval notranslate">
-class nsIFoo : public nsISupports {
-
-     NS_IMETHOD GetUtf16String(nsAString&amp;
-                               aResult) = 0;
-     NS_IMETHOD SetUtf16String(const nsAString&amp;
-                              aValue) = 0;
-
-     NS_IMETHOD GetValue(const nsACString&amp; aKey,
-                     nsACString&amp; aResult) = 0;
-};
-</pre>
-   </td>
-  </tr>
- </tbody>
-</table>
-
-<p>In the above example, <code>utf16String</code> is treated as a UTF-16 string. The implementation of <code>GetUtf16String()</code> will use <code>aResult.Assign</code> to "return" the value. In <code>SetUtf16String()</code> the value of the string can be used through a variety of methods including <a href="#Iterators">Iterators</a>, <code><a href="#Raw_Character_Pointers">PromiseFlatString</a></code>, and assignment to other strings.</p>
-
-<p>In <code>GetValue()</code>, the first parameter, <code>aKey</code>, is treated as a raw sequence of 8-bit values. Any non-ASCII characters in <code>aKey</code> will be preserved when crossing XPConnect boundaries. The implementation of <code>GetValue()</code> will assign a UTF-8 encoded 8-bit string into <code>aResult</code>. If the <code>this</code> method is called across XPConnect boundaries, such as from a script, then the result will be decoded from UTF-8 into UTF-16 and used as a Unicode value.</p>
-
-<h3 id="Choosing_a_string_type">Choosing a string type</h3>
-
-<p>It can be difficult to determine the correct string type to use for IDL. The following points should help determine the appropriate string type.</p>
-
-<ul>
- <li>Using string classes may avoid new memory allocation for <code>out</code> parameters. For example, if the caller is using an <code>nsAutoString</code> to receive the value for an <code>out</code> parameter, (defined in C++ as simply <code>nsAString&amp;</code> then assignment of short (less than 64-characters) values to an <code>out</code> parameter will only copy the value into the <code>nsAutoString</code>'s buffer. Moreover, using the string classes allows for sharing of string buffers. In many cases, assigning from one string object to another avoids copying in favor of simply incrementing a reference count.</li>
- <li><code>in</code> strings using string classes often have their length pre-calculated. This can be a performance win.</li>
- <li>In cases where a raw-character buffer is required, <code>string</code> and <code>wstring</code> provide faster access than <code>PromiseFlatString</code>.</li>
- <li>UTF-8 strings defined with <code>AUTF8String</code> may need to be decoded when crossing XPConnect boundaries. This can be a performance hit. On the other hand, UTF-8 strings take up less space for strings that are commonly ASCII.</li>
- <li>UTF-16 strings defined with <code>wstring</code> or <code>AString</code> are fast when the unicode value is required. However, if the value is more often ASCII, then half of the storage space of the underlying string may be wasted.</li>
-</ul>
-
-<h2 id="String_Guidelines">String Guidelines</h2>
-
-<p>Follow these simple rules in your code to keep your fellow developers, reviewers, and users happy.</p>
-
-<ul>
- <li>Use the most abstract string class that you can. Usually this is:
-  <ul>
-   <li><code><a href="#The_Abstract_Classes">nsAString</a></code> for function parameters</li>
-   <li><code><a href="#The_Concrete_Classes_-_which_classes_to_use_when">nsString</a></code> for member variables</li>
-   <li><a href="#The_Concrete_Classes_-_which_classes_to_use_when"><code>nsAutoString</code> </a> for local (stack-based) variables</li>
-  </ul>
- </li>
- <li>Use the <code>""_ns</code> and <code>u""_ns</code> user-defined literals to represent literal strings (e.g. <code>"foo"_ns</code>) as nsAString-compatible objects.</li>
- <li>Use <a href="#String_Concatenation">string concatenation</a> (i.e. the "+" operator) when combining strings.</li>
- <li>Use <code><a href="#Raw_Character_Pointers">nsDependentString</a></code> when you have a raw character pointer that you need to convert to an nsAString-compatible string.</li>
- <li>Use <code><a href="#Substrings_.28string_fragments.29">Substring()</a></code> to extract fragments of existing strings.</li>
- <li>Use <a href="#Iterators">iterators</a> to parse and extract string fragments.</li>
-</ul>
-
-<h2 id="Appendix_A_-_What_class_to_use_when">Appendix A - What class to use when</h2>
-
-<p>This table provides a quick reference for what classes you should be using.</p>
-
-<table class="standard-table">
- <tbody>
-  <tr>
-   <th class="header">Context</th>
-   <th class="header">class</th>
-   <th class="header">Notes</th>
-  </tr>
-  <tr>
-   <td>Local Variables</td>
-   <td><code>nsAutoString<br>
-    nsAutoCString</code></td>
-   <td></td>
-  </tr>
-  <tr>
-   <td>Class Member Variables</td>
-   <td><code>nsString<br>
-    nsCString</code></td>
-   <td></td>
-  </tr>
-  <tr>
-   <td>Method Parameter types</td>
-   <td><code>nsAString<br>
-    nsACString</code></td>
-   <td>Use abstract classes for parameters. Use <code>const nsAString&amp;</code> for "in" parameters and <code>nsAString&amp;</code> for "out" parameters.</td>
-  </tr>
-  <tr>
-   <td>Retrieving "out" string/wstrings</td>
-   <td><code>nsString<br>
-    nsCString</code></td>
-   <td>Use <code>getter_Copies()</code>. Similar to <code>nsString / nsCString</code>.</td>
-  </tr>
-  <tr>
-   <td>Wrapping character buffers</td>
-   <td><code>nsDependentString<br>
-    nsDependentCString</code></td>
-   <td>Wrap <code>const char* / const <code>char16_t</code>*</code> buffers.</td>
-  </tr>
-  <tr>
-   <td>Literal strings</td>
-   <td><code>nsLiteralString<br>
-    nsLiteralCString</code></td>
-   <td>Similar to <code>nsDependent[C]String</code>, but pre-calculates length at build time.</td>
-  </tr>
- </tbody>
-</table>
-
-<h2 id="Appendix_B_-_nsAString_Reference">Appendix B - nsAString Reference</h2>
-
-<p>Read-only methods.</p>
-
-<ul>
- <li><code><strong>Length()</strong></code></li>
- <li><code><strong>IsEmpty()</strong></code></li>
- <li><code><strong>IsVoid()</strong></code> - XPConnect will convert void nsAStrings to JavaScript <code>null</code>.</li>
- <li><code><strong>BeginReading(<em>iterator</em>)</strong></code></li>
- <li><code><strong>EndReading(<em>iterator</em>)</strong></code></li>
- <li><code><strong>Equals(<em>string[, comparator]</em>)</strong></code></li>
- <li><code><strong>First()</strong></code></li>
- <li><code><strong>Last()</strong></code></li>
- <li><code><strong>CountChar()</strong></code></li>
- <li><code><strong>Left(<em>outstring, length</em>)</strong></code></li>
- <li><code><strong>Mid(<em>outstring, position, length</em>)</strong></code></li>
- <li><code><strong>Right(<em>outstring, length</em>)</strong></code></li>
- <li><code><strong>FindChar(<em>character</em>)</strong></code></li>
-</ul>
-
-<p>Methods that modify the string.</p>
-
-<ul>
- <li><code><strong>Assign(<em>string</em>)</strong></code></li>
- <li><code><strong>Append(<em>string</em>)</strong></code></li>
- <li><code><strong>Insert(<em>string</em>)</strong></code></li>
- <li><code><strong>Cut(<em>start, length</em>)</strong></code></li>
- <li><code><strong>Replace(<em>start, length, string</em>)</strong></code></li>
- <li><code><strong>Truncate(<em>length</em>)</strong></code></li>
- <li><code><strong>SetIsVoid(<em>true</em>)</strong></code> - Make it null. XPConnect will convert void nsAStrings to JavaScript <code>null</code>.</li>
- <li><code><strong>BeginWriting(<em>iterator</em>)</strong></code></li>
- <li><code><strong>EndWriting(<em>iterator</em>)</strong></code></li>
- <li><code><strong>SetCapacity()</strong></code> - Inform the string about buffer size need before a sequence of calls to  Append() or converting appends that convert between UTF-16 and Latin1 in either direction. (Don't use if you use appends that convert between UTF-16 and UTF-8 in either direction.) Calling this method does not give you permission to use <code>BeginWriting()</code> to write past the logical length of the string. Use <code>SetLength()</code> or <code>BulkWrite()</code> as appropriate.</li>
-</ul>
-
-<div class="originaldocinfo">
-<h2 id="Original_Document_Information">Original Document Information</h2>
-
-<ul>
- <li>Author: <a class="link-mailto" href="mailto:alecf@flett.org">Alec Flett</a></li>
- <li>Copyright Information: Portions of this content are © 1998–2007 by individual mozilla.org contributors; content available under a Creative Commons license | <a class="external" href="http://www.mozilla.org/foundation/licensing/website-content.html">Details</a>.</li>
- <li>Thanks to David Baron for <a class="external" href="http://dbaron.org/mozilla/coding-practices">actual docs</a>,</li>
- <li>Peter Annema for lots of direction</li>
- <li>Myk Melez for some more docs</li>
- <li>David Bradley for a diagram</li>
- <li>Revised by Darin Fisher for Mozilla 1.7</li>
- <li>Revised by Jungshik Shin to clarify character encoding issues</li>
-</ul>
-</div>

+ 1 - 0
files/ja/_redirects.txt

@@ -182,6 +182,7 @@
 /ja/docs/Mobile/Viewport_meta_tag	/ja/docs/Mozilla/Mobile/Viewport_meta_tag
 /ja/docs/Mochitest	/ja/docs/Mozilla/Projects/Mochitest
 /ja/docs/Modularization_Techniques	/ja/docs/Archive/Mozilla/Modularization_Techniques
+/ja/docs/Mozilla/Tech/XPCOM/Guide/Mozilla_internal_string_guide	https://firefox-source-docs.mozilla.org/xpcom/stringguide.html
 /ja/docs/Mozilla's_DOCTYPE_sniffing	/ja/docs/Archive/Web_Standards/Mozilla_s_DOCTYPE_sniffing
 /ja/docs/Mozilla_internal_string_guide	/ja/docs/Mozilla/Tech/XPCOM/Guide/Mozilla_internal_string_guide
 /ja/docs/Multiple_Item_Packaging	/ja/docs/Archive/Add-ons/Multiple_Item_Packaging

+ 0 - 13
files/ja/_wikihistory.json

@@ -16825,19 +16825,6 @@
       "electrolysis"
     ]
   },
-  "Mozilla/Tech/XPCOM/Guide/Mozilla_internal_string_guide": {
-    "modified": "2019-10-14T23:39:18.444Z",
-    "contributors": [
-      "mfuji09",
-      "wbamberg",
-      "SphinxKnight",
-      "ethertank",
-      "Potappo",
-      "Debuyo",
-      "Taken",
-      "Mgjbot"
-    ]
-  },
   "Mozilla/Tech/XPCOM/Guide/XPCOM_hashtable_guide": {
     "modified": "2019-10-14T23:39:17.804Z",
     "contributors": [

+ 0 - 834
files/ja/mozilla/tech/xpcom/guide/mozilla_internal_string_guide/index.html

@@ -1,834 +0,0 @@
----
-title: XPCOM string guide
-slug: Mozilla/Tech/XPCOM/Guide/Mozilla_internal_string_guide
-tags:
-  - 移行
-  - 要更新
-translation_of: Mozilla/Tech/XPCOM/Guide/Internal_strings
----
-<h2 id=".E5.BA.8F.E6.96.87" name=".E5.BA.8F.E6.96.87">序文</h2>
-
-<div>
-<p>by Alec Flett<br>
- Thanks to David Baron for <a class="external" href="http://dbaron.org/mozilla/coding-practices">actual docs</a>,<br>
- Peter Annema for lots of direction,<br>
- Myk Melez for some more docs, and<br>
- David Bradley for a diagram<br>
- Revised by Darin Fisher for Mozilla 1.7<br>
- Revised by Jungshik Shin to clarify character encoding issues</p>
-</div>
-
-<p> このガイドは過剰に存在しているstringクラスについてドキュメント化したものです。これによって「こういう場合、いったいどのstringクラスを使ったらいいの?」という長年の疑問に対する答えとなることを期待しています。</p>
-
-<div style="border: thin solid steelblue; padding: 0.5em;">
-<p> もしあなたがMozillaの組み込み開発者か、もしくはMozillaコードベースとは別個に配布されることを予定しているXPCOMコンポーネントを書いているなら、この文書は多くの場合あなたにとって最適のものとは言えません!もしあなたがMozilla 1.7以降を対象に開発を行っているなら、この文書の代わりに新しい最小版APIである <code><a href="https://dxr.mozilla.org/mozilla-central/source/xpcom/glue/nsStringAPI.h" rel="custom">xpcom/glue/nsStringAPI.h</a></code> とりわけ<code><a href="https://dxr.mozilla.org/mozilla-central/source/xpcom/string/public/nsEmbedString.h" rel="custom">xpcom/string/public/nsEmbedString.h</a></code> クラスを使うべきです。</p>
-</div>
-
-<p> お急ぎですか?それなら <a href="/ja/XPCOM/String_Quick_Reference" title="ja/XPCOM/String_Quick_Reference">String Quick-Reference</a> () を見てください。</p>
-
-<h2 id=".E3.81.AF.E3.81.98.E3.82.81.E3.81.AB" name=".E3.81.AF.E3.81.98.E3.82.81.E3.81.AB">はじめに</h2>
-
-<p>string クラスは、unicode と 1バイト文字の文字列のバッファを扱うために使われる C++ クラスのライブラリです。 これらは <code><a href="https://dxr.mozilla.org/mozilla-central/source/xpcom/string" rel="custom">xpcom/string</a></code> ディレクトリの Mozilla コードベースに属します。</p>
-
-<p>Abstract (interface) クラスは"nsA"で始まり、 具象クラスは"<code>ns</code>"で始まります。 "<code><strong>C</strong>String</code>" を名前に持つクラスは シングルバイト ASCII 文字列もしくは、UTF-8 やその他の文字セットでエンコードされたマルチバイト unicode 文字列を参照する 8 ビット文字を格納します。 名前に単に "<code>String</code>" を持つ他のすべてのクラスは、 主に UTF16 でエンコードされる 2バイト(<code>PRUnichar</code>)文字列を参照します。 例:<code>nsAString</code> は 2バイト文字を蓄えるための抽象クラスで、 <code>nsDependentCString</code> は 1バイト文字を蓄えるための具象クラスです。 どの 2バイト文字列クラスも同等の 1バイト文字列クラスを持ちます。 例:<code>nsCString</code> クラスは 1バイト文字列クラスで、 <code>nsString</code> と対応します。</p>
-
-<p>1バイトと 2バイトの文字列クラスは完全に異なる基底クラスを持ちますが、 同じ API を共有します。 このように、1バイト文字列を 2バイト文字列へ、何らかのヘルパークラスかルーチンによる変換なしに代入することは出来ません。 このドキュメントの目的は、クラスドキュメントの中で 2バイト文字列クラスについて言及することです。 どの 2バイトクラスも同等の 1バイトクラスを持つと仮定しても安全です。</p>
-
-<h2 id="String_ガイドライン">String ガイドライン</h2>
-
-<p>仲間の開発者、レビューワ、ユーザをハッピーにさせるために、コードの中でこれらの単純なルールに従ってください。</p>
-
-<ul>
- <li>是非とも<a href="#Unicode_Conversion"><code><span class="nowiki">*WithConversion</span></code> 関数を避ける</a>: <code>AssignWithConversion</code>, <code>AppendWithConversion</code>, <code>EqualsWithConversion</code>, など</li>
- <li>出来る限りもっとも抽象的なクラスを使う。大抵はこれ:
-  <ul>
-   <li><a href="#Abstract_Classes"><code>nsAString</code></a> 関数の引数のために</li>
-   <li><a href="#Concrete_Classes"><code>nsString</code> メンバ変数のために </a></li>
-   <li><a href="#Concrete_Classes"><code>nsAutoString</code> または <code>nsXPIDLString</code></a> ローカル(スタックベース)変数のために</li>
-  </ul>
- </li>
- <li><code>nsAString</code> 互換オブジェクトとしてリテラル文字列(例:<code>"foo"</code>)を表現するためには <a href="#Literal_Strings"><code>NS_LITERAL_[C</code></a>STRING/<code>NS_NAMED_LITERAL_[C]STRING</code>] を使う。</li>
- <li>文字列を結合するときには<a href="#Concatenation">string 連結</a>(例: "<code>+</code>" オペレータ)を使う。</li>
- <li><code>nsAString</code>互換文字列を変換する必要のある生の文字ポインタを持つとき、 <a href="#Raw_Pointers"><code>nsDependentString</code></a>を使う。</li>
- <li>既存の文字列から一部を抜き出すためには <a href="#Substring"><code>Substring()</code></a> を使う。</li>
- <li>文字列断片の解析と抜き出しには <a href="#Iterators">iterators</a> を使う。</li>
-</ul>
-
-<p> </p>
-
-<h2 id="Abstract.EF.BC.88.E6.8A.BD.E8.B1.A1.EF.BC.89.E3.82.AF.E3.83.A9.E3.82.B9" name="Abstract.EF.BC.88.E6.8A.BD.E8.B1.A1.EF.BC.89.E3.82.AF.E3.83.A9.E3.82.B9">Abstract(抽象)クラス</h2>
-
-<p>どの string クラスも <code>nsAString</code>(もしくは <code>nsACString</code>) から派生しています。 このクラスはアクセスと文字列操作のための基礎インタフェースを提供します。 具象クラスが <code>nsAString</code>から派生する一方、 <code>nsAString</code>自身はインスタンス化できません。</p>
-
-<p>これは、コードベースの他の部分で抽象オブジェクト記述の記述のために Mozilla が使っている "interface" の考え方によく似ています。 インタフェースについて、クラス名は "nsI" で始まり、"I" は "Interface" を意味します。 抽象クラスは "nsA" で始まり、"A" は "Abstract" を意味します。</p>
-
-<p><code>nsAString</code> から派生した抽象クラスがたくさんあります。 これらの抽象サブクラスもまたインスタンス化できません。 しかし、それらは <code>nsAString</code> よりもわずかながらより詳細に string を記述します。 それからは、抽象クラスの背後で下敷きになった実装が <code>nsAString</code> に加えて特定の能力を提供することを保証します。</p>
-
-<p>以下のリストで主な抽象クラスについて記述します。一度それらになじんだら、<a href="#Appendix_WhatToUse">どのクラスをいつ使うか</a>を参照してください。</p>
-
-<ul>
- <li><code class="decl">nsAString</code><span class="nowiki">: すべての文字列のための基底クラスです。 これは、代入/個々の文字へのアクセス/基本的なの文字操作/文字列比較のための API を提供します。このクラスは XPIDL の </span><code>AString</code> 引数型に対応します。</li>
- <li><code class="decl">nsSubstring</code><span class="nowiki">: string クラスのすべてに対する共通の基本クラスです。文字列の内部のデータへ最適化されたアクセスをするためのものです。</span><code>nsSubstring</code> は null 終端文字列である必要はありません(後方互換のために、<code>nsASingleFragmentString</code> は、このクラスを示す typedef がなされています)。</li>
- <li><code class="decl">nsString</code><span class="nowiki">: null 終端の保存を保証した </span><code>nsSubstring</code> によって作られます。このクラスでは、下敷きとなっている文字バッファにアクセスするためのメソッド(<code>.get()</code>) を使うことができます。(後方互換のために、<code>nsAFlatString</code> は、このクラスを示す typedef がなされています)。</li>
-</ul>
-
-<p>その他の string クラスは、<code>nsSubstring</code> か <code>nsString</code> かを継承しています。 そのため、どの文字列クラスも <code>nsAString</code> と互換があります。</p>
-
-<p><code>nsSubstring</code> と <code>nsAString</code> は共に null 終端である必要のない一続きの文字の配列を示していることに言及するのは重要なことでしょう。 この似通った二つのクラスが存在する必要があるのは何故なのかと疑問に思う人もいるでしょう。 えぇ、<code>nsSubstring</code> は主として、最適化目的に存在します。それは、<code>nsAString</code> が Mozilla 1.0 とともにリリースされた凍結された <code>nsAString</code> crustバイナリレベルの互換を保たなくてはならないからです。 Mozilla 1.7 のリリースまで、<code>nsAString</code> は複合的な断片に分けられた文字列を表す能力を持っていました。 複合的な断片に分けられた文字列のサポートに関するコストは高く、限られたメリットを提供していました。 文字列クラスの複雑さを減らして、パフォーマンスを改善しようという努力のために、複合的な断片に分けられた文字列のサポートを削減する決定がなされました。詳細は <a class="external" href="http://bugzilla.mozilla.org/show_bug.cgi?id=231995">bug 231995</a> を参照して下さい。</p>
-
-<p><code>nsSubstring</code> は、下敷きとしている <code>nsAString</code> バッファへのより効果的なインタフェースを提供していますが、<code>nsAString</code> は未だにパラメータ渡しのためにもっとも共通して使われています。 それは、XPIDL の <code>AString</code> と対応した文字辣クラスだからです。 そのために、この文字列ガイドは <code>nsAString</code> に重点を置いた文字列クラスについて言及し続けるでしょう。</p>
-
-<p>どの stiring も <code>nsAString</code>(もしくは <code>nsACString</code>) から派生しているため、それらはいくつかの基礎となる機能を共有します。</p>
-
-<p>読取専用の共通メソッド:</p>
-
-<ul>
- <li><code class="decl">.Length()</code> - string 中の文字数。</li>
- <li><code class="decl">.IsEmpty()</code> - string が何らかの値を持っているかどうか決定する最も早い方法。次のコードでテストするよりこれを使ってください:<code><em>string</em>.Length == 0</code></li>
- <li><code class="decl">.Equals(<em>string</em>)</code> - もし引数の string が現在の string と同じ値を持っていれば TRUE。</li>
-</ul>
-
-<p>文字列修正の共通メソッド:</p>
-
-<ul>
- <li><code class="decl">.Assign(<em>string</em>)</code> - 新しい値を string に代入する。</li>
- <li><code class="decl">.Append(<em>string</em>)</code> - string に値を追加する。</li>
- <li><code class="decl">.Insert(<em>string</em>, <em>position</em>)</code> - 引数の string を <em>position</em> の文字の後ろに挿入する。</li>
- <li><code class="decl">.Truncate(<em>length</em>)</code> - string を引数の長さに縮める。</li>
-</ul>
-
-<p><a href="#Appendix_nsAString">付録</a>に完全なドキュメントがあります。</p>
-
-<p> </p>
-
-<h3 id=".E8.AA.AD.E5.8F.96.E5.B0.82.E7.94.A8_strings" name=".E8.AA.AD.E5.8F.96.E5.B0.82.E7.94.A8_strings">読取専用 strings</h3>
-
-<p>string 上での<code>const</code> 属性は string が書き込み可能かどうかを示します。 もし、string が<code>const nsAString</code> のように定義されていたら、string 内のデータは操作不可です。 もし、<code>const</code> でないメソッドを <code>const</code> string 上で呼び出そうとしても、 コンパイラはビルド時にエラーとしてこれを示します。</p>
-
-<p>例:</p>
-
-<p> </p>
-
-<pre class="eval">void nsFoo::ReverseCharacters(nsAString&amp; str) {
-      ...
-     str.Assign(reversedStr); // modifies the string
-}
-</pre>
-
-<p>これはコンパイルできない。なぜなら、<code>const</code> クラスへ代入しているから:</p>
-
-<pre class="eval">void nsFoo::ReverseCharacters(const nsAString&amp; str) {
-      ...
-     <strong>str.Assign(reversedStr);</strong>
-}
-</pre>
-
-<p> </p>
-
-<h3 id=".E9.96.A2.E6.95.B0.E3.81.AE.E5.BC.95.E6.95.B0.E3.81.A8.E3.81.97.E3.81.A6.E3.81.AE_Abstract.EF.BC.88.E6.8A.BD.E8.B1.A1.EF.BC.89.E3.82.AF.E3.83.A9.E3.82.B9" name=".E9.96.A2.E6.95.B0.E3.81.AE.E5.BC.95.E6.95.B0.E3.81.A8.E3.81.97.E3.81.A6.E3.81.AE_Abstract.EF.BC.88.E6.8A.BD.E8.B1.A1.EF.BC.89.E3.82.AF.E3.83.A9.E3.82.B9">関数の引数としての Abstract(抽象)クラス</h3>
-
-<p>関数の引数としては、具象クラスの代わりにできる限りもっとも抽象的なインタフェースを使うことが推奨されます。 文字列を参照渡しする場合には、ポインタ('*'文字)ではなく、C++の参照('&amp;' 文字)を用いるのが習慣です。たとえば:</p>
-
-<pre class="eval">// 抽象クラスによる参照
-nsFoo::PrintString(<strong>const nsAString&amp;</strong> str) {..}
-
-// 具象クラスを使っている!
-nsFoo::PrintString(const <strong>nsString&amp;</strong> str) {..}
-
-// ポインタを使っている!
-nsFoo::PrintString(const <strong>nsAString*</strong> str) {..}
-</pre>
-
-<p>抽象クラスはまた、ときどきオブジェクトへの一時的な参照を蓄えるのにも使われます。 これらの両方の使い方は、後述の<a href="#Common_Patterns">共通パターン</a>を参照してください。</p>
-
-<p> </p>
-
-<h2 id=".E5.85.B7.E8.B1.A1.E3.82.AF.E3.83.A9.E3.82.B9" name=".E5.85.B7.E8.B1.A1.E3.82.AF.E3.83.A9.E3.82.B9">具象クラス</h2>
-
-<p>具象クラスは string データを実際に蓄える必要があるコードの中で使うためのものです。 具象クラスのもっともありがちな使い方は、ローカル変数もしくはクラスや構造体のメンバとして使うというものです。 抽象クラスはそれぞれデータの格納方式が違うために、大抵は具象クラスの格納方針も異なります。</p>
-
-<p>以下はに、もっとも一般的な具象クラスの一覧を示します。一度それらになじんだら、<a href="#Appendix_WhatToUse">どのクラスをいつ使うか</a>を参照してください。 The following is a list of the most common concrete classes. Once you are familiar with them, see the appendix describing <a href="#Appendix_WhatToUse">What Class to Use When</a>.</p>
-
-<ul>
- <li><code class="decl">nsString / nsCString</code> - バッファがヒープ上に割り当てられている null 終端 string です。 string オブジェクトがなくなったときにそのバッファは破棄されます。</li>
- <li><code class="decl">nsAutoString / nsCAutoString</code> - nsString から派生した、 string 自身と同じ格納スペースに 64文字バッファを持つ string。 もし、文字長が 64より長い string が nsAutoString に代入されると、新しいバッファがヒープ上に割り当てられます。 これはメンバ変数としてはあまり用いられるべきではないでしょう。</li>
- <li><code class="decl">nsXPIDLString / nsXPIDLCString</code>- nsString から派生した文字列で、 このクラスは <code>getter_Copies()</code> 演算子によって、XPIDL の<code>出力 wstring / string</code> パラメータへの簡単なアクセスをサポートしています。</li>
- <li><code class="decl">nsDependentString</code>- nsString から派生した文字列で、 この string は自分自身ではバッファを<strong>持ちません</strong>。 生の文字列(<code>const PRUnichar*</code> や <code>const char*</code>)を <code>nsAString</code> 型に変換するのに有用です。</li>
- <li><code class="decl">nsPrintfCString</code>- nsString から派生した文字列で、 この string は nsCAutoString のように振る舞います。 このクラスのコンストラクタで <code>printf</code> スタイルのフォーマットの string と引数リストから シングルバイトの string を作ることが出来ます。</li>
- <li><code class="decl">NS_LITERAL_STRING/NS_NAMED_LITERAL_STRING</code>- これらは("abc" のような)リテラル stringを <code>nsAStrings</code> や nsString のサブクラスに変換します。 プラットフォームでサポートしているダブルバイト string リテラルにおいて(例:MSVC++ や -fshort-wchar オプションを付けた GCC)、これらは <code>nsDependentString</code> クラス周辺の単純なマクロです。 これらは <code>nsDependentString</code> による単なるラップより若干速いです。なぜなら、それらの長さの計算にコンパイラを使い、ダブルバイトリテラル string のごちゃごちゃしたクロスプラットフォーム上の細部を隠しもするからです。</li>
-</ul>
-
-<p>ヘルパールーチンなどの副産物として作られた具象クラスはたくさんあります。 これらのクラスは<em>直接使うことを避け</em>るべきです。 string ライブラリから自分のためのクラスを作ってください。</p>
-
-<ul>
- <li><code class="decl">nsSubstringTuple</code> - <a href="#Concatenation">文字列連結</a>を通した生成</li>
- <li><code class="decl">nsDependentSubstring</code> - <a href="#Substring">Substring</a> を通した生成</li>
- <li><code class="decl">nsPromiseFlatString</code> - <a href="#Raw_Pointers"><code>PromiseFlatString()</code></a> を通した生成</li>
-</ul>
-
-<p>もちろん、あなたのコードの中のこれらの string クラスの参照が必要な時はあります。 しかし、一般的なルールでは、これらは使わない方がよいです。</p>
-
-<h2 id=".E3.82.A4.E3.83.86.E3.83.AC.E3.83.BC.E3.82.BF" name=".E3.82.A4.E3.83.86.E3.83.AC.E3.83.BC.E3.82.BF">イテレータ</h2>
-
-<p>イテレータは string の中の位置への参照を保つオブジェクトです。 ある意味で、これらは配列の中のインデックスを参照する数値や、 文字列の中の位置を参照する文字ポインタのようなものです。 イテレータは、文字列への読み込みと書き込みとを区別する文法的な意味も提供します。</p>
-
-<p>イテレータは文字列の部分文字列の抽出のためにもっともよく用いられるべきです。 これらは文字列の内容の修正のための機能を提供しますが、 たいていはヘルパールーチン、つまり文字列自身のメソッドの方が、複雑な文字列変換より早いでしょう。</p>
-
-<p>イテレータは繰り返している文字列クラスで宣言されます:</p>
-
-<pre class="eval">nsAString::const_iterator start, end; // 読み取り専用イテレータ
-    nsAStrings
-    nsAFlatString::iterator substr_start, substr_end; // nsString のための書き込み用イテレータ
-</pre>
-
-<p>イテレータは string にある4つのメソッドのうちあなたが参照したい一つにより初期化されます:</p>
-
-<pre class="eval">// 'str' から読み込もう
-str.BeginReading(start); // 'str' の先頭で 'start' を初期化する
-str.EndReading(end); // 'end' は string の終端になるでしょう
-
-// 'url' に書き込みもしたいんです
-url.BeginWriting(substr_start);
-url.EndWriting(substr_end);
-</pre>
-
-<p>ポインタ参照オペレータ * によってイテレータが指す文字へアクセス可能です。</p>
-
-<pre class="eval">if (*start == '[')
-     printf("Starts with a bracket\n");
-</pre>
-
-<p>上記例では、'end' と 'substr_end' は実際にかつて string の終端だった文字を指すでしょう。 なので、.EndReading() の結果を直接ポインタ内容参照してはいけないことに注意してください。</p>
-
-<p>二つのイテレータが同じ位置を指すかどうかは、== か != で調べることが出来ます。 ++ でイテレータの参照を進めることも出来ます。 ++ はイテレータの前にもってくることが好まれます。そして、それは一時的なイテレータの作成を防ぐことになります。</p>
-
-<p> </p>
-
-<pre class="eval">while (start != end) // string 全体を通して順番に回る
-     ++start;
-</pre>
-
-<p>(const-iterators とは反対のように)書き込みイテレータにより、効果的に string に書き込むことが出来ます:</p>
-
-<p> </p>
-
-<pre class="eval">// * をすべて ! に変える
-while (substr_start != substr_end) {
-     if (*substr_start == '*')
-          *substr_start = '!';
-     ++substr_start;
-}
-</pre>
-
-<p>With the patch for <a class="external" href="http://bugzilla.mozilla.org/show_bug.cgi?id=231995">bug 231995</a>, this loop is now as efficient as iterating with raw character pointers.</p>
-
-<p> </p>
-
-<h3 id=".E3.82.A4.E3.83.86.E3.83.AC.E3.83.BC.E3.82.BF.E3.81.AB.E3.82.88.E3.82.8B.E3.83.AB.E3.83.BC.E3.83.97" name=".E3.82.A4.E3.83.86.E3.83.AC.E3.83.BC.E3.82.BF.E3.81.AB.E3.82.88.E3.82.8B.E3.83.AB.E3.83.BC.E3.83.97">イテレータによるループ</h3>
-
-<p><span id="comment">原文ではindex部分には残っていますが、見出し、a nameを含めて削除されています。</span></p>
-
-<h2 id=".E3.83.98.E3.83.AB.E3.83.91.E3.83.BC.E3.82.AF.E3.83.A9.E3.82.B9.E3.81.A8.E9.96.A2.E6.95.B0" name=".E3.83.98.E3.83.AB.E3.83.91.E3.83.BC.E3.82.AF.E3.83.A9.E3.82.B9.E3.81.A8.E9.96.A2.E6.95.B0">ヘルパークラスと関数</h2>
-
-<h3 id=".E6.96.87.E5.AD.97.E5.88.97.E6.A4.9C.E7.B4.A2" name=".E6.96.87.E5.AD.97.E5.88.97.E6.A4.9C.E7.B4.A2">文字列検索</h3>
-
-<p><code>FindInReadable()</code> はかつての <code>string.Find(..)</code> の代わりのものです。 構文は:</p>
-
-<pre class="eval">PRBool FindInReadable(const nsAString&amp; pattern,
-                      nsAString::const_iterator start, nsAString::const_iterator end,
-                      nsStringComparator&amp; aComparator = nsDefaultStringComparator());
-</pre>
-
-<p>これを使うために、<code>start</code> と <code>end</code> は検索したい文字列のそれぞれ先頭と終端をさしていなくてはなりません。 もし、探している文字列が見つかったら、<code>start</code> と <code>end</code> は見つかった部分の先頭と終端を指すように調整されます。 戻り値は、PR_TRUE か PR_FALSE で、文字列が見つかったかどうかを示します。</p>
-
-<p>例:</p>
-
-<pre class="eval">const nsAString&amp; str = GetSomeString();
-nsAString::const_iterator start, end;
-
-str.BeginReading(start);
-str.EndReading(end);
-
-NS_NAMED_LITERAL_STRING(valuePrefix, "value=");
-
-if (FindInReadable(valuePrefix, start, end)) {
-    // end は今、検索した文字の後ろを指している
-    valueStart = end;
-}
-</pre>
-
-<h3 id=".E3.83.A1.E3.83.A2.E3.83.AA.E5.89.B2.E5.BD.93" name=".E3.83.A1.E3.83.A2.E3.83.AA.E5.89.B2.E5.BD.93">メモリ割当</h3>
-
-<p>既存の文字列から新しい文字列バッファ(<code>PRUnichar*</code>/<code>char*</code>)を割り当てるために好ましいメソッドは、 以下のメソッドです:</p>
-
-<ul>
- <li><strong><code>PRUnichar* ToNewUnicode(<em>nsAString&amp;</em>)</code></strong> - nsAString から <code>PRUnichar*</code> を割り当てます。</li>
- <li><strong><code>char *ToNewCString(<em>nsACString&amp;</em>)</code></strong> - nsACString から <code>char*</code> バッファを割り当てます。 このメソッドは nsAStrings 上でも働きますが、暗黙の<a href="#Lossy_Conversions">損失の多い変換</a>となるでしょう。 この機能は入力が厳密に ASCII であることが判っている場合にだけ使うべきです。しばしば UTF8 への変換がより適しています。 次項 <code class="decl">ToNewUTF8String</code> も参照のこと。</li>
- <li><strong><code>char* ToNewUTF8String(<em>nsAString&amp;</em>)</code></strong> - 与えられた nsAString の UTF8 エンコードされたバージョンを含む新しい <code>char*</code> バッファを割り当てます。 詳細は<a href="#Unicode_Conversion">Unicode 変換</a>を参照して下さい。</li>
-</ul>
-
-<p>これらのメソッドは These methods return a buffer allocated using XPCOM's allocator (<code>nsMemory::Alloc</code>) instead of the traditional allocator (<code>malloc</code>, etc.). You should use <code>nsMemory::Free</code> to deallocate the result when you no longer need it. これらのメソッドは伝統的なアロケータ (<code>malloc</code> など)の代わりに XPCOM のアロケータ (<code>nsMemory::Alloc</code>)で割り当てられたバッファを返すでしょう。 必要としなくなった時、その戻り値を開放するために <code>nsMemory::Free</code> を使うべきです。</p>
-
-<h3 id=".E6.97.A2.E5.AD.98.E3.81.AE.E6.96.87.E5.AD.97.E5.88.97.E3.81.AE.E6.96.AD.E7.89.87" name=".E6.97.A2.E5.AD.98.E3.81.AE.E6.96.87.E5.AD.97.E5.88.97.E3.81.AE.E6.96.AD.E7.89.87">既存の文字列の断片</h3>
-
-<p>実際に新しいスペースを割り当てたり、その文字列の部分文字列の文字をコピーしたりしないで既存の文字列の部分文字列を参照するのはとても簡単です。<code>Substring()</code> はそのような文字列への参照を生成するのにとてもよいメソッドです。</p>
-
-<pre class="eval">void ProcessString(const nsAString&amp; str) {
-    const nsAString&amp; firstFive = Substring(str, 0, 5);
-    // firstFive は今最初の 5 文字を示す文字列です
-}
-</pre>
-
-<p> </p>
-
-<h2 id="Unicode_.E5.A4.89.E6.8F.9B" name="Unicode_.E5.A4.89.E6.8F.9B">Unicode 変換</h2>
-
-<p>文字列は二つの基本的な形式で<em>保存</em>することができます。 8 ビット文字(<code>char</code>)文字列もしくは、16 ビット文字(<code>PRUnichar</code>)文字列です。 クラス名に大文字の「C」を持つどの文字列クラスも、8 ビット文字を含みます。 それには、nsCString、nsDependentCString などのクラスが含まれます。 「C」を<em>持たない</em>どの文字列クラスも、16 ビット文字を含みます。</p>
-
-<p>格納構造に加え、文字列はまた、<em>エンコード</em>もされているという側面があります。 文字列のエンコーディングは、8 もしくは 16 ビットの文字列として unicode 文字値の組を保持することを意味します。 大きな文字値を小さな容量の文字列として保存する方法がたくさんあるため、エンコーディングがたくさんあります。 <em>文字セット</em>は、特定のエンコーディング方法の人間が理解できる名称です。 例えば、「ASCII」は 7 ビット値を 8 ビット文字列にマッピングした文字セットです。 「isolatin1」は8 ビット文字列で西欧文字エンコーディングするための共通文字セットです。</p>
-
-<p>文字列の文字セットは、文字列クラスの名前によって定義されて<em>いません</em>。 そのかわり、文字列の適切なエンコーディングを決定することは実装者の責任です。 もっとも一般的なのエンコーディングは:</p>
-
-<ul>
- <li><strong>ASCII</strong> - 基本的な英語のみの文字列のための 8 ビットエンコーディングです。 どの ASCII 値も配列の正確に 1 バイトに格納されます。</li>
- <li><strong>UCS2</strong> - 基本の unicode 格納のためのエンコーディングです。 UCS2 で格納された文字の unicode 値は、文字列クラスの正確に一つの 16 ビット <code>PRUnichar</code> に格納されます。</li>
- <li><strong>UTF8</strong> - unicode 値のための8 ビットエンコーディング。 どの UTF8 値も 1 から 6 バイトの組で保持されます。 UTF8 は unicode 文字セット全体を表現する能力があり、効率よく UTF32 へマップします。</li>
- <li><strong>UTF16</strong> - 拡張 unicode 格納のための 16 ビットエンコーディングです。 UCS2 に対して後方互換性があります。 UTF16で格納された unicode 文字値は、文字列クラスの 1 つもしくは 2 つの 16ビット <code>PRUnichar</code> を必要とします。 このエンコーディングは現在頻繁には使われません。しかし、より新しい unicode 標準が採用されるにつれて、きっと増えるでしょう。 UTF16 は unicode 文字セット全体を表現する能力があり、効率よく UTF32 へマップします。</li>
-</ul>
-
-<p>加えて、国際化ライブラリによって提供される文字通り何百のエンコーディングがあります。 これらのライブラリへのアクセスは、アプリケーションの一部であるか(例えば Mozilla の <code>nsICharsetConversionManager</code> のように)、オペレーティングシステムへビルドされている(例えば Unix 系 OS の <code>iconv()</code> のように)でしょう。</p>
-
-<p>既存のコードで作業する時、正しい変換機構を決定するために、操作している文字列の現在の使い方を吟味することは重要です。</p>
-
-<p>新しいコードを書く時、どの格納クラスそしてエンコーディングがもっとも適切かを知ろうとしても混乱するかもしれません。この問題への単純な答えはありません。しかし、少数の重要なガイドラインがあります:</p>
-
-<ul>
- <li><strong>文字列はいつも ASCII ?</strong> 最初の、そして最大のもので、どの種類の値が文字列に格納されているかを決めることが必要です。文字列がいつでも内部の ASCII 文字列、例えば「left」、「true」、「background」などであれば、そのままの C-文字列がたぶん選ぶべきものです。</li>
- <li><strong>文字列が ASCII であれば、ASCII でない文字列と比較・代入・さもなければ相互作用しますか?</strong> 8 ビット ASCII 値を 16 ビット UCS2 文字列と代入もしくは比較するとき、実行時に「膨らませる」必要があります。 もし、文字列が十分に小さい(そう、 64 バイトより小さい)ならば、余計な変換を避けるために 16 ビット unicode クラスにも文字列を格納する、というのも手です。そのかわり、ASCII 文字列が、8 ビット文字列だった場合に比べて 2 倍のスペース、つまり 16 ビット unicode 文字列と同じスペースを占める、という欠点があります。</li>
- <li><strong>文字列はたいてい ASCII であるけれど、unicode をサポートしなくていいのでしょうか?</strong> 文字列はほとんど大抵 ASCII であるけれど、unicode 値も格納する必要があるのなら、UTF8 は正しいエンコーディングだ。 ASCII 値は 8 ビットのまま格納され、拡張 unicode 値は 2 〜 6 バイトで格納されるでしょう。 しかし、もし文字列が unicode 値との比較や代入までも必要とするのなら、実行時変換が必要となるでしょう。</li>
- <li><strong>ASCII でない大きな文字列データを格納しますか?</strong> ここに至るまでは、 UTF8 は理想的な文字列に見えます。 欠点は、西欧以外の文字のほとんど(例えば日本語文字)を使う場合に、UTF8 は「ふくれ気味な」エンコーディングです。 UTF-8 では、日本語文字列は大抵一文字あたり 3 バイト必要です。 UCS2 では、一文字が 2 バイトであるのと比べて、日本語テキストにおいて UTF-8 は相当に多くのサイズを消費します。</li>
- <li><strong>unicode 文字列のコンテントを処理する必要がありますか?</strong> UTF8 やその他の 8 ビット格納形式で unicode 値をエンコーディングすることの一つの問題点は、実際も unicode 値も文字列の中で複数バイトにわたることです。 ほとんどのエンコーディングでは、実際のバイト数は文字から文字へで異なります。 それぞれの文字を通して反復処理する必要があるときには、エンコーディングを考慮する必要があります。 UCS2 文字列で反復処理するなら、これは非常に簡易です。なぜなら、どの 16 ビット PRUnichar も、unicode 値と対応するからです。</li>
-</ul>
-
-<p>ASCII、UTF8、UCS2 の変換を助けるため、 いくつかのヘルパーメソッドとヘルパークラスがあります。 これらのクラスのうちいくつかは、スタック上の一時オブジェクトとしてもっともよく使われるため、関数のように見えます。</p>
-
-<p>To assist with ASCII, UT8 and UCS2 conversions, there are some helper methods and classes. Some of these classes look like functions, becuase they are most often used as temporary objects on the stack.</p>
-
-<h4 id="UTF8_.2F_UCS2_.E5.A4.89.E6.8F.9B" name="UTF8_.2F_UCS2_.E5.A4.89.E6.8F.9B">UTF8 / UCS2 変換</h4>
-
-<ul>
- <li><code class="decl">NS_ConvertUTF8toUCS2(<em>const nsACString&amp;</em>)</code> - UTF-8 エンコードされた nsACString もしくは <code>const char*</code> を UCS2 string に変換する nsAutoString のサブクラス。もし、代わりに <code>const PRUnichar*</code> バッファが必要なら、.get() メソッドを使ってください。例:</li>
-</ul>
-
-<pre class="eval">/* シグネチャ: void HandleUnicodeString(const nsAString&amp; str); */
-object-&gt;HandleUnicodeString(<strong>NS_ConvertUTF8toUCS2</strong>(utf8String));
-</pre>
-
-<pre class="eval">/* シグネチャ: void HandleUnicodeBuffer(const PRUnichar* str); */
-object-&gt;HandleUnicodeBuffer(<strong>NS_ConvertUTF8toUCS2</strong>(utf8String).get())
-
-</pre>
-
-<ul>
- <li><code class="decl">NS_ConvertUCS2toUTF8(<em>const nsAString&amp;</em>)</code> - UTF8 エンコードされた nsAString を UTF8 された文字列に変換する nsAFlatCString。上記同様に、<code>const char*</code> へアクセスするときは、.get() を使ってください。</li>
- <li><code class="decl">NS_ConvertUCS2toUTF8(<em>const nsAString&amp;</em>)</code> - UCS2 エンコードされた nsAString を UTF-8 エンコードされた string へ変換する nsCAutoString。 上記項目と同様に、<code>const char*</code> にアクセスするために .get() を使うことが出来ます。</li>
-</ul>
-
-<pre class="eval">/* シグネチャ: void HandleUTF8String(const nsACString&amp; str); */
-object-&gt;HandleUTF8String(NS_ConvertUCS2toUTF8(unicodeString));
-</pre>
-
-<pre class="eval">/* シグネチャ: void HandleUTF8Buffer(const char* str); */
-object-&gt;HandleUTF8Buffer(NS_ConvertUCS2toUTF8(unicodeString).get())
-</pre>
-
-<ul>
- <li><code class="decl">CopyUTF8toUCS2(<em>const nsACString&amp;, const nsAString&amp;</em>)</code> - 変換と割り当てを行う。</li>
-</ul>
-
-<pre class="eval">// UCS2 値を返す
-void Foo::GetUnicodeValue(nsAString&amp; result) {
-    CopyUTF8toUCS2(mLocalUTF8Value, result);
-}
-</pre>
-
-<ul>
- <li><code class="decl">CopyUCS2toUTF8(<em>const nsAString&amp;, const nsACString&amp;</em>)</code> - 変換と割り当てを行う。</li>
-</ul>
-
-<pre class="eval">// UTF8 値を返す
-void Foo::GetUTF8Value(nsACString&amp; result) {
-    CopyUCS2toUTF8(mLocalUnicodeValue, result);
-}
-</pre>
-
-<ul>
- <li><code class="decl">ToNewUTF8String(<em>const nsAString&amp;</em>)</code> - 割り当てと変換を行う</li>
-</ul>
-
-<pre class="eval">void Foo::GetUTF8Value(const char** result) {
-  *result = ToNewUTF8String(mLocalUnicodeValue);
-}
-</pre>
-
-<p> </p>
-
-<h3 id=".E6.90.8D.E5.A4.B1.E3.81.AE.E5.A4.9A.E3.81.84.E5.A4.89.E6.8F.9B" name=".E6.90.8D.E5.A4.B1.E3.81.AE.E5.A4.9A.E3.81.84.E5.A4.89.E6.8F.9B">損失の多い変換</h3>
-
-<p>以下はオリジナル文字列が ASCII ベースであることが保証できるときだけ使われるべきです。</p>
-
-<h4 id="UCS2_.E3.81.8B.E3.82.89_ASCII_.E3.81.B8.E3.81.AE.E3.82.B3.E3.83.B3.E3.83.90.E3.83.BC.E3.82.BF" name="UCS2_.E3.81.8B.E3.82.89_ASCII_.E3.81.B8.E3.81.AE.E3.82.B3.E3.83.B3.E3.83.90.E3.83.BC.E3.82.BF">UCS2 から ASCII へのコンバータ</h4>
-
-<p>これらのコンバータ(変換機構)は、変換プロセスの中で<em><strong>情報の消失</strong></em>があるため<em><strong>とても危険</strong></em>です。<em><strong>UCS2 から ASCII への変換は、文字列が ASCII であることが保証されない限りさけるべきです</strong></em>。どの UCS2(16 ビット)文字も、8 ビット文字は単に 8 ビット文字にキャストされます。それは、0xFF を超えるすべての文字の値は任意の 8 ビット文字に変換されてしまうということです。</p>
-
-<ul>
- <li><code class="decl">NS_LossyConvertUCS2toASCII(<em>nsAString</em>)</code> - string の圧縮した値を含む一時バッファを持つ nsCAutoString。</li>
- <li><code class="decl">CopyUCS2toASCII(<em>nsAString</em>, <em>nsACString</em>)</code> - UCS2 から ASCII 文字列オブジェクトへコピーの変換をします。</li>
- <li><code class="decl">ToNewCString(<em>nsAString</em>)</code> - 新しい <code>char*</code> 文字列を割り当てます。</li>
-</ul>
-
-<p> </p>
-
-<h4 id="ASCII_.E3.81.8B.E3.82.89_UCS2_.E3.81.B8.E3.81.AE.E3.82.B3.E3.83.B3.E3.83.90.E3.83.BC.E3.82.BF" name="ASCII_.E3.81.8B.E3.82.89_UCS2_.E3.81.B8.E3.81.AE.E3.82.B3.E3.83.B3.E3.83.90.E3.83.BC.E3.82.BF">ASCII から UCS2 へのコンバータ</h4>
-
-<p>これらは、<em>ASCII でない文字列を無意味な unicode 文字列に壊してしまう</em>ため、<em><strong>とても危険</strong></em>です。 <em><strong>ASCII から UCS2 への変換は、文字列が ASCII であることが保証されない限りさけるべきです</strong></em>。 つまり、もし複数バイト文字セットの 8 ビット文字列エンコードされたものを持っている場合、文字列のどのバイトもただキャストによって 16 バイト数値に"水増し"されるだけなのです。</p>
-
-<p>例えば、文字列の最初の unicode 文字が 4 バイトの UTF-8 シーケンスで表されているような UTF-8 文字列を想像してください。"水増しされた" unicode 文字列の最初の 4 バイトは最初の文字をあらわす 4 つの値を含んでいます。これらの値は文字列を UCS2 として扱うなら無意味です。</p>
-
-<ul>
- <li><code class="decl">NS_ConvertASCIItoUCS2(<em>nsACString</em>)</code> - string の水増しした値を含む一時バッファを持つ nsCAutoString。</li>
- <li><code class="decl">NS_ConvertASCIItoUCS2(<em>nsACString</em>)</code> - 水増しした文字列の値を含む一時バッファをもつ nsAFlatString。</li>
- <li><code class="decl">CopyASCIItoUCS2(<em>nsACString</em>, <em>nsAString</em>)</code> - ある文字列から unicode 文字列オブジェクトへコピーの変換をします。</li>
- <li><code class="decl">ToNewUnicode(<em>nsACString</em>)</code> - 水増しした値を含む新しい <code>PRUnichar*</code> 文字列を生成します。</li>
-</ul>
-
-<p> </p>
-
-<h2 id=".E5.85.B1.E9.80.9A.E3.83.91.E3.82.BF.E3.83.BC.E3.83.B3" name=".E5.85.B1.E9.80.9A.E3.83.91.E3.82.BF.E3.83.BC.E3.83.B3">共通パターン</h2>
-
-<p>多くの API は、呼び出し元に文字列を返すためのバッファを割り当てるメソッドとなります。 バッファを使い終えた時に呼び出し元が文字列を解放することを覚えている必要があるため、これはトリッキーでもあります。 幸運にも、<code>nsXPIDLString</code> クラスでこれを簡単に行えます。</p>
-
-<p><br>
- メソッドはこのような感じです:</p>
-
-<pre class="eval">void GetValue(PRUnichar** aValue)
-{
-    *aValue = ToNewUnicode(foo);
-}
-</pre>
-
-<p>文字列クラスなしで、呼び出し元は文字列を解放する必要があるでしょう:</p>
-
-<pre class="eval">{
-    PRUnichar* val;
-    GetValue(&amp;val);
-
-    if (someCondition) {
-        // 値を解放することを忘れてはいけない
-        // don't forget to free the value!
-        nsMemory::Free(val);
-        return NS_ERROR_FAILURE;
-    }
-
-    ...
-    // 後で、やはり解放を忘れてはいけない!
-    // and later, still don't forget to free!
-    nsMemory::Free(val);
-}
-</pre>
-
-<p><code>nsXPIDLString</code> を使えば、このことを心配する必要はありません。 <code>getter_Copies()</code> を使って文字列クラスをラップするだけで、 スコープの外へ出た時にクラスがバッファを解放するでしょう。</p>
-
-<pre class="eval">{
-    nsXPIDLString val;
-    GetValue(getter_Copies(val));
-</pre>
-
-<pre class="eval">    // val はここで自身によって解放されるでしょう
-    // val will free itself here
-    if (someCondition)
-        return NS_ERROR_FAILURE;
-    ...
-    // 後で、やはり解放する必要がない
-    // and later, still nothing to free
-}
-</pre>
-
-<p>結果としてのコードはとても単純で、読みやすいです。</p>
-
-<p> </p>
-
-<h3 id=".E3.83.AA.E3.83.86.E3.83.A9.E3.83.AB.E6.96.87.E5.AD.97.E5.88.97" name=".E3.83.AA.E3.83.86.E3.83.A9.E3.83.AB.E6.96.87.E5.AD.97.E5.88.97">リテラル文字列</h3>
-
-<p><em>リテラル文字列</em>は C++ コードに書かれた生の文字列の値です。 例えば、<code>printf("Hello World\n");</code> ステートメント中の値 <code>"Hello World\n"</code> はリテラル文字列です。 nsAString や nsACString が必要なとき、リテラル文字列値を挿入する必要がしばしば発生します。 これら 4 つのマクロは必要な変換のために提供されています:</p>
-
-<ul>
- <li><code class="decl">NS_LITERAL_CSTRING(<em>literal string</em>)</code> - 一時的 nsCString</li>
- <li><code class="decl">NS_NAMED_LITERAL_CSTRING(<em>variable</em>,<em>literal string</em>);</code> - <em>variable</em> と名づけられた nsCString 変数を定義します</li>
- <li><code class="decl">NS_LITERAL_STRING(<em>literal string</em>)</code> - Unicode 版<em>literal string</em>(リテラル文字列)を持つ一時 nsString</li>
- <li><code class="decl">NS_NAMED_LITERAL_STRING(<em>variable</em>,<em>literal string</em>);</code> - Unicode 版 <em>literal string</em>(リテラル文字列)を持つ、名称 <em>variable</em> の nsString 変数を宣言する</li>
-</ul>
-
-<p>nsDependentCString もまた nsCString の中の文字列の値をラップすることを考えれば、これらのマクロの <code>CSTRING</code> 版は、一見不要に見えます。これらのマクロの長所は、これらの文字列の長さがコンパイル時に計算できるため、実行時に長さを決めるために文字列を読み込む必要がありません。</p>
-
-<p>これらのマクロの <code>STRING</code> 版は、(例えば、MSVC++ や -fshort-wchar オプション付きの GCC のように)リテラル unicode 文字列をサポートするプラットフォーム上での実行時の変換をしないで、unicode 版の固定のリテラル string を宣言する移植性のある方法を提供します。</p>
-
-<pre class="eval">// Init(const PRUnichar*) 呼び出し
-Init(L"start value"); // よくない - L"..." は移植性が低い!
-Init(NS_ConvertASCIItoUCS2("start value").get()); // よくない - 実行時の ASCII-&gt;UCS2 変換!
-
-// Init(const nsAString&amp;) 呼び出し
-Init(nsDependentString(L"start value")); // よくない - 移植性が低い!
-Init(NS_ConvertASCIItoUCS2("start value")); // よくない - 実行時の ASCII-&gt;UCS2 変換!
-
-// Init(const nsACString&amp;) 呼び出し
-Init(nsDependentCString("start value")); // よくない - 文字列長が実行時に決まる
-</pre>
-
-<p>適切な NS_LITERAL_[C]STRING 使用法を以下にいくつか示します。</p>
-
-<pre class="eval">// Init(const PRUnichar*) 呼び出し
-Init(NS_LITERAL_STRING("start value").get());
-
-// Init(const nsAString&amp;) 呼び出し
-Init(NS_LITERAL_STRING("start value"));
-
-// Init(const nsACString&amp;) 呼び出し
-Init(NS_LITERAL_CSTRING("start value"));
-</pre>
-
-<p><br>
- これらのマクロを使った問題の追跡に役に立つであろう詳細をいくつか示します:</p>
-
-<p><code>NS_LITERAL_STRING</code> は(Windows や Macintosh などといった)いくつかのプラットフォーム上でコンパイル時に UCS2 への変換をしますが、他のプラットフォームでは実行時に変換されます。NS_LITERAL_STRING を使うことによって、あなたのコードで問題のプラットフォームのために最良の変換が使われることを保証します。</p>
-
-<p>いくつかのプラットフォームで実行時変換が行われるため、<code>NS_LITERAL_STRING/NS_NAMED_LITERAL_STRING</code> マクロ内部でのリテラル文字列連結の使用は、それらのプラットフォームではコンパイルされるでしょうが、コンパイル時変換をサポートするプラットフォーム上ではコンパイルできないでしょう。</p>
-
-<p>以下に例示します:</p>
-
-<p> </p>
-
-<pre class="eval">// Init(nsAString&amp;) 呼び出し
-Init(NS_LITERAL_STRING("start "
-     "value")); // いくつかのプラットフォームでだけコンパイルされます。
-</pre>
-
-<p>その理由は、いくつかのプラットフォームで<code>L"..."</code> 構文が使われますが、これは連結の最初の文字列にだけ適用されるためです(<code>"start "</code>)。コンパイラは unicode でない文字列 <code>"value"</code> との連結を試みると困惑します。</p>
-
-<h3 id=".E6.96.87.E5.AD.97.E5.88.97.E9.80.A3.E7.B5.90" name=".E6.96.87.E5.AD.97.E5.88.97.E9.80.A3.E7.B5.90">文字列連結</h3>
-
-<p>文字列は <code>+</code> 演算子を用いて互いに連結可能です。結果となる文字列は <code>const nsAString</code> オブジェクトとなります。 結果となる文字列は、その他の <code>nsAString</code> のように振る舞いをさせることも参照もできます。 連結は<em>部分文字列のコピーではありません</em>。代わりに、オリジナルの文字列を参照するに過ぎません。 結果としての文字列は、少なくとも連結された文字列と同じだけの生存期間を持つという意味で、その部分文字列のすべてに依存します。</p>
-
-<p>例えば、二つの文字列の値を使うことができ、接続を <code>const nsAString&amp;</code> をとる他の関数に引き渡すことができます:</p>
-
-<pre class="eval">void HandleTwoStrings(const nsAString&amp; one, const nsAString&amp; two) {
-    // HandleString(const nsAString&amp;) 呼び出し
-    HandleString(one + two);
-}
-</pre>
-
-<p>注意:このケースでは、二つの文字列は暗黙のうちに一時的な nsString に結びつけられ、 一時的 string は、<code>HandleString</code> に渡されます。 もし、<code>HandleString</code> がその入力を他の nsString へ代入したならば、 いくつかの文字列を連結し、その結果を一時変数に格納することもできます:</p>
-
-<pre class="eval">NS_NAMED_LITERAL_STRING(start, "start ");
-NS_NAMED_LITERAL_STRING(middle, "middle ");
-NS_NAMED_LITERAL_STRING(end, "end");
-// 3つの相互依存した断片を持つ string を生成 - 複雑なコピーではない!
-nsString combinedString = start + middle + end;
-</pre>
-
-<p> </p>
-
-<pre class="eval">// void HandleString(const nsAString&amp;); 呼び出し
-HandleString(combinedString);
-</pre>
-
-<p>もし、一度きりだけ使う一時的なものを作るために <code>NS_LITERAL_STRING</code> を使うのなら、 結合の中で定義するのが安全でしょう。なぜなら、文字列バッファは(<code>nsSubstringTuple</code> 型の)一時的結合オブジェクトと同じ生存期間となるからです。</p>
-
-<pre class="eval">// HandlePage(const nsAString&amp;); 呼び出し
-// 結合された文字列はその部分文字列と同じ生存期間なので安全
-HandlePage(NS_LITERAL_STRING("start ") + NS_LITERAL_STRING("end"));
-</pre>
-
-<h3 id=".E3.83.AD.E3.83.BC.E3.82.AB.E3.83.AB.E5.A4.89.E6.95.B0" name=".E3.83.AD.E3.83.BC.E3.82.AB.E3.83.AB.E5.A4.89.E6.95.B0">ローカル変数</h3>
-
-<p>関数内のローカル変数は通常、スタック上に確保されます。 <code>nsAutoString</code>/<code>nsCAutoString</code> クラスは、 <code>nsString</code>/<code>nsCString</code> の派生物です。 これらが、文字列自身と同じ格納領域に割り当てられた 64 文字のバッファを持ちます。 もし、nsAutoString がスタック上に割り当てられていれば、文字列は破棄時に 64 文字スタックバッファ持ちます。 They own a 64-character buffer allocated in the same storage space as the string itself. If the nsAutoString is allocated on the stack, then it has at its disposal a 64-character stack buffer. このことにより、小さな文字列を扱う時、余計なメモリの割り当てをしないで実装することができます。</p>
-
-<pre class="eval">...
-nsAutoString value;
-GetValue(value); // 結果が 64 文字より少なければ、
-                 // 割り当てを省くことができる。
-GetValue(value); // if the result is less than 64 characters,
-                 // then this just saved us an allocation
-...
-</pre>
-
-<h3 id=".E3.83.A1.E3.83.B3.E3.83.90.E5.A4.89.E6.95.B0" name=".E3.83.A1.E3.83.B3.E3.83.90.E5.A4.89.E6.95.B0">メンバ変数</h3>
-
-<p>一般に、メンバ変数としては、<code>nsString</code> や <code>nsCString</code> といった具象クラスを使うべきでしょう。</p>
-
-<pre class="eval">class Foo {
-    ...
-    // これらは UTF8 や unicode 値を各々格納する
-    // these store UTF8 and Unicode values respectively
-    nsCString mLocalName;
-    nsString mTitle;
-};
-</pre>
-
-<p>文字列は直接クラスの中で宣言され、文字列へのポインタとして宣言されるのではないことに注意してください。 このようにはしないでください:</p>
-
-<p>Note that the strings are declared directly in the class, not as pointers to strings. Don't do this:</p>
-
-<pre class="eval">class Foo {
-public:
-    Foo() { mLocalName = new nsCString(); }
-    ~Foo() { delete mLocalName; }
-
-private:
-    // これらは UTF8 や unicode 値を各々格納する
-    // these store UTF8 and Unicode values respectively
-    nsCString* mLocalName;
-};
-</pre>
-
-<p>上記のコードは文字列オブジェクトのコストを節約しているように見えるかもしれませんが、 <code>nsString</code>/<code>nsCString</code> は小さなオブジェクトです。 割り当てのオーバーヘッドは、ポインタで節約するよりも数バイト勝っているだけです。</p>
-
-<p><br>
- その他の間違ったパターンとしては、メンバ変数として、 <code>nsAutoString</code>/<code>nsCAutoString</code> を使うというものがあります。 <a href="#Local_Variables">ローカル変数</a>で述べたように、 これらのクラスはとても大きなバッファを中にもって作られます。 もしクラスの中にこれらを持つことは、クラスを 64 バイト(<code>nsCAutoString</code>)もしくは 128 バイト(<code>nsAutoString</code>)膨らませることになります。</p>
-
-<p><br>
- 例:</p>
-
-<pre class="eval">class Foo {
-    ...
-
-    // Foo クラスが 128 バイト膨らむ!
-    // bloats 'Foo' by 128 bytes!
-    nsAutoString mLocalName;
-};
-</pre>
-
-<p> </p>
-
-<h3 id=".E7.94.9F.E3.81.AE.E6.96.87.E5.AD.97.E3.83.9D.E3.82.A4.E3.83.B3.E3.82.BF" name=".E7.94.9F.E3.81.AE.E6.96.87.E5.AD.97.E3.83.9D.E3.82.A4.E3.83.B3.E3.82.BF">生の文字ポインタ</h3>
-
-<p><code>PromiseFlatString()</code> は、元になった文字列と同じ値を含む null 終端のバッファを持つ一時バッファを生成するのに使うことができます。 <code>PromiseFlatString()</code> は必要ならば一時バッファを作ります。 これは多くの場合、nsAString を null 終端文字列を要求する API に通すために使われます。</p>
-
-<p>以下の例では、nsAString はリテラル文字列と一体化されます。そして結果は単純な文字バッファを求める API に通されます。</p>
-
-<pre class="eval">// URL を修正し、AddPage(const PRUnichar* url) へ通す
-void AddModifiedPage(const nsAString&amp; url) {
-    NS_NAMED_LITERAL_STRING(httpPrefix, "<a class="external" href="http://" rel="freelink">http://</a>");
-    const nsAString&amp; modifiedURL = httpPrefix + url;
-
-    // 一時バッファ生成
-    AddPage(PromiseFlatString(modifiedURL).get());
-}
-</pre>
-
-<p>既に null 終端である文字列を扱うとき、<code>PromiseFlatString()</code> は洗練された方法です。 そのようなケースで一時バッファを作らなくてすみます。</p>
-
-<p> </p>
-
-<pre class="eval">// URL を修正し、AddPage(const PRUnichar* url) へ通す
-void AddModifiedPage(const nsAString&amp; url, PRBool addPrefix) {
-    if (addPrefix) {
-        // 一時バッファを作成<strong>なければならない</strong> - 文字列は複数の断片でできている
-        NS_NAMED_LITERAL_STRING(httpPrefix, "<a class="external" href="http://" rel="freelink">http://</a>");
-        AddPage(PromiseFlatString(httpPrefix + modifiedURL));
-    } else {
-        // 一時バッファを作成<strong>してもよい</strong>、実行時にチェックする
-        AddPage(PromiseFlatString(url).get());
-    }
-}
-</pre>
-
-<p> </p>
-
-<h2 id="IDL" name="IDL">IDL</h2>
-
-<p>文字列ライブラリは IDL を通しても利用できます。 特別に IDL 型を定義することによって属性やメソッドを宣言することで、 string クラスは対応メソッドへの引数として使えます。</p>
-
-<p> </p>
-
-<h3 id="IDL_.E6.96.87.E5.AD.97.E5.88.97.E5.9E.8B" name="IDL_.E6.96.87.E5.AD.97.E5.88.97.E5.9E.8B">IDL 文字列型</h3>
-
-<p>C++ シグネチャにより、メソッドのすべての引数が<a href="#Abstract_Classes">抽象クラス</a>をベースにしているようなのような上述通りの抽象型を定義することができます。 以下のテーブルには IDL のそれぞれの文字列型の目的を記します。</p>
-
-<table class="standard-table">
- <tbody>
-  <tr>
-   <th class="header">IDL type</th>
-   <th class="header">C++ Type</th>
-   <th class="header">Purpose</th>
-  </tr>
-  <tr>
-   <td><code>string</code></td>
-   <td><code>char*</code></td>
-   <td>Raw character pointer to ASCII (7-bit) string, no string classes used. High bit is not guaranteed across XPConnect boundaries</td>
-  </tr>
-  <tr>
-   <td><code>wstring</code></td>
-   <td><code>PRUnichar*</code></td>
-   <td>Raw character pointer to UTF-16 string, no string classes used</td>
-  </tr>
-  <tr>
-   <td><code>AString</code></td>
-   <td><code>nsAString</code></td>
-   <td>UTF-16 string</td>
-  </tr>
-  <tr>
-   <td><code>ACString</code></td>
-   <td><code>nsACString</code></td>
-   <td>8-bit string, all bits are preserved across XPConnect boundaries</td>
-  </tr>
-  <tr>
-   <td><code>AUTF8String</code></td>
-   <td><code>nsACString</code></td>
-   <td>UTF-8 string - converted to UTF-16 as necessary when value is used across XPConnect boundaries</td>
-  </tr>
-  <tr>
-   <td><code>DOMString</code></td>
-   <td><code>nsAString</code></td>
-   <td>UTF-16 string used in the DOM. More or less the same as <code>AString</code>, but in JavaScript it has no distinction between whether the string is void or just empty. (not sure on this, looking for corrections.</td>
-  </tr>
- </tbody>
-</table>
-
-<h3 id="C.2B.2B_.E3.82.B7.E3.82.B0.E3.83.8D.E3.83.81.E3.83.A3" name="C.2B.2B_.E3.82.B7.E3.82.B0.E3.83.8D.E3.83.81.E3.83.A3">C++ シグネチャ</h3>
-
-<p>IDL では、<code>in</code> 引数は読み込み専用で、*String 引数に対する C++ シグネチャにより、それらの引数のための <code>const nsAString&amp;</code> を使った上記ガイドラインが行えます。 <code>out</code> と <code>inout</code> 引数は、呼ばれた側で書き込み可能なよう単純に <code>nsAString</code> として定義されています。</p>
-
-<table class="standard-table">
- <tbody>
-  <tr>
-   <th class="header">IDL</th>
-   <th class="header">C++</th>
-  </tr>
-  <tr>
-   <td>
-    <pre class="eval">
-interface nsIFoo : nsISupports {
-
-    attribute AString utf16String;
-
-
-
-
-    AUTF8String getValue(in ACString key);
-
-};
-</pre>
-   </td>
-   <td>
-    <pre class="eval">
-class nsIFoo : public nsISupports {
-
-     NS_IMETHOD GetUtf16String(nsAString&amp;
-                               aResult) = 0;
-     NS_IMETHOD SetUtf16String(const nsAString&amp;
-                              aValue) = 0;
-
-     NS_IMETHOD GetValue(const nsACString&amp; aKey,
-                     nsACString&amp; aResult) = 0;
-};
-</pre>
-   </td>
-  </tr>
- </tbody>
-</table>
-
-<p>上記の例では、<code>unicodeString</code> は Unicode 文字列として扱われています。 <code>GetUnicodeString()</code> のインプリメントでは <code>aResult.Assign</code> を値を返す("return"する)のに使っています。 <code>SetUnicodeString()</code> では、文字列の値は<a href="#Iterators">イテレータ</a>、<a href="#Raw_Pointers"><code>PromiseFlatString</code></a>、その他の文字列への代入などを含む雑多なメソッドを通して使うことができます。</p>
-
-<p><code>GetValue()</code> では、最初の引数である <code>aKey</code> は、生の 8 ビット 値の連続として扱われます。 <code>aKey</code> 内の ASCII でないどの文字列も、XPConnect 境界を越えた場合も内容が保証されます。 <code>GetValue()</code> のインプリメンテーションは UTF-8 エンコードされた 8 ビット文字列の aResult への代入となっています。 もし、このメソッドがスクリプトからの呼び出しなどによって XPConnect の境界を越えて呼ばれたとき、結果は UTF-8 から UCS2へデコードされ、Unicode 値として利用されます。</p>
-
-<h3 id=".E6.96.87.E5.AD.97.E5.88.97.E5.9E.8B.E3.81.AE.E9.81.B8.E6.8A.9E" name=".E6.96.87.E5.AD.97.E5.88.97.E5.9E.8B.E3.81.AE.E9.81.B8.E6.8A.9E">文字列型の選択</h3>
-
-<p>IDL で使う正しい文字列型を決めるのは難しいかもしれません。 以下の点は適切な文字列型を決める助けになるでしょう。</p>
-
-<ul>
- <li>文字列クラスを用いることは、<code>out</code> 引数へ新規にメモリ割当をすることを防ぐでしょう。 例えば、もし呼び出し側が <code>nsAutoString</code> を <code>out</code> 引数のための値を受け取るのに使っている場合、(C++ 内で単純に <code>nsAString</code> として定義されている)短い(64 文字以下の)値の <code>out</code> 引数への代入は <code>nsAutoString</code> のバッファへの値のコピーに過ぎません。 それ以上に、文字列クラスを使うことで、文字列バッファを共有することができます。 多くの場合、ある文字列オブジェクトから別の文字列オブジェクトへ代入することにより、参照のカウントを単に増やすことを優先してコピーを避けることが出来ます。</li>
- <li>文字列クラスを使った <code>in</code> 文字列は、しばしば長さを事前に計算します。これはパフォーマンス上のメリットとなるでしょう。</li>
- <li>生の文字バッファが必要とされる場所では、<code>string</code> と <code>wstring</code> は <code>PromiseFlatString</code> よりも高速なアクセスを提供します。</li>
- <li><code>AUTF8String</code> で定義された UTF-8 文字列は、XPConnect 境界を越えるとき、デコードされる必要があるでしょう。 これはパフォーマンスに打撃を与えます。一方で、 UTF-8 文字列は共通で用いられる ASCII 文字列では省スペースしか占有しません。</li>
- <li><code>wstring</code> や <code>AString</code> で定義された Unicode 文字列は、Unicode 値が必要とされるときは、速いです。 しかし、もし値によりしばしば ASCII が使われるなら、下敷きとなった文字列の格納スペースの半分は無駄になります。</li>
-</ul>
-
-<h2 id=".E4.BB.98.E9.8C.B2_A_.E3.81.A9.E3.81.AE.E3.82.AF.E3.83.A9.E3.82.B9.E3.82.92.E3.81.84.E3.81.A4.E4.BD.BF.E3.81.86.E3.81.8B" name=".E4.BB.98.E9.8C.B2_A:_.E3.81.A9.E3.81.AE.E3.82.AF.E3.83.A9.E3.82.B9.E3.82.92.E3.81.84.E3.81.A4.E4.BD.BF.E3.81.86.E3.81.8B">付録 A: どのクラスをいつ使うか</h2>
-
-<p>この表はどのクラスをいつ使うべきかを示すクィックリファレンスです。</p>
-
-<table class="alternate data">
- <tbody>
-  <tr>
-   <th>内容</th>
-   <th>クラス</th>
-   <th>メモ</th>
-  </tr>
-  <tr class="even">
-   <td>ローカル変数</td>
-   <td><code>nsAutoString</code><code>nsCAutoString</code></td>
-   <td> </td>
-  </tr>
-  <tr class="odd">
-   <td>クラスのメンバ変数</td>
-   <td><code>nsString</code><code>nsCString</code></td>
-   <td> </td>
-  </tr>
-  <tr class="even">
-   <td>メソッドの引数の型</td>
-   <td><code>nsAString</code><code>nsACString</code></td>
-   <td>引数に抽象クラスを使う。入力引数には <code>const nsAString&amp;</code> を使い、出力引数には <code>nsAString&amp;</code> を使う。</td>
-  </tr>
-  <tr class="odd">
-   <td>出力文字列を回収するRetrieving "out" string/wstrings</td>
-   <td><code>nsXPIDLString</code><code>nsXPIDLCString</code></td>
-   <td><code>getter_Copies()</code> を使う。<code>nsString</code> / <code>nsCString</code> と似ている。</td>
-  </tr>
-  <tr class="even">
-   <td>文字バッファをラップするbuffers</td>
-   <td><code>nsDependentString</code><code>nsDependentCString</code></td>
-   <td><code>const char*</code> / <code>const PRUnichar*</code> バッファをラップする。</td>
-  </tr>
-  <tr class="odd">
-   <td>リテラル文字列</td>
-   <td><code>NS_LITERAL_STRING</code><code>NS_LITERAL_CSTRING</code></td>
-   <td>nsDependent[C]String と似ているが、ビルド時に長さが事前計算される。</td>
-  </tr>
- </tbody>
-</table>
-
-<h2 id=".E4.BB.98.E9.8C.B2_B_nsAString_.E3.83.AA.E3.83.95.E3.82.A1.E3.83.AC.E3.83.B3.E3.82.B9.3D" name=".E4.BB.98.E9.8C.B2_B:_nsAString_.E3.83.AA.E3.83.95.E3.82.A1.E3.83.AC.E3.83.B3.E3.82.B9.3D">付録 B: nsAString リファレンス=</h2>
-
-<p>読み込み専用メソッド</p>
-
-<ul>
- <li><strong>Length()</strong></li>
- <li><strong>IsEmpty()</strong></li>
- <li><strong>IsVoid()</strong></li>
- <li><strong>BeginReading(<em>iterator</em>)</strong></li>
- <li><strong>EndReading(<em>iterator</em>)</strong></li>
- <li><strong>Equals(<em>string</em>[,<em>comparator</em>])</strong></li>
- <li><strong>First()</strong></li>
- <li><strong>Last()</strong></li>
- <li><strong>CountChar()</strong></li>
- <li><strong>Left(<em>outstring</em>, <em>length</em>)</strong></li>
- <li><strong>Mid(<em>outstring</em>, <em>position</em>, <em>length</em>)</strong></li>
- <li><strong>Right(<em>outstring</em>, <em>length</em>)</strong></li>
- <li><strong>FindChar(<em>character</em>)</strong></li>
-</ul>
-
-<p>文字列を修正するメソッド</p>
-
-<ul>
- <li><strong>Assign(<em>string</em>)</strong></li>
- <li><strong>Append(<em>string</em>)</strong></li>
- <li><strong>Insert(<em>string</em>)</strong></li>
- <li><strong>Cut(<em>start</em>, <em>length</em>)</strong></li>
- <li><strong>Replace(<em>start</em>, <em>length</em>, <em>string</em>)</strong></li>
- <li><strong>Truncate(<em>length</em>)</strong></li>
- <li><strong>SetIsVoid(<em>state</em>)</strong></li>
- <li><strong>BeginWriting(<em>iterator</em>)</strong></li>
- <li><strong>EndWriting(<em>iterator</em>)</strong></li>
- <li><strong>SetCapacity()</strong></li>
-</ul>

+ 0 - 826
files/ja/mozilla/tech/xpcom/guide/mozilla_internal_string_guide/raw.html

@@ -1,826 +0,0 @@
-<h2 id=".E5.BA.8F.E6.96.87" name=".E5.BA.8F.E6.96.87">序文</h2>
-
-<div>
-<p>by Alec Flett<br>
- Thanks to David Baron for <a class="external" href="http://dbaron.org/mozilla/coding-practices">actual docs</a>,<br>
- Peter Annema for lots of direction,<br>
- Myk Melez for some more docs, and<br>
- David Bradley for a diagram<br>
- Revised by Darin Fisher for Mozilla 1.7<br>
- Revised by Jungshik Shin to clarify character encoding issues</p>
-</div>
-
-<p> このガイドは過剰に存在しているstringクラスについてドキュメント化したものです。これによって「こういう場合、いったいどのstringクラスを使ったらいいの?」という長年の疑問に対する答えとなることを期待しています。</p>
-
-<div style="border: thin solid steelblue; padding: 0.5em;">
-<p> もしあなたがMozillaの組み込み開発者か、もしくはMozillaコードベースとは別個に配布されることを予定しているXPCOMコンポーネントを書いているなら、この文書は多くの場合あなたにとって最適のものとは言えません!もしあなたがMozilla 1.7以降を対象に開発を行っているなら、この文書の代わりに新しい最小版APIである <code><a href="https://dxr.mozilla.org/mozilla-central/source/xpcom/glue/nsStringAPI.h" rel="custom">xpcom/glue/nsStringAPI.h</a></code> とりわけ<code><a href="https://dxr.mozilla.org/mozilla-central/source/xpcom/string/public/nsEmbedString.h" rel="custom">xpcom/string/public/nsEmbedString.h</a></code> クラスを使うべきです。</p>
-</div>
-
-<p> お急ぎですか?それなら <a href="/ja/XPCOM/String_Quick_Reference" title="ja/XPCOM/String_Quick_Reference">String Quick-Reference</a> () を見てください。</p>
-
-<h2 id=".E3.81.AF.E3.81.98.E3.82.81.E3.81.AB" name=".E3.81.AF.E3.81.98.E3.82.81.E3.81.AB">はじめに</h2>
-
-<p>string クラスは、unicode と 1バイト文字の文字列のバッファを扱うために使われる C++ クラスのライブラリです。 これらは <code><a href="https://dxr.mozilla.org/mozilla-central/source/xpcom/string" rel="custom">xpcom/string</a></code> ディレクトリの Mozilla コードベースに属します。</p>
-
-<p>Abstract (interface) クラスは"nsA"で始まり、 具象クラスは"<code>ns</code>"で始まります。 "<code><strong>C</strong>String</code>" を名前に持つクラスは シングルバイト ASCII 文字列もしくは、UTF-8 やその他の文字セットでエンコードされたマルチバイト unicode 文字列を参照する 8 ビット文字を格納します。 名前に単に "<code>String</code>" を持つ他のすべてのクラスは、 主に UTF16 でエンコードされる 2バイト(<code>PRUnichar</code>)文字列を参照します。 例:<code>nsAString</code> は 2バイト文字を蓄えるための抽象クラスで、 <code>nsDependentCString</code> は 1バイト文字を蓄えるための具象クラスです。 どの 2バイト文字列クラスも同等の 1バイト文字列クラスを持ちます。 例:<code>nsCString</code> クラスは 1バイト文字列クラスで、 <code>nsString</code> と対応します。</p>
-
-<p>1バイトと 2バイトの文字列クラスは完全に異なる基底クラスを持ちますが、 同じ API を共有します。 このように、1バイト文字列を 2バイト文字列へ、何らかのヘルパークラスかルーチンによる変換なしに代入することは出来ません。 このドキュメントの目的は、クラスドキュメントの中で 2バイト文字列クラスについて言及することです。 どの 2バイトクラスも同等の 1バイトクラスを持つと仮定しても安全です。</p>
-
-<h2 id="String_ガイドライン">String ガイドライン</h2>
-
-<p>仲間の開発者、レビューワ、ユーザをハッピーにさせるために、コードの中でこれらの単純なルールに従ってください。</p>
-
-<ul>
- <li>是非とも<a href="#Unicode_Conversion"><code><span class="nowiki">*WithConversion</span></code> 関数を避ける</a>: <code>AssignWithConversion</code>, <code>AppendWithConversion</code>, <code>EqualsWithConversion</code>, など</li>
- <li>出来る限りもっとも抽象的なクラスを使う。大抵はこれ:
-  <ul>
-   <li><a href="#Abstract_Classes"><code>nsAString</code></a> 関数の引数のために</li>
-   <li><a href="#Concrete_Classes"><code>nsString</code> メンバ変数のために </a></li>
-   <li><a href="#Concrete_Classes"><code>nsAutoString</code> または <code>nsXPIDLString</code></a> ローカル(スタックベース)変数のために</li>
-  </ul>
- </li>
- <li><code>nsAString</code> 互換オブジェクトとしてリテラル文字列(例:<code>"foo"</code>)を表現するためには <a href="#Literal_Strings"><code>NS_LITERAL_[C</code></a>STRING/<code>NS_NAMED_LITERAL_[C]STRING</code>] を使う。</li>
- <li>文字列を結合するときには<a href="#Concatenation">string 連結</a>(例: "<code>+</code>" オペレータ)を使う。</li>
- <li><code>nsAString</code>互換文字列を変換する必要のある生の文字ポインタを持つとき、 <a href="#Raw_Pointers"><code>nsDependentString</code></a>を使う。</li>
- <li>既存の文字列から一部を抜き出すためには <a href="#Substring"><code>Substring()</code></a> を使う。</li>
- <li>文字列断片の解析と抜き出しには <a href="#Iterators">iterators</a> を使う。</li>
-</ul>
-
-<p> </p>
-
-<h2 id="Abstract.EF.BC.88.E6.8A.BD.E8.B1.A1.EF.BC.89.E3.82.AF.E3.83.A9.E3.82.B9" name="Abstract.EF.BC.88.E6.8A.BD.E8.B1.A1.EF.BC.89.E3.82.AF.E3.83.A9.E3.82.B9">Abstract(抽象)クラス</h2>
-
-<p>どの string クラスも <code>nsAString</code>(もしくは <code>nsACString</code>) から派生しています。 このクラスはアクセスと文字列操作のための基礎インタフェースを提供します。 具象クラスが <code>nsAString</code>から派生する一方、 <code>nsAString</code>自身はインスタンス化できません。</p>
-
-<p>これは、コードベースの他の部分で抽象オブジェクト記述の記述のために Mozilla が使っている "interface" の考え方によく似ています。 インタフェースについて、クラス名は "nsI" で始まり、"I" は "Interface" を意味します。 抽象クラスは "nsA" で始まり、"A" は "Abstract" を意味します。</p>
-
-<p><code>nsAString</code> から派生した抽象クラスがたくさんあります。 これらの抽象サブクラスもまたインスタンス化できません。 しかし、それらは <code>nsAString</code> よりもわずかながらより詳細に string を記述します。 それからは、抽象クラスの背後で下敷きになった実装が <code>nsAString</code> に加えて特定の能力を提供することを保証します。</p>
-
-<p>以下のリストで主な抽象クラスについて記述します。一度それらになじんだら、<a href="#Appendix_WhatToUse">どのクラスをいつ使うか</a>を参照してください。</p>
-
-<ul>
- <li><code class="decl">nsAString</code><span class="nowiki">: すべての文字列のための基底クラスです。 これは、代入/個々の文字へのアクセス/基本的なの文字操作/文字列比較のための API を提供します。このクラスは XPIDL の </span><code>AString</code> 引数型に対応します。</li>
- <li><code class="decl">nsSubstring</code><span class="nowiki">: string クラスのすべてに対する共通の基本クラスです。文字列の内部のデータへ最適化されたアクセスをするためのものです。</span><code>nsSubstring</code> は null 終端文字列である必要はありません(後方互換のために、<code>nsASingleFragmentString</code> は、このクラスを示す typedef がなされています)。</li>
- <li><code class="decl">nsString</code><span class="nowiki">: null 終端の保存を保証した </span><code>nsSubstring</code> によって作られます。このクラスでは、下敷きとなっている文字バッファにアクセスするためのメソッド(<code>.get()</code>) を使うことができます。(後方互換のために、<code>nsAFlatString</code> は、このクラスを示す typedef がなされています)。</li>
-</ul>
-
-<p>その他の string クラスは、<code>nsSubstring</code> か <code>nsString</code> かを継承しています。 そのため、どの文字列クラスも <code>nsAString</code> と互換があります。</p>
-
-<p><code>nsSubstring</code> と <code>nsAString</code> は共に null 終端である必要のない一続きの文字の配列を示していることに言及するのは重要なことでしょう。 この似通った二つのクラスが存在する必要があるのは何故なのかと疑問に思う人もいるでしょう。 えぇ、<code>nsSubstring</code> は主として、最適化目的に存在します。それは、<code>nsAString</code> が Mozilla 1.0 とともにリリースされた凍結された <code>nsAString</code> crustバイナリレベルの互換を保たなくてはならないからです。 Mozilla 1.7 のリリースまで、<code>nsAString</code> は複合的な断片に分けられた文字列を表す能力を持っていました。 複合的な断片に分けられた文字列のサポートに関するコストは高く、限られたメリットを提供していました。 文字列クラスの複雑さを減らして、パフォーマンスを改善しようという努力のために、複合的な断片に分けられた文字列のサポートを削減する決定がなされました。詳細は <a class="external" href="http://bugzilla.mozilla.org/show_bug.cgi?id=231995">bug 231995</a> を参照して下さい。</p>
-
-<p><code>nsSubstring</code> は、下敷きとしている <code>nsAString</code> バッファへのより効果的なインタフェースを提供していますが、<code>nsAString</code> は未だにパラメータ渡しのためにもっとも共通して使われています。 それは、XPIDL の <code>AString</code> と対応した文字辣クラスだからです。 そのために、この文字列ガイドは <code>nsAString</code> に重点を置いた文字列クラスについて言及し続けるでしょう。</p>
-
-<p>どの stiring も <code>nsAString</code>(もしくは <code>nsACString</code>) から派生しているため、それらはいくつかの基礎となる機能を共有します。</p>
-
-<p>読取専用の共通メソッド:</p>
-
-<ul>
- <li><code class="decl">.Length()</code> - string 中の文字数。</li>
- <li><code class="decl">.IsEmpty()</code> - string が何らかの値を持っているかどうか決定する最も早い方法。次のコードでテストするよりこれを使ってください:<code><em>string</em>.Length == 0</code></li>
- <li><code class="decl">.Equals(<em>string</em>)</code> - もし引数の string が現在の string と同じ値を持っていれば TRUE。</li>
-</ul>
-
-<p>文字列修正の共通メソッド:</p>
-
-<ul>
- <li><code class="decl">.Assign(<em>string</em>)</code> - 新しい値を string に代入する。</li>
- <li><code class="decl">.Append(<em>string</em>)</code> - string に値を追加する。</li>
- <li><code class="decl">.Insert(<em>string</em>, <em>position</em>)</code> - 引数の string を <em>position</em> の文字の後ろに挿入する。</li>
- <li><code class="decl">.Truncate(<em>length</em>)</code> - string を引数の長さに縮める。</li>
-</ul>
-
-<p><a href="#Appendix_nsAString">付録</a>に完全なドキュメントがあります。</p>
-
-<p> </p>
-
-<h3 id=".E8.AA.AD.E5.8F.96.E5.B0.82.E7.94.A8_strings" name=".E8.AA.AD.E5.8F.96.E5.B0.82.E7.94.A8_strings">読取専用 strings</h3>
-
-<p>string 上での<code>const</code> 属性は string が書き込み可能かどうかを示します。 もし、string が<code>const nsAString</code> のように定義されていたら、string 内のデータは操作不可です。 もし、<code>const</code> でないメソッドを <code>const</code> string 上で呼び出そうとしても、 コンパイラはビルド時にエラーとしてこれを示します。</p>
-
-<p>例:</p>
-
-<p> </p>
-
-<pre class="eval">void nsFoo::ReverseCharacters(nsAString&amp; str) {
-      ...
-     str.Assign(reversedStr); // modifies the string
-}
-</pre>
-
-<p>これはコンパイルできない。なぜなら、<code>const</code> クラスへ代入しているから:</p>
-
-<pre class="eval">void nsFoo::ReverseCharacters(const nsAString&amp; str) {
-      ...
-     <strong>str.Assign(reversedStr);</strong>
-}
-</pre>
-
-<p> </p>
-
-<h3 id=".E9.96.A2.E6.95.B0.E3.81.AE.E5.BC.95.E6.95.B0.E3.81.A8.E3.81.97.E3.81.A6.E3.81.AE_Abstract.EF.BC.88.E6.8A.BD.E8.B1.A1.EF.BC.89.E3.82.AF.E3.83.A9.E3.82.B9" name=".E9.96.A2.E6.95.B0.E3.81.AE.E5.BC.95.E6.95.B0.E3.81.A8.E3.81.97.E3.81.A6.E3.81.AE_Abstract.EF.BC.88.E6.8A.BD.E8.B1.A1.EF.BC.89.E3.82.AF.E3.83.A9.E3.82.B9">関数の引数としての Abstract(抽象)クラス</h3>
-
-<p>関数の引数としては、具象クラスの代わりにできる限りもっとも抽象的なインタフェースを使うことが推奨されます。 文字列を参照渡しする場合には、ポインタ('*'文字)ではなく、C++の参照('&amp;' 文字)を用いるのが習慣です。たとえば:</p>
-
-<pre class="eval">// 抽象クラスによる参照
-nsFoo::PrintString(<strong>const nsAString&amp;</strong> str) {..}
-
-// 具象クラスを使っている!
-nsFoo::PrintString(const <strong>nsString&amp;</strong> str) {..}
-
-// ポインタを使っている!
-nsFoo::PrintString(const <strong>nsAString*</strong> str) {..}
-</pre>
-
-<p>抽象クラスはまた、ときどきオブジェクトへの一時的な参照を蓄えるのにも使われます。 これらの両方の使い方は、後述の<a href="#Common_Patterns">共通パターン</a>を参照してください。</p>
-
-<p> </p>
-
-<h2 id=".E5.85.B7.E8.B1.A1.E3.82.AF.E3.83.A9.E3.82.B9" name=".E5.85.B7.E8.B1.A1.E3.82.AF.E3.83.A9.E3.82.B9">具象クラス</h2>
-
-<p>具象クラスは string データを実際に蓄える必要があるコードの中で使うためのものです。 具象クラスのもっともありがちな使い方は、ローカル変数もしくはクラスや構造体のメンバとして使うというものです。 抽象クラスはそれぞれデータの格納方式が違うために、大抵は具象クラスの格納方針も異なります。</p>
-
-<p>以下はに、もっとも一般的な具象クラスの一覧を示します。一度それらになじんだら、<a href="#Appendix_WhatToUse">どのクラスをいつ使うか</a>を参照してください。 The following is a list of the most common concrete classes. Once you are familiar with them, see the appendix describing <a href="#Appendix_WhatToUse">What Class to Use When</a>.</p>
-
-<ul>
- <li><code class="decl">nsString / nsCString</code> - バッファがヒープ上に割り当てられている null 終端 string です。 string オブジェクトがなくなったときにそのバッファは破棄されます。</li>
- <li><code class="decl">nsAutoString / nsCAutoString</code> - nsString から派生した、 string 自身と同じ格納スペースに 64文字バッファを持つ string。 もし、文字長が 64より長い string が nsAutoString に代入されると、新しいバッファがヒープ上に割り当てられます。 これはメンバ変数としてはあまり用いられるべきではないでしょう。</li>
- <li><code class="decl">nsXPIDLString / nsXPIDLCString</code>- nsString から派生した文字列で、 このクラスは <code>getter_Copies()</code> 演算子によって、XPIDL の<code>出力 wstring / string</code> パラメータへの簡単なアクセスをサポートしています。</li>
- <li><code class="decl">nsDependentString</code>- nsString から派生した文字列で、 この string は自分自身ではバッファを<strong>持ちません</strong>。 生の文字列(<code>const PRUnichar*</code> や <code>const char*</code>)を <code>nsAString</code> 型に変換するのに有用です。</li>
- <li><code class="decl">nsPrintfCString</code>- nsString から派生した文字列で、 この string は nsCAutoString のように振る舞います。 このクラスのコンストラクタで <code>printf</code> スタイルのフォーマットの string と引数リストから シングルバイトの string を作ることが出来ます。</li>
- <li><code class="decl">NS_LITERAL_STRING/NS_NAMED_LITERAL_STRING</code>- これらは("abc" のような)リテラル stringを <code>nsAStrings</code> や nsString のサブクラスに変換します。 プラットフォームでサポートしているダブルバイト string リテラルにおいて(例:MSVC++ や -fshort-wchar オプションを付けた GCC)、これらは <code>nsDependentString</code> クラス周辺の単純なマクロです。 これらは <code>nsDependentString</code> による単なるラップより若干速いです。なぜなら、それらの長さの計算にコンパイラを使い、ダブルバイトリテラル string のごちゃごちゃしたクロスプラットフォーム上の細部を隠しもするからです。</li>
-</ul>
-
-<p>ヘルパールーチンなどの副産物として作られた具象クラスはたくさんあります。 これらのクラスは<em>直接使うことを避け</em>るべきです。 string ライブラリから自分のためのクラスを作ってください。</p>
-
-<ul>
- <li><code class="decl">nsSubstringTuple</code> - <a href="#Concatenation">文字列連結</a>を通した生成</li>
- <li><code class="decl">nsDependentSubstring</code> - <a href="#Substring">Substring</a> を通した生成</li>
- <li><code class="decl">nsPromiseFlatString</code> - <a href="#Raw_Pointers"><code>PromiseFlatString()</code></a> を通した生成</li>
-</ul>
-
-<p>もちろん、あなたのコードの中のこれらの string クラスの参照が必要な時はあります。 しかし、一般的なルールでは、これらは使わない方がよいです。</p>
-
-<h2 id=".E3.82.A4.E3.83.86.E3.83.AC.E3.83.BC.E3.82.BF" name=".E3.82.A4.E3.83.86.E3.83.AC.E3.83.BC.E3.82.BF">イテレータ</h2>
-
-<p>イテレータは string の中の位置への参照を保つオブジェクトです。 ある意味で、これらは配列の中のインデックスを参照する数値や、 文字列の中の位置を参照する文字ポインタのようなものです。 イテレータは、文字列への読み込みと書き込みとを区別する文法的な意味も提供します。</p>
-
-<p>イテレータは文字列の部分文字列の抽出のためにもっともよく用いられるべきです。 これらは文字列の内容の修正のための機能を提供しますが、 たいていはヘルパールーチン、つまり文字列自身のメソッドの方が、複雑な文字列変換より早いでしょう。</p>
-
-<p>イテレータは繰り返している文字列クラスで宣言されます:</p>
-
-<pre class="eval">nsAString::const_iterator start, end; // 読み取り専用イテレータ
-    nsAStrings
-    nsAFlatString::iterator substr_start, substr_end; // nsString のための書き込み用イテレータ
-</pre>
-
-<p>イテレータは string にある4つのメソッドのうちあなたが参照したい一つにより初期化されます:</p>
-
-<pre class="eval">// 'str' から読み込もう
-str.BeginReading(start); // 'str' の先頭で 'start' を初期化する
-str.EndReading(end); // 'end' は string の終端になるでしょう
-
-// 'url' に書き込みもしたいんです
-url.BeginWriting(substr_start);
-url.EndWriting(substr_end);
-</pre>
-
-<p>ポインタ参照オペレータ * によってイテレータが指す文字へアクセス可能です。</p>
-
-<pre class="eval">if (*start == '[')
-     printf("Starts with a bracket\n");
-</pre>
-
-<p>上記例では、'end' と 'substr_end' は実際にかつて string の終端だった文字を指すでしょう。 なので、.EndReading() の結果を直接ポインタ内容参照してはいけないことに注意してください。</p>
-
-<p>二つのイテレータが同じ位置を指すかどうかは、== か != で調べることが出来ます。 ++ でイテレータの参照を進めることも出来ます。 ++ はイテレータの前にもってくることが好まれます。そして、それは一時的なイテレータの作成を防ぐことになります。</p>
-
-<p> </p>
-
-<pre class="eval">while (start != end) // string 全体を通して順番に回る
-     ++start;
-</pre>
-
-<p>(const-iterators とは反対のように)書き込みイテレータにより、効果的に string に書き込むことが出来ます:</p>
-
-<p> </p>
-
-<pre class="eval">// * をすべて ! に変える
-while (substr_start != substr_end) {
-     if (*substr_start == '*')
-          *substr_start = '!';
-     ++substr_start;
-}
-</pre>
-
-<p>With the patch for <a class="external" href="http://bugzilla.mozilla.org/show_bug.cgi?id=231995">bug 231995</a>, this loop is now as efficient as iterating with raw character pointers.</p>
-
-<p> </p>
-
-<h3 id=".E3.82.A4.E3.83.86.E3.83.AC.E3.83.BC.E3.82.BF.E3.81.AB.E3.82.88.E3.82.8B.E3.83.AB.E3.83.BC.E3.83.97" name=".E3.82.A4.E3.83.86.E3.83.AC.E3.83.BC.E3.82.BF.E3.81.AB.E3.82.88.E3.82.8B.E3.83.AB.E3.83.BC.E3.83.97">イテレータによるループ</h3>
-
-<p><span id="comment">原文ではindex部分には残っていますが、見出し、a nameを含めて削除されています。</span></p>
-
-<h2 id=".E3.83.98.E3.83.AB.E3.83.91.E3.83.BC.E3.82.AF.E3.83.A9.E3.82.B9.E3.81.A8.E9.96.A2.E6.95.B0" name=".E3.83.98.E3.83.AB.E3.83.91.E3.83.BC.E3.82.AF.E3.83.A9.E3.82.B9.E3.81.A8.E9.96.A2.E6.95.B0">ヘルパークラスと関数</h2>
-
-<h3 id=".E6.96.87.E5.AD.97.E5.88.97.E6.A4.9C.E7.B4.A2" name=".E6.96.87.E5.AD.97.E5.88.97.E6.A4.9C.E7.B4.A2">文字列検索</h3>
-
-<p><code>FindInReadable()</code> はかつての <code>string.Find(..)</code> の代わりのものです。 構文は:</p>
-
-<pre class="eval">PRBool FindInReadable(const nsAString&amp; pattern,
-                      nsAString::const_iterator start, nsAString::const_iterator end,
-                      nsStringComparator&amp; aComparator = nsDefaultStringComparator());
-</pre>
-
-<p>これを使うために、<code>start</code> と <code>end</code> は検索したい文字列のそれぞれ先頭と終端をさしていなくてはなりません。 もし、探している文字列が見つかったら、<code>start</code> と <code>end</code> は見つかった部分の先頭と終端を指すように調整されます。 戻り値は、PR_TRUE か PR_FALSE で、文字列が見つかったかどうかを示します。</p>
-
-<p>例:</p>
-
-<pre class="eval">const nsAString&amp; str = GetSomeString();
-nsAString::const_iterator start, end;
-
-str.BeginReading(start);
-str.EndReading(end);
-
-NS_NAMED_LITERAL_STRING(valuePrefix, "value=");
-
-if (FindInReadable(valuePrefix, start, end)) {
-    // end は今、検索した文字の後ろを指している
-    valueStart = end;
-}
-</pre>
-
-<h3 id=".E3.83.A1.E3.83.A2.E3.83.AA.E5.89.B2.E5.BD.93" name=".E3.83.A1.E3.83.A2.E3.83.AA.E5.89.B2.E5.BD.93">メモリ割当</h3>
-
-<p>既存の文字列から新しい文字列バッファ(<code>PRUnichar*</code>/<code>char*</code>)を割り当てるために好ましいメソッドは、 以下のメソッドです:</p>
-
-<ul>
- <li><strong><code>PRUnichar* ToNewUnicode(<em>nsAString&amp;</em>)</code></strong> - nsAString から <code>PRUnichar*</code> を割り当てます。</li>
- <li><strong><code>char *ToNewCString(<em>nsACString&amp;</em>)</code></strong> - nsACString から <code>char*</code> バッファを割り当てます。 このメソッドは nsAStrings 上でも働きますが、暗黙の<a href="#Lossy_Conversions">損失の多い変換</a>となるでしょう。 この機能は入力が厳密に ASCII であることが判っている場合にだけ使うべきです。しばしば UTF8 への変換がより適しています。 次項 <code class="decl">ToNewUTF8String</code> も参照のこと。</li>
- <li><strong><code>char* ToNewUTF8String(<em>nsAString&amp;</em>)</code></strong> - 与えられた nsAString の UTF8 エンコードされたバージョンを含む新しい <code>char*</code> バッファを割り当てます。 詳細は<a href="#Unicode_Conversion">Unicode 変換</a>を参照して下さい。</li>
-</ul>
-
-<p>これらのメソッドは These methods return a buffer allocated using XPCOM's allocator (<code>nsMemory::Alloc</code>) instead of the traditional allocator (<code>malloc</code>, etc.). You should use <code>nsMemory::Free</code> to deallocate the result when you no longer need it. これらのメソッドは伝統的なアロケータ (<code>malloc</code> など)の代わりに XPCOM のアロケータ (<code>nsMemory::Alloc</code>)で割り当てられたバッファを返すでしょう。 必要としなくなった時、その戻り値を開放するために <code>nsMemory::Free</code> を使うべきです。</p>
-
-<h3 id=".E6.97.A2.E5.AD.98.E3.81.AE.E6.96.87.E5.AD.97.E5.88.97.E3.81.AE.E6.96.AD.E7.89.87" name=".E6.97.A2.E5.AD.98.E3.81.AE.E6.96.87.E5.AD.97.E5.88.97.E3.81.AE.E6.96.AD.E7.89.87">既存の文字列の断片</h3>
-
-<p>実際に新しいスペースを割り当てたり、その文字列の部分文字列の文字をコピーしたりしないで既存の文字列の部分文字列を参照するのはとても簡単です。<code>Substring()</code> はそのような文字列への参照を生成するのにとてもよいメソッドです。</p>
-
-<pre class="eval">void ProcessString(const nsAString&amp; str) {
-    const nsAString&amp; firstFive = Substring(str, 0, 5);
-    // firstFive は今最初の 5 文字を示す文字列です
-}
-</pre>
-
-<p> </p>
-
-<h2 id="Unicode_.E5.A4.89.E6.8F.9B" name="Unicode_.E5.A4.89.E6.8F.9B">Unicode 変換</h2>
-
-<p>文字列は二つの基本的な形式で<em>保存</em>することができます。 8 ビット文字(<code>char</code>)文字列もしくは、16 ビット文字(<code>PRUnichar</code>)文字列です。 クラス名に大文字の「C」を持つどの文字列クラスも、8 ビット文字を含みます。 それには、nsCString、nsDependentCString などのクラスが含まれます。 「C」を<em>持たない</em>どの文字列クラスも、16 ビット文字を含みます。</p>
-
-<p>格納構造に加え、文字列はまた、<em>エンコード</em>もされているという側面があります。 文字列のエンコーディングは、8 もしくは 16 ビットの文字列として unicode 文字値の組を保持することを意味します。 大きな文字値を小さな容量の文字列として保存する方法がたくさんあるため、エンコーディングがたくさんあります。 <em>文字セット</em>は、特定のエンコーディング方法の人間が理解できる名称です。 例えば、「ASCII」は 7 ビット値を 8 ビット文字列にマッピングした文字セットです。 「isolatin1」は8 ビット文字列で西欧文字エンコーディングするための共通文字セットです。</p>
-
-<p>文字列の文字セットは、文字列クラスの名前によって定義されて<em>いません</em>。 そのかわり、文字列の適切なエンコーディングを決定することは実装者の責任です。 もっとも一般的なのエンコーディングは:</p>
-
-<ul>
- <li><strong>ASCII</strong> - 基本的な英語のみの文字列のための 8 ビットエンコーディングです。 どの ASCII 値も配列の正確に 1 バイトに格納されます。</li>
- <li><strong>UCS2</strong> - 基本の unicode 格納のためのエンコーディングです。 UCS2 で格納された文字の unicode 値は、文字列クラスの正確に一つの 16 ビット <code>PRUnichar</code> に格納されます。</li>
- <li><strong>UTF8</strong> - unicode 値のための8 ビットエンコーディング。 どの UTF8 値も 1 から 6 バイトの組で保持されます。 UTF8 は unicode 文字セット全体を表現する能力があり、効率よく UTF32 へマップします。</li>
- <li><strong>UTF16</strong> - 拡張 unicode 格納のための 16 ビットエンコーディングです。 UCS2 に対して後方互換性があります。 UTF16で格納された unicode 文字値は、文字列クラスの 1 つもしくは 2 つの 16ビット <code>PRUnichar</code> を必要とします。 このエンコーディングは現在頻繁には使われません。しかし、より新しい unicode 標準が採用されるにつれて、きっと増えるでしょう。 UTF16 は unicode 文字セット全体を表現する能力があり、効率よく UTF32 へマップします。</li>
-</ul>
-
-<p>加えて、国際化ライブラリによって提供される文字通り何百のエンコーディングがあります。 これらのライブラリへのアクセスは、アプリケーションの一部であるか(例えば Mozilla の <code>nsICharsetConversionManager</code> のように)、オペレーティングシステムへビルドされている(例えば Unix 系 OS の <code>iconv()</code> のように)でしょう。</p>
-
-<p>既存のコードで作業する時、正しい変換機構を決定するために、操作している文字列の現在の使い方を吟味することは重要です。</p>
-
-<p>新しいコードを書く時、どの格納クラスそしてエンコーディングがもっとも適切かを知ろうとしても混乱するかもしれません。この問題への単純な答えはありません。しかし、少数の重要なガイドラインがあります:</p>
-
-<ul>
- <li><strong>文字列はいつも ASCII ?</strong> 最初の、そして最大のもので、どの種類の値が文字列に格納されているかを決めることが必要です。文字列がいつでも内部の ASCII 文字列、例えば「left」、「true」、「background」などであれば、そのままの C-文字列がたぶん選ぶべきものです。</li>
- <li><strong>文字列が ASCII であれば、ASCII でない文字列と比較・代入・さもなければ相互作用しますか?</strong> 8 ビット ASCII 値を 16 ビット UCS2 文字列と代入もしくは比較するとき、実行時に「膨らませる」必要があります。 もし、文字列が十分に小さい(そう、 64 バイトより小さい)ならば、余計な変換を避けるために 16 ビット unicode クラスにも文字列を格納する、というのも手です。そのかわり、ASCII 文字列が、8 ビット文字列だった場合に比べて 2 倍のスペース、つまり 16 ビット unicode 文字列と同じスペースを占める、という欠点があります。</li>
- <li><strong>文字列はたいてい ASCII であるけれど、unicode をサポートしなくていいのでしょうか?</strong> 文字列はほとんど大抵 ASCII であるけれど、unicode 値も格納する必要があるのなら、UTF8 は正しいエンコーディングだ。 ASCII 値は 8 ビットのまま格納され、拡張 unicode 値は 2 〜 6 バイトで格納されるでしょう。 しかし、もし文字列が unicode 値との比較や代入までも必要とするのなら、実行時変換が必要となるでしょう。</li>
- <li><strong>ASCII でない大きな文字列データを格納しますか?</strong> ここに至るまでは、 UTF8 は理想的な文字列に見えます。 欠点は、西欧以外の文字のほとんど(例えば日本語文字)を使う場合に、UTF8 は「ふくれ気味な」エンコーディングです。 UTF-8 では、日本語文字列は大抵一文字あたり 3 バイト必要です。 UCS2 では、一文字が 2 バイトであるのと比べて、日本語テキストにおいて UTF-8 は相当に多くのサイズを消費します。</li>
- <li><strong>unicode 文字列のコンテントを処理する必要がありますか?</strong> UTF8 やその他の 8 ビット格納形式で unicode 値をエンコーディングすることの一つの問題点は、実際も unicode 値も文字列の中で複数バイトにわたることです。 ほとんどのエンコーディングでは、実際のバイト数は文字から文字へで異なります。 それぞれの文字を通して反復処理する必要があるときには、エンコーディングを考慮する必要があります。 UCS2 文字列で反復処理するなら、これは非常に簡易です。なぜなら、どの 16 ビット PRUnichar も、unicode 値と対応するからです。</li>
-</ul>
-
-<p>ASCII、UTF8、UCS2 の変換を助けるため、 いくつかのヘルパーメソッドとヘルパークラスがあります。 これらのクラスのうちいくつかは、スタック上の一時オブジェクトとしてもっともよく使われるため、関数のように見えます。</p>
-
-<p>To assist with ASCII, UT8 and UCS2 conversions, there are some helper methods and classes. Some of these classes look like functions, becuase they are most often used as temporary objects on the stack.</p>
-
-<h4 id="UTF8_.2F_UCS2_.E5.A4.89.E6.8F.9B" name="UTF8_.2F_UCS2_.E5.A4.89.E6.8F.9B">UTF8 / UCS2 変換</h4>
-
-<ul>
- <li><code class="decl">NS_ConvertUTF8toUCS2(<em>const nsACString&amp;</em>)</code> - UTF-8 エンコードされた nsACString もしくは <code>const char*</code> を UCS2 string に変換する nsAutoString のサブクラス。もし、代わりに <code>const PRUnichar*</code> バッファが必要なら、.get() メソッドを使ってください。例:</li>
-</ul>
-
-<pre class="eval">/* シグネチャ: void HandleUnicodeString(const nsAString&amp; str); */
-object-&gt;HandleUnicodeString(<strong>NS_ConvertUTF8toUCS2</strong>(utf8String));
-</pre>
-
-<pre class="eval">/* シグネチャ: void HandleUnicodeBuffer(const PRUnichar* str); */
-object-&gt;HandleUnicodeBuffer(<strong>NS_ConvertUTF8toUCS2</strong>(utf8String).get())
-
-</pre>
-
-<ul>
- <li><code class="decl">NS_ConvertUCS2toUTF8(<em>const nsAString&amp;</em>)</code> - UTF8 エンコードされた nsAString を UTF8 された文字列に変換する nsAFlatCString。上記同様に、<code>const char*</code> へアクセスするときは、.get() を使ってください。</li>
- <li><code class="decl">NS_ConvertUCS2toUTF8(<em>const nsAString&amp;</em>)</code> - UCS2 エンコードされた nsAString を UTF-8 エンコードされた string へ変換する nsCAutoString。 上記項目と同様に、<code>const char*</code> にアクセスするために .get() を使うことが出来ます。</li>
-</ul>
-
-<pre class="eval">/* シグネチャ: void HandleUTF8String(const nsACString&amp; str); */
-object-&gt;HandleUTF8String(NS_ConvertUCS2toUTF8(unicodeString));
-</pre>
-
-<pre class="eval">/* シグネチャ: void HandleUTF8Buffer(const char* str); */
-object-&gt;HandleUTF8Buffer(NS_ConvertUCS2toUTF8(unicodeString).get())
-</pre>
-
-<ul>
- <li><code class="decl">CopyUTF8toUCS2(<em>const nsACString&amp;, const nsAString&amp;</em>)</code> - 変換と割り当てを行う。</li>
-</ul>
-
-<pre class="eval">// UCS2 値を返す
-void Foo::GetUnicodeValue(nsAString&amp; result) {
-    CopyUTF8toUCS2(mLocalUTF8Value, result);
-}
-</pre>
-
-<ul>
- <li><code class="decl">CopyUCS2toUTF8(<em>const nsAString&amp;, const nsACString&amp;</em>)</code> - 変換と割り当てを行う。</li>
-</ul>
-
-<pre class="eval">// UTF8 値を返す
-void Foo::GetUTF8Value(nsACString&amp; result) {
-    CopyUCS2toUTF8(mLocalUnicodeValue, result);
-}
-</pre>
-
-<ul>
- <li><code class="decl">ToNewUTF8String(<em>const nsAString&amp;</em>)</code> - 割り当てと変換を行う</li>
-</ul>
-
-<pre class="eval">void Foo::GetUTF8Value(const char** result) {
-  *result = ToNewUTF8String(mLocalUnicodeValue);
-}
-</pre>
-
-<p> </p>
-
-<h3 id=".E6.90.8D.E5.A4.B1.E3.81.AE.E5.A4.9A.E3.81.84.E5.A4.89.E6.8F.9B" name=".E6.90.8D.E5.A4.B1.E3.81.AE.E5.A4.9A.E3.81.84.E5.A4.89.E6.8F.9B">損失の多い変換</h3>
-
-<p>以下はオリジナル文字列が ASCII ベースであることが保証できるときだけ使われるべきです。</p>
-
-<h4 id="UCS2_.E3.81.8B.E3.82.89_ASCII_.E3.81.B8.E3.81.AE.E3.82.B3.E3.83.B3.E3.83.90.E3.83.BC.E3.82.BF" name="UCS2_.E3.81.8B.E3.82.89_ASCII_.E3.81.B8.E3.81.AE.E3.82.B3.E3.83.B3.E3.83.90.E3.83.BC.E3.82.BF">UCS2 から ASCII へのコンバータ</h4>
-
-<p>これらのコンバータ(変換機構)は、変換プロセスの中で<em><strong>情報の消失</strong></em>があるため<em><strong>とても危険</strong></em>です。<em><strong>UCS2 から ASCII への変換は、文字列が ASCII であることが保証されない限りさけるべきです</strong></em>。どの UCS2(16 ビット)文字も、8 ビット文字は単に 8 ビット文字にキャストされます。それは、0xFF を超えるすべての文字の値は任意の 8 ビット文字に変換されてしまうということです。</p>
-
-<ul>
- <li><code class="decl">NS_LossyConvertUCS2toASCII(<em>nsAString</em>)</code> - string の圧縮した値を含む一時バッファを持つ nsCAutoString。</li>
- <li><code class="decl">CopyUCS2toASCII(<em>nsAString</em>, <em>nsACString</em>)</code> - UCS2 から ASCII 文字列オブジェクトへコピーの変換をします。</li>
- <li><code class="decl">ToNewCString(<em>nsAString</em>)</code> - 新しい <code>char*</code> 文字列を割り当てます。</li>
-</ul>
-
-<p> </p>
-
-<h4 id="ASCII_.E3.81.8B.E3.82.89_UCS2_.E3.81.B8.E3.81.AE.E3.82.B3.E3.83.B3.E3.83.90.E3.83.BC.E3.82.BF" name="ASCII_.E3.81.8B.E3.82.89_UCS2_.E3.81.B8.E3.81.AE.E3.82.B3.E3.83.B3.E3.83.90.E3.83.BC.E3.82.BF">ASCII から UCS2 へのコンバータ</h4>
-
-<p>これらは、<em>ASCII でない文字列を無意味な unicode 文字列に壊してしまう</em>ため、<em><strong>とても危険</strong></em>です。 <em><strong>ASCII から UCS2 への変換は、文字列が ASCII であることが保証されない限りさけるべきです</strong></em>。 つまり、もし複数バイト文字セットの 8 ビット文字列エンコードされたものを持っている場合、文字列のどのバイトもただキャストによって 16 バイト数値に"水増し"されるだけなのです。</p>
-
-<p>例えば、文字列の最初の unicode 文字が 4 バイトの UTF-8 シーケンスで表されているような UTF-8 文字列を想像してください。"水増しされた" unicode 文字列の最初の 4 バイトは最初の文字をあらわす 4 つの値を含んでいます。これらの値は文字列を UCS2 として扱うなら無意味です。</p>
-
-<ul>
- <li><code class="decl">NS_ConvertASCIItoUCS2(<em>nsACString</em>)</code> - string の水増しした値を含む一時バッファを持つ nsCAutoString。</li>
- <li><code class="decl">NS_ConvertASCIItoUCS2(<em>nsACString</em>)</code> - 水増しした文字列の値を含む一時バッファをもつ nsAFlatString。</li>
- <li><code class="decl">CopyASCIItoUCS2(<em>nsACString</em>, <em>nsAString</em>)</code> - ある文字列から unicode 文字列オブジェクトへコピーの変換をします。</li>
- <li><code class="decl">ToNewUnicode(<em>nsACString</em>)</code> - 水増しした値を含む新しい <code>PRUnichar*</code> 文字列を生成します。</li>
-</ul>
-
-<p> </p>
-
-<h2 id=".E5.85.B1.E9.80.9A.E3.83.91.E3.82.BF.E3.83.BC.E3.83.B3" name=".E5.85.B1.E9.80.9A.E3.83.91.E3.82.BF.E3.83.BC.E3.83.B3">共通パターン</h2>
-
-<p>多くの API は、呼び出し元に文字列を返すためのバッファを割り当てるメソッドとなります。 バッファを使い終えた時に呼び出し元が文字列を解放することを覚えている必要があるため、これはトリッキーでもあります。 幸運にも、<code>nsXPIDLString</code> クラスでこれを簡単に行えます。</p>
-
-<p><br>
- メソッドはこのような感じです:</p>
-
-<pre class="eval">void GetValue(PRUnichar** aValue)
-{
-    *aValue = ToNewUnicode(foo);
-}
-</pre>
-
-<p>文字列クラスなしで、呼び出し元は文字列を解放する必要があるでしょう:</p>
-
-<pre class="eval">{
-    PRUnichar* val;
-    GetValue(&amp;val);
-
-    if (someCondition) {
-        // 値を解放することを忘れてはいけない
-        // don't forget to free the value!
-        nsMemory::Free(val);
-        return NS_ERROR_FAILURE;
-    }
-
-    ...
-    // 後で、やはり解放を忘れてはいけない!
-    // and later, still don't forget to free!
-    nsMemory::Free(val);
-}
-</pre>
-
-<p><code>nsXPIDLString</code> を使えば、このことを心配する必要はありません。 <code>getter_Copies()</code> を使って文字列クラスをラップするだけで、 スコープの外へ出た時にクラスがバッファを解放するでしょう。</p>
-
-<pre class="eval">{
-    nsXPIDLString val;
-    GetValue(getter_Copies(val));
-</pre>
-
-<pre class="eval">    // val はここで自身によって解放されるでしょう
-    // val will free itself here
-    if (someCondition)
-        return NS_ERROR_FAILURE;
-    ...
-    // 後で、やはり解放する必要がない
-    // and later, still nothing to free
-}
-</pre>
-
-<p>結果としてのコードはとても単純で、読みやすいです。</p>
-
-<p> </p>
-
-<h3 id=".E3.83.AA.E3.83.86.E3.83.A9.E3.83.AB.E6.96.87.E5.AD.97.E5.88.97" name=".E3.83.AA.E3.83.86.E3.83.A9.E3.83.AB.E6.96.87.E5.AD.97.E5.88.97">リテラル文字列</h3>
-
-<p><em>リテラル文字列</em>は C++ コードに書かれた生の文字列の値です。 例えば、<code>printf("Hello World\n");</code> ステートメント中の値 <code>"Hello World\n"</code> はリテラル文字列です。 nsAString や nsACString が必要なとき、リテラル文字列値を挿入する必要がしばしば発生します。 これら 4 つのマクロは必要な変換のために提供されています:</p>
-
-<ul>
- <li><code class="decl">NS_LITERAL_CSTRING(<em>literal string</em>)</code> - 一時的 nsCString</li>
- <li><code class="decl">NS_NAMED_LITERAL_CSTRING(<em>variable</em>,<em>literal string</em>);</code> - <em>variable</em> と名づけられた nsCString 変数を定義します</li>
- <li><code class="decl">NS_LITERAL_STRING(<em>literal string</em>)</code> - Unicode 版<em>literal string</em>(リテラル文字列)を持つ一時 nsString</li>
- <li><code class="decl">NS_NAMED_LITERAL_STRING(<em>variable</em>,<em>literal string</em>);</code> - Unicode 版 <em>literal string</em>(リテラル文字列)を持つ、名称 <em>variable</em> の nsString 変数を宣言する</li>
-</ul>
-
-<p>nsDependentCString もまた nsCString の中の文字列の値をラップすることを考えれば、これらのマクロの <code>CSTRING</code> 版は、一見不要に見えます。これらのマクロの長所は、これらの文字列の長さがコンパイル時に計算できるため、実行時に長さを決めるために文字列を読み込む必要がありません。</p>
-
-<p>これらのマクロの <code>STRING</code> 版は、(例えば、MSVC++ や -fshort-wchar オプション付きの GCC のように)リテラル unicode 文字列をサポートするプラットフォーム上での実行時の変換をしないで、unicode 版の固定のリテラル string を宣言する移植性のある方法を提供します。</p>
-
-<pre class="eval">// Init(const PRUnichar*) 呼び出し
-Init(L"start value"); // よくない - L"..." は移植性が低い!
-Init(NS_ConvertASCIItoUCS2("start value").get()); // よくない - 実行時の ASCII-&gt;UCS2 変換!
-
-// Init(const nsAString&amp;) 呼び出し
-Init(nsDependentString(L"start value")); // よくない - 移植性が低い!
-Init(NS_ConvertASCIItoUCS2("start value")); // よくない - 実行時の ASCII-&gt;UCS2 変換!
-
-// Init(const nsACString&amp;) 呼び出し
-Init(nsDependentCString("start value")); // よくない - 文字列長が実行時に決まる
-</pre>
-
-<p>適切な NS_LITERAL_[C]STRING 使用法を以下にいくつか示します。</p>
-
-<pre class="eval">// Init(const PRUnichar*) 呼び出し
-Init(NS_LITERAL_STRING("start value").get());
-
-// Init(const nsAString&amp;) 呼び出し
-Init(NS_LITERAL_STRING("start value"));
-
-// Init(const nsACString&amp;) 呼び出し
-Init(NS_LITERAL_CSTRING("start value"));
-</pre>
-
-<p><br>
- これらのマクロを使った問題の追跡に役に立つであろう詳細をいくつか示します:</p>
-
-<p><code>NS_LITERAL_STRING</code> は(Windows や Macintosh などといった)いくつかのプラットフォーム上でコンパイル時に UCS2 への変換をしますが、他のプラットフォームでは実行時に変換されます。NS_LITERAL_STRING を使うことによって、あなたのコードで問題のプラットフォームのために最良の変換が使われることを保証します。</p>
-
-<p>いくつかのプラットフォームで実行時変換が行われるため、<code>NS_LITERAL_STRING/NS_NAMED_LITERAL_STRING</code> マクロ内部でのリテラル文字列連結の使用は、それらのプラットフォームではコンパイルされるでしょうが、コンパイル時変換をサポートするプラットフォーム上ではコンパイルできないでしょう。</p>
-
-<p>以下に例示します:</p>
-
-<p> </p>
-
-<pre class="eval">// Init(nsAString&amp;) 呼び出し
-Init(NS_LITERAL_STRING("start "
-     "value")); // いくつかのプラットフォームでだけコンパイルされます。
-</pre>
-
-<p>その理由は、いくつかのプラットフォームで<code>L"..."</code> 構文が使われますが、これは連結の最初の文字列にだけ適用されるためです(<code>"start "</code>)。コンパイラは unicode でない文字列 <code>"value"</code> との連結を試みると困惑します。</p>
-
-<h3 id=".E6.96.87.E5.AD.97.E5.88.97.E9.80.A3.E7.B5.90" name=".E6.96.87.E5.AD.97.E5.88.97.E9.80.A3.E7.B5.90">文字列連結</h3>
-
-<p>文字列は <code>+</code> 演算子を用いて互いに連結可能です。結果となる文字列は <code>const nsAString</code> オブジェクトとなります。 結果となる文字列は、その他の <code>nsAString</code> のように振る舞いをさせることも参照もできます。 連結は<em>部分文字列のコピーではありません</em>。代わりに、オリジナルの文字列を参照するに過ぎません。 結果としての文字列は、少なくとも連結された文字列と同じだけの生存期間を持つという意味で、その部分文字列のすべてに依存します。</p>
-
-<p>例えば、二つの文字列の値を使うことができ、接続を <code>const nsAString&amp;</code> をとる他の関数に引き渡すことができます:</p>
-
-<pre class="eval">void HandleTwoStrings(const nsAString&amp; one, const nsAString&amp; two) {
-    // HandleString(const nsAString&amp;) 呼び出し
-    HandleString(one + two);
-}
-</pre>
-
-<p>注意:このケースでは、二つの文字列は暗黙のうちに一時的な nsString に結びつけられ、 一時的 string は、<code>HandleString</code> に渡されます。 もし、<code>HandleString</code> がその入力を他の nsString へ代入したならば、 いくつかの文字列を連結し、その結果を一時変数に格納することもできます:</p>
-
-<pre class="eval">NS_NAMED_LITERAL_STRING(start, "start ");
-NS_NAMED_LITERAL_STRING(middle, "middle ");
-NS_NAMED_LITERAL_STRING(end, "end");
-// 3つの相互依存した断片を持つ string を生成 - 複雑なコピーではない!
-nsString combinedString = start + middle + end;
-</pre>
-
-<p> </p>
-
-<pre class="eval">// void HandleString(const nsAString&amp;); 呼び出し
-HandleString(combinedString);
-</pre>
-
-<p>もし、一度きりだけ使う一時的なものを作るために <code>NS_LITERAL_STRING</code> を使うのなら、 結合の中で定義するのが安全でしょう。なぜなら、文字列バッファは(<code>nsSubstringTuple</code> 型の)一時的結合オブジェクトと同じ生存期間となるからです。</p>
-
-<pre class="eval">// HandlePage(const nsAString&amp;); 呼び出し
-// 結合された文字列はその部分文字列と同じ生存期間なので安全
-HandlePage(NS_LITERAL_STRING("start ") + NS_LITERAL_STRING("end"));
-</pre>
-
-<h3 id=".E3.83.AD.E3.83.BC.E3.82.AB.E3.83.AB.E5.A4.89.E6.95.B0" name=".E3.83.AD.E3.83.BC.E3.82.AB.E3.83.AB.E5.A4.89.E6.95.B0">ローカル変数</h3>
-
-<p>関数内のローカル変数は通常、スタック上に確保されます。 <code>nsAutoString</code>/<code>nsCAutoString</code> クラスは、 <code>nsString</code>/<code>nsCString</code> の派生物です。 これらが、文字列自身と同じ格納領域に割り当てられた 64 文字のバッファを持ちます。 もし、nsAutoString がスタック上に割り当てられていれば、文字列は破棄時に 64 文字スタックバッファ持ちます。 They own a 64-character buffer allocated in the same storage space as the string itself. If the nsAutoString is allocated on the stack, then it has at its disposal a 64-character stack buffer. このことにより、小さな文字列を扱う時、余計なメモリの割り当てをしないで実装することができます。</p>
-
-<pre class="eval">...
-nsAutoString value;
-GetValue(value); // 結果が 64 文字より少なければ、
-                 // 割り当てを省くことができる。
-GetValue(value); // if the result is less than 64 characters,
-                 // then this just saved us an allocation
-...
-</pre>
-
-<h3 id=".E3.83.A1.E3.83.B3.E3.83.90.E5.A4.89.E6.95.B0" name=".E3.83.A1.E3.83.B3.E3.83.90.E5.A4.89.E6.95.B0">メンバ変数</h3>
-
-<p>一般に、メンバ変数としては、<code>nsString</code> や <code>nsCString</code> といった具象クラスを使うべきでしょう。</p>
-
-<pre class="eval">class Foo {
-    ...
-    // これらは UTF8 や unicode 値を各々格納する
-    // these store UTF8 and Unicode values respectively
-    nsCString mLocalName;
-    nsString mTitle;
-};
-</pre>
-
-<p>文字列は直接クラスの中で宣言され、文字列へのポインタとして宣言されるのではないことに注意してください。 このようにはしないでください:</p>
-
-<p>Note that the strings are declared directly in the class, not as pointers to strings. Don't do this:</p>
-
-<pre class="eval">class Foo {
-public:
-    Foo() { mLocalName = new nsCString(); }
-    ~Foo() { delete mLocalName; }
-
-private:
-    // これらは UTF8 や unicode 値を各々格納する
-    // these store UTF8 and Unicode values respectively
-    nsCString* mLocalName;
-};
-</pre>
-
-<p>上記のコードは文字列オブジェクトのコストを節約しているように見えるかもしれませんが、 <code>nsString</code>/<code>nsCString</code> は小さなオブジェクトです。 割り当てのオーバーヘッドは、ポインタで節約するよりも数バイト勝っているだけです。</p>
-
-<p><br>
- その他の間違ったパターンとしては、メンバ変数として、 <code>nsAutoString</code>/<code>nsCAutoString</code> を使うというものがあります。 <a href="#Local_Variables">ローカル変数</a>で述べたように、 これらのクラスはとても大きなバッファを中にもって作られます。 もしクラスの中にこれらを持つことは、クラスを 64 バイト(<code>nsCAutoString</code>)もしくは 128 バイト(<code>nsAutoString</code>)膨らませることになります。</p>
-
-<p><br>
- 例:</p>
-
-<pre class="eval">class Foo {
-    ...
-
-    // Foo クラスが 128 バイト膨らむ!
-    // bloats 'Foo' by 128 bytes!
-    nsAutoString mLocalName;
-};
-</pre>
-
-<p> </p>
-
-<h3 id=".E7.94.9F.E3.81.AE.E6.96.87.E5.AD.97.E3.83.9D.E3.82.A4.E3.83.B3.E3.82.BF" name=".E7.94.9F.E3.81.AE.E6.96.87.E5.AD.97.E3.83.9D.E3.82.A4.E3.83.B3.E3.82.BF">生の文字ポインタ</h3>
-
-<p><code>PromiseFlatString()</code> は、元になった文字列と同じ値を含む null 終端のバッファを持つ一時バッファを生成するのに使うことができます。 <code>PromiseFlatString()</code> は必要ならば一時バッファを作ります。 これは多くの場合、nsAString を null 終端文字列を要求する API に通すために使われます。</p>
-
-<p>以下の例では、nsAString はリテラル文字列と一体化されます。そして結果は単純な文字バッファを求める API に通されます。</p>
-
-<pre class="eval">// URL を修正し、AddPage(const PRUnichar* url) へ通す
-void AddModifiedPage(const nsAString&amp; url) {
-    NS_NAMED_LITERAL_STRING(httpPrefix, "<a class="external" href="http://" rel="freelink">http://</a>");
-    const nsAString&amp; modifiedURL = httpPrefix + url;
-
-    // 一時バッファ生成
-    AddPage(PromiseFlatString(modifiedURL).get());
-}
-</pre>
-
-<p>既に null 終端である文字列を扱うとき、<code>PromiseFlatString()</code> は洗練された方法です。 そのようなケースで一時バッファを作らなくてすみます。</p>
-
-<p> </p>
-
-<pre class="eval">// URL を修正し、AddPage(const PRUnichar* url) へ通す
-void AddModifiedPage(const nsAString&amp; url, PRBool addPrefix) {
-    if (addPrefix) {
-        // 一時バッファを作成<strong>なければならない</strong> - 文字列は複数の断片でできている
-        NS_NAMED_LITERAL_STRING(httpPrefix, "<a class="external" href="http://" rel="freelink">http://</a>");
-        AddPage(PromiseFlatString(httpPrefix + modifiedURL));
-    } else {
-        // 一時バッファを作成<strong>してもよい</strong>、実行時にチェックする
-        AddPage(PromiseFlatString(url).get());
-    }
-}
-</pre>
-
-<p> </p>
-
-<h2 id="IDL" name="IDL">IDL</h2>
-
-<p>文字列ライブラリは IDL を通しても利用できます。 特別に IDL 型を定義することによって属性やメソッドを宣言することで、 string クラスは対応メソッドへの引数として使えます。</p>
-
-<p> </p>
-
-<h3 id="IDL_.E6.96.87.E5.AD.97.E5.88.97.E5.9E.8B" name="IDL_.E6.96.87.E5.AD.97.E5.88.97.E5.9E.8B">IDL 文字列型</h3>
-
-<p>C++ シグネチャにより、メソッドのすべての引数が<a href="#Abstract_Classes">抽象クラス</a>をベースにしているようなのような上述通りの抽象型を定義することができます。 以下のテーブルには IDL のそれぞれの文字列型の目的を記します。</p>
-
-<table class="standard-table">
- <tbody>
-  <tr>
-   <th class="header">IDL type</th>
-   <th class="header">C++ Type</th>
-   <th class="header">Purpose</th>
-  </tr>
-  <tr>
-   <td><code>string</code></td>
-   <td><code>char*</code></td>
-   <td>Raw character pointer to ASCII (7-bit) string, no string classes used. High bit is not guaranteed across XPConnect boundaries</td>
-  </tr>
-  <tr>
-   <td><code>wstring</code></td>
-   <td><code>PRUnichar*</code></td>
-   <td>Raw character pointer to UTF-16 string, no string classes used</td>
-  </tr>
-  <tr>
-   <td><code>AString</code></td>
-   <td><code>nsAString</code></td>
-   <td>UTF-16 string</td>
-  </tr>
-  <tr>
-   <td><code>ACString</code></td>
-   <td><code>nsACString</code></td>
-   <td>8-bit string, all bits are preserved across XPConnect boundaries</td>
-  </tr>
-  <tr>
-   <td><code>AUTF8String</code></td>
-   <td><code>nsACString</code></td>
-   <td>UTF-8 string - converted to UTF-16 as necessary when value is used across XPConnect boundaries</td>
-  </tr>
-  <tr>
-   <td><code>DOMString</code></td>
-   <td><code>nsAString</code></td>
-   <td>UTF-16 string used in the DOM. More or less the same as <code>AString</code>, but in JavaScript it has no distinction between whether the string is void or just empty. (not sure on this, looking for corrections.</td>
-  </tr>
- </tbody>
-</table>
-
-<h3 id="C.2B.2B_.E3.82.B7.E3.82.B0.E3.83.8D.E3.83.81.E3.83.A3" name="C.2B.2B_.E3.82.B7.E3.82.B0.E3.83.8D.E3.83.81.E3.83.A3">C++ シグネチャ</h3>
-
-<p>IDL では、<code>in</code> 引数は読み込み専用で、*String 引数に対する C++ シグネチャにより、それらの引数のための <code>const nsAString&amp;</code> を使った上記ガイドラインが行えます。 <code>out</code> と <code>inout</code> 引数は、呼ばれた側で書き込み可能なよう単純に <code>nsAString</code> として定義されています。</p>
-
-<table class="standard-table">
- <tbody>
-  <tr>
-   <th class="header">IDL</th>
-   <th class="header">C++</th>
-  </tr>
-  <tr>
-   <td>
-    <pre class="eval">
-interface nsIFoo : nsISupports {
-
-    attribute AString utf16String;
-
-
-
-
-    AUTF8String getValue(in ACString key);
-
-};
-</pre>
-   </td>
-   <td>
-    <pre class="eval">
-class nsIFoo : public nsISupports {
-
-     NS_IMETHOD GetUtf16String(nsAString&amp;
-                               aResult) = 0;
-     NS_IMETHOD SetUtf16String(const nsAString&amp;
-                              aValue) = 0;
-
-     NS_IMETHOD GetValue(const nsACString&amp; aKey,
-                     nsACString&amp; aResult) = 0;
-};
-</pre>
-   </td>
-  </tr>
- </tbody>
-</table>
-
-<p>上記の例では、<code>unicodeString</code> は Unicode 文字列として扱われています。 <code>GetUnicodeString()</code> のインプリメントでは <code>aResult.Assign</code> を値を返す("return"する)のに使っています。 <code>SetUnicodeString()</code> では、文字列の値は<a href="#Iterators">イテレータ</a>、<a href="#Raw_Pointers"><code>PromiseFlatString</code></a>、その他の文字列への代入などを含む雑多なメソッドを通して使うことができます。</p>
-
-<p><code>GetValue()</code> では、最初の引数である <code>aKey</code> は、生の 8 ビット 値の連続として扱われます。 <code>aKey</code> 内の ASCII でないどの文字列も、XPConnect 境界を越えた場合も内容が保証されます。 <code>GetValue()</code> のインプリメンテーションは UTF-8 エンコードされた 8 ビット文字列の aResult への代入となっています。 もし、このメソッドがスクリプトからの呼び出しなどによって XPConnect の境界を越えて呼ばれたとき、結果は UTF-8 から UCS2へデコードされ、Unicode 値として利用されます。</p>
-
-<h3 id=".E6.96.87.E5.AD.97.E5.88.97.E5.9E.8B.E3.81.AE.E9.81.B8.E6.8A.9E" name=".E6.96.87.E5.AD.97.E5.88.97.E5.9E.8B.E3.81.AE.E9.81.B8.E6.8A.9E">文字列型の選択</h3>
-
-<p>IDL で使う正しい文字列型を決めるのは難しいかもしれません。 以下の点は適切な文字列型を決める助けになるでしょう。</p>
-
-<ul>
- <li>文字列クラスを用いることは、<code>out</code> 引数へ新規にメモリ割当をすることを防ぐでしょう。 例えば、もし呼び出し側が <code>nsAutoString</code> を <code>out</code> 引数のための値を受け取るのに使っている場合、(C++ 内で単純に <code>nsAString</code> として定義されている)短い(64 文字以下の)値の <code>out</code> 引数への代入は <code>nsAutoString</code> のバッファへの値のコピーに過ぎません。 それ以上に、文字列クラスを使うことで、文字列バッファを共有することができます。 多くの場合、ある文字列オブジェクトから別の文字列オブジェクトへ代入することにより、参照のカウントを単に増やすことを優先してコピーを避けることが出来ます。</li>
- <li>文字列クラスを使った <code>in</code> 文字列は、しばしば長さを事前に計算します。これはパフォーマンス上のメリットとなるでしょう。</li>
- <li>生の文字バッファが必要とされる場所では、<code>string</code> と <code>wstring</code> は <code>PromiseFlatString</code> よりも高速なアクセスを提供します。</li>
- <li><code>AUTF8String</code> で定義された UTF-8 文字列は、XPConnect 境界を越えるとき、デコードされる必要があるでしょう。 これはパフォーマンスに打撃を与えます。一方で、 UTF-8 文字列は共通で用いられる ASCII 文字列では省スペースしか占有しません。</li>
- <li><code>wstring</code> や <code>AString</code> で定義された Unicode 文字列は、Unicode 値が必要とされるときは、速いです。 しかし、もし値によりしばしば ASCII が使われるなら、下敷きとなった文字列の格納スペースの半分は無駄になります。</li>
-</ul>
-
-<h2 id=".E4.BB.98.E9.8C.B2_A_.E3.81.A9.E3.81.AE.E3.82.AF.E3.83.A9.E3.82.B9.E3.82.92.E3.81.84.E3.81.A4.E4.BD.BF.E3.81.86.E3.81.8B" name=".E4.BB.98.E9.8C.B2_A:_.E3.81.A9.E3.81.AE.E3.82.AF.E3.83.A9.E3.82.B9.E3.82.92.E3.81.84.E3.81.A4.E4.BD.BF.E3.81.86.E3.81.8B">付録 A: どのクラスをいつ使うか</h2>
-
-<p>この表はどのクラスをいつ使うべきかを示すクィックリファレンスです。</p>
-
-<table class="alternate data">
- <tbody>
-  <tr>
-   <th>内容</th>
-   <th>クラス</th>
-   <th>メモ</th>
-  </tr>
-  <tr class="even">
-   <td>ローカル変数</td>
-   <td><code>nsAutoString</code><code>nsCAutoString</code></td>
-   <td> </td>
-  </tr>
-  <tr class="odd">
-   <td>クラスのメンバ変数</td>
-   <td><code>nsString</code><code>nsCString</code></td>
-   <td> </td>
-  </tr>
-  <tr class="even">
-   <td>メソッドの引数の型</td>
-   <td><code>nsAString</code><code>nsACString</code></td>
-   <td>引数に抽象クラスを使う。入力引数には <code>const nsAString&amp;</code> を使い、出力引数には <code>nsAString&amp;</code> を使う。</td>
-  </tr>
-  <tr class="odd">
-   <td>出力文字列を回収するRetrieving "out" string/wstrings</td>
-   <td><code>nsXPIDLString</code><code>nsXPIDLCString</code></td>
-   <td><code>getter_Copies()</code> を使う。<code>nsString</code> / <code>nsCString</code> と似ている。</td>
-  </tr>
-  <tr class="even">
-   <td>文字バッファをラップするbuffers</td>
-   <td><code>nsDependentString</code><code>nsDependentCString</code></td>
-   <td><code>const char*</code> / <code>const PRUnichar*</code> バッファをラップする。</td>
-  </tr>
-  <tr class="odd">
-   <td>リテラル文字列</td>
-   <td><code>NS_LITERAL_STRING</code><code>NS_LITERAL_CSTRING</code></td>
-   <td>nsDependent[C]String と似ているが、ビルド時に長さが事前計算される。</td>
-  </tr>
- </tbody>
-</table>
-
-<h2 id=".E4.BB.98.E9.8C.B2_B_nsAString_.E3.83.AA.E3.83.95.E3.82.A1.E3.83.AC.E3.83.B3.E3.82.B9.3D" name=".E4.BB.98.E9.8C.B2_B:_nsAString_.E3.83.AA.E3.83.95.E3.82.A1.E3.83.AC.E3.83.B3.E3.82.B9.3D">付録 B: nsAString リファレンス=</h2>
-
-<p>読み込み専用メソッド</p>
-
-<ul>
- <li><strong>Length()</strong></li>
- <li><strong>IsEmpty()</strong></li>
- <li><strong>IsVoid()</strong></li>
- <li><strong>BeginReading(<em>iterator</em>)</strong></li>
- <li><strong>EndReading(<em>iterator</em>)</strong></li>
- <li><strong>Equals(<em>string</em>[,<em>comparator</em>])</strong></li>
- <li><strong>First()</strong></li>
- <li><strong>Last()</strong></li>
- <li><strong>CountChar()</strong></li>
- <li><strong>Left(<em>outstring</em>, <em>length</em>)</strong></li>
- <li><strong>Mid(<em>outstring</em>, <em>position</em>, <em>length</em>)</strong></li>
- <li><strong>Right(<em>outstring</em>, <em>length</em>)</strong></li>
- <li><strong>FindChar(<em>character</em>)</strong></li>
-</ul>
-
-<p>文字列を修正するメソッド</p>
-
-<ul>
- <li><strong>Assign(<em>string</em>)</strong></li>
- <li><strong>Append(<em>string</em>)</strong></li>
- <li><strong>Insert(<em>string</em>)</strong></li>
- <li><strong>Cut(<em>start</em>, <em>length</em>)</strong></li>
- <li><strong>Replace(<em>start</em>, <em>length</em>, <em>string</em>)</strong></li>
- <li><strong>Truncate(<em>length</em>)</strong></li>
- <li><strong>SetIsVoid(<em>state</em>)</strong></li>
- <li><strong>BeginWriting(<em>iterator</em>)</strong></li>
- <li><strong>EndWriting(<em>iterator</em>)</strong></li>
- <li><strong>SetCapacity()</strong></li>
-</ul>

+ 3 - 2
files/zh-cn/_redirects.txt

@@ -104,6 +104,7 @@
 /zh-CN/docs/Learn_XPI_Installer_Scripting_by_Example	/zh-CN/docs/Archive/Mozilla/XPInstall/Scripting_by_example
 /zh-CN/docs/Mozilla/Firefox/企业部署	/zh-CN/docs/Mozilla/Firefox/Enterprise_deployment
 /zh-CN/docs/Mozilla/Firefox/开发者_版本	/zh-CN/docs/Mozilla/Firefox/Developer_Edition
+/zh-CN/docs/Mozilla/Tech/XPCOM/Guide/Internal_strings	https://firefox-source-docs.mozilla.org/xpcom/stringguide.html
 /zh-CN/docs/Mozilla/调试	/zh-CN/docs/Mozilla/Debugging
 /zh-CN/docs/Multiple_Item_Packaging	/zh-CN/docs/Archive/Add-ons/Multiple_Item_Packaging
 /zh-CN/docs/NSPR	/zh-CN/docs/Mozilla/Projects/NSPR
@@ -177,11 +178,11 @@
 /zh-CN/docs/XPCOM/Array	/zh-CN/docs/Mozilla/Tech/XPCOM/Guide/Arrays
 /zh-CN/docs/XPCOM/Hashtables	/zh-CN/docs/Mozilla/Tech/XPCOM/Guide/Hashtables
 /zh-CN/docs/XPCOM/Language_Bindings	/zh-CN/docs/Mozilla/Tech/XPCOM/Language_Bindings
-/zh-CN/docs/XPCOM/Strings	/zh-CN/docs/Mozilla/Tech/XPCOM/Guide/Internal_strings
+/zh-CN/docs/XPCOM/Strings	https://firefox-source-docs.mozilla.org/xpcom/stringguide.html
 /zh-CN/docs/XPCOM:Array	/zh-CN/docs/Mozilla/Tech/XPCOM/Guide/Arrays
 /zh-CN/docs/XPCOM:Hashtables	/zh-CN/docs/Mozilla/Tech/XPCOM/Guide/Hashtables
 /zh-CN/docs/XPCOM:Language_Bindings	/zh-CN/docs/Mozilla/Tech/XPCOM/Language_Bindings
-/zh-CN/docs/XPCOM:Strings	/zh-CN/docs/Mozilla/Tech/XPCOM/Guide/Internal_strings
+/zh-CN/docs/XPCOM:Strings	https://firefox-source-docs.mozilla.org/xpcom/stringguide.html
 /zh-CN/docs/XPCOM_Glue	/zh-CN/docs/Mozilla/Tech/XPCOM/Glue
 /zh-CN/docs/XPCOM_Interface_Reference	/zh-CN/docs/Mozilla/Tech/XPCOM/Reference/Interface
 /zh-CN/docs/XPCOM_Interface_Reference/NsITraceableChannel	/zh-CN/docs/Mozilla/Tech/XPCOM/Reference/Interface/NsITraceableChannel

+ 0 - 8
files/zh-cn/_wikihistory.json

@@ -3731,14 +3731,6 @@
       "Secure alex"
     ]
   },
-  "Mozilla/Tech/XPCOM/Guide/Internal_strings": {
-    "modified": "2019-04-19T18:49:18.471Z",
-    "contributors": [
-      "wbamberg",
-      "ziyunfei",
-      "Secure alex"
-    ]
-  },
   "Mozilla/Tech/XPCOM/Interfacing_with_the_XPCOM_cycle_collector": {
     "modified": "2019-04-19T18:49:49.731Z",
     "contributors": [

+ 0 - 809
files/zh-cn/mozilla/tech/xpcom/guide/internal_strings/index.html

@@ -1,809 +0,0 @@
----
-title: Strings
-slug: Mozilla/Tech/XPCOM/Guide/Internal_strings
-tags:
-  - XPCOM
-  - 所有分类
-translation_of: Mozilla/Tech/XPCOM/Guide/Internal_strings
----
-<p> </p>
-<h2 id="Preface" name="Preface">Preface</h2>
-<div>
- <p>by Alec Flett<br>
-  Thanks to David Baron for <a class="external" href="http://dbaron.org/mozilla/coding-practices">actual docs</a>,<br>
-  Peter Annema for lots of direction,<br>
-  Myk Melez for some more docs, and<br>
-  David Bradley for a diagram<br>
-  Revised by Darin Fisher for Mozilla 1.7<br>
-  Revised by Jungshik Shin to clarify character encoding issues</p>
-</div>
-<p>This guide will attempt to document the plethora of string classes, and hopefully provide an answer to the age old question, "what string class should I use here?"</p>
-<div style="border: solid thin steelblue; padding: 0.5em;">
- <p>If you are a Mozilla embedder or if you are writing an XPCOM component that will be distributed separately from the Mozilla code base, then this string guide is most likely not for you! Provided you are developing against Mozilla 1.7 or later, you should instead be using the new minimal <a href="https://dxr.mozilla.org/mozilla-central/source/xpcom/glue/nsStringAPI.h" rel="custom">Mozilla string API</a> and in particular the <a href="https://dxr.mozilla.org/mozilla-central/source/xpcom/string/public/nsEmbedString.h" rel="custom">nsEmbedString</a> class.</p>
-</div>
-<p>In a hurry? Go check out the <a href="cn/XPCOM/String_Quick_Reference">String Quick-Reference</a> (<a class="external" href="http://www.mozilla.org/projects/xpcom/string-quickref.html">). </a></p>
-<h2 id="Introduction" name="Introduction"><a class="external" href="http://www.mozilla.org/projects/xpcom/string-quickref.html">Introduction </a></h2>
-<p><a class="external" href="http://www.mozilla.org/projects/xpcom/string-quickref.html">The string classes are a library of C++ classes which are used to manage buffers of unicode and single-byte character strings. They reside in the mozilla codebase in the <code></code></a><code><a href="https://dxr.mozilla.org/mozilla-central/source/xpcom/string" rel="custom">xpcom/string</a></code> directory. </p>
-<p><a class="external" href="http://www.mozilla.org/projects/xpcom/string-quickref.html">Abstract (interface) classes begin with "nsA" and concrete classes simply begin with "ns". Classes with a "<code>CString</code>" in the name store 8-bit bytes (<code>char</code>'s) which may refer to single byte ASCII strings, or multibyte Unicode strings encoded in UTF-8 or a (multibyte or single byte) legacy character encoding (e.g. ISO-8859-1, Shift_JIS, GB2312, KOI8-R). All other classes simply have "<code>String</code>" in their name and refer to 16-bit strings made up of <code>PRUnichar</code>'s, For example: <code>nsAString</code> is an abstract class for storing Unicode characters in UTF-16 encoding, and <code>nsDependentCString</code> is a concrete class which stores a 8-bit string. Every 16-bit string class has an equivalent 8-bit string class. For example: <code>nsCString</code> is the 8-bit string class which corresponds to <code>nsString</code>. </a></p>
-<p><a class="external" href="http://www.mozilla.org/projects/xpcom/string-quickref.html">8-bit and 16-bit string classes have completely separate base classes, but share the same APIs. As a result, you cannot assign a 8-bit string to a 16-bit string without some kind of conversion helper class or routine. For the purpose of this document, we will refer to the 16-bit string classes in class documentation. It is safe to assume that every 16-bit class has an equivalent 8-bit class. </a></p>
-<h2 id="String_Guidelines" name="String_Guidelines"><a class="external" href="http://www.mozilla.org/projects/xpcom/string-quickref.html">String Guidelines </a></h2>
-<p><a class="external" href="http://www.mozilla.org/projects/xpcom/string-quickref.html">Follow these simple rules in your code to keep your fellow developers, reviewers, and users happy. </a></p>
-<ul>
- <li><a class="external" href="http://www.mozilla.org/projects/xpcom/string-quickref.html">Avoid </a><code><a href="#Unicode_Conversion_ns.2ACString_vs._ns.2AString">*WithConversion</a></code> functions at all costs: <code>AssignWithConversion</code>, <code>AppendWithConversion</code>, <code>EqualsWithConversion</code>, etc</li>
- <li>Use the most abstract string class that you can. Usually this is:
-  <ul>
-   <li><code><a href="#The_Abstract_Classes">nsAString</a></code> for function parameters</li>
-   <li><code><a href="#The_Concrete_Classes_-_which_classes_to_use_when">nsString</a></code> for member variables</li>
-   <li><a href="#The_Concrete_Classes_-_which_classes_to_use_when"><code>nsAutoString</code> or <code>nsXPIDLString</code></a> for local (stack-based) variables</li>
-  </ul>
- </li>
- <li>Use <a href="#Literal_Strings"><code>NS_LITERAL_[C]STRING</code> / <code>NS_NAMED_LITERAL_[C]STRING</code></a> to represent literal strings (i.e. "foo") as nsAString-compatible objects.</li>
- <li>Use <a href="#String_Concatenation">string concatenation</a> (i.e. the "+" operator) when combining strings.</li>
- <li>Use <code><a href="#Raw_Character_Pointers">nsDependentString</a></code> when you have a raw character pointer that you need to convert to an nsAString-compatible string.</li>
- <li>Use <code><a href="#Substrings_.28string_fragments.29">Substring()</a></code> to extract fragments of existing strings.</li>
- <li>Use <a href="#Iterators">iterators</a> to parse and extract string fragments.</li>
-</ul>
-<h2 id="The_Abstract_Classes" name="The_Abstract_Classes">The Abstract Classes</h2>
-<p>Every string class derives from <code>nsAString</code> (or <code>nsACString</code>). This class provides the fundamental interface for access and manipulation of strings. While concrete classes derive from <code>nsAString</code>, <code>nsAString</code> itself cannot be instantiated.</p>
-<p>This is very similar to the idea of an "interface" that mozilla uses to describe abstract object descriptions in the rest of the codebase. In the case of interfaces, class names begin with "nsI" where "I" refers to "Interface". In the case of strings, abstract classes begin with "nsA" and the "A" means "Abstract".</p>
-<p>There are a number of abstract classes which derive from <code>nsAString</code>. These abstract subclasses also cannot be instantiated, but they describe a string in slightly more detail than <code>nsAString</code>. They guarantee that the underlying implementation behind the abstract class provides specific capabilities above and beyond <code>nsAString</code>.</p>
-<p>The list below describes the main base classes. Once you are familiar with them, see the appendix describing What Class to Use When.</p>
-<ul>
- <li><b><code>nsAString</code></b>: the abstract base class for all strings. It provides an API for assignment, individual character access, basic manipulation of characters in the string, and string comparison. This class corresponds to the XPIDL <code>AString</code> parameter type.</li>
- <li><b><code>nsSubstring</code></b>: the common base class for all of the string classes. Provides optimized access to data within the string. A <code>nsSubstring</code> is not necessarily null-terminated. (For backwards compatibility, <code>nsASingleFragmentString</code> is a typedef for this string class.)</li>
- <li><b><code>nsString</code></b>: builds on <code>nsSubstring</code> by guaranteeing a null-terminated storage. This allows for a method (<code>.get()</code>) to access the underlying character buffer. (For backwards compatibility, <code>nsAFlatString</code> is a typedef for this string class.)</li>
-</ul>
-<p>The remainder of the string classes inherit from either <code>nsSubstring</code> or <code>nsString</code>. Thus, every string class is compatible with <code>nsAString</code>.</p>
-<p>It's important to note that <code>nsSubstring</code> and <code>nsAString</code> both represent a contiguous array of characters that are not necessarily null-terminated. One might ask then ask why two different yet similar string classes need to exist. Well, <code>nsSubstring</code> exists primarily as an optimization since <code>nsAString</code> must retain binary compatibility with the frozen <code>nsAString</code> class that shipped with Mozilla 1.0. Up until the release of Mozilla 1.7, <code>nsAString</code> was capable of representing a string broken into multiple fragments. The cost associated with supporting multi-fragment strings was high and offered limited benefits. It was decided to eliminate support for multi-fragment strings in an effort to reduce the complexity of the string classes and improve performance. See <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=231995" title='FIXED: Exploring nsAString "defragmentation"'>bug 231995</a> for more details.</p>
-<p>Though <code>nsSubstring</code> provides a more efficient interface to its underlying buffer than <code>nsAString</code>, <code>nsAString</code> is still the most commonly used class for parameter passing. This is because it is the string class corresponding to <code>AString</code> in XPIDL. Therefore, this string guide will continue to discuss the string classes with an emphasis on <code>nsAString</code>.</p>
-<p>Since every string derives from <code>nsAString</code> (or <code>nsACString</code>), they all share a simple API. Common read-only methods:</p>
-<ul>
- <li><b><code>.Length()</code></b> - the number of code units (bytes for 8-bit string classes and PRUnichar's for 16-bit string classes) in the string.</li>
- <li><b><code>.IsEmpty()</code></b> - the fastest way of determining if the string has any value. Use this instead of testing <code>string.Length</code> == 0</li>
- <li><b><code>.Equals(string)</code></b> - TRUE if the given string has the same value as the current string.</li>
-</ul>
-<p>Common methods that modify the string:</p>
-<ul>
- <li><b><code>.Assign(string)</code></b> - Assigns a new value to the string.</li>
- <li><b><code>.Append(string)</code></b> - Appends a value to the string.</li>
- <li><b><code>.Insert(string, position)</code></b> - Inserts the given string before the code unit at position.</li>
- <li><b><code>.Truncate(length)</code></b> - shortens the string to the given length.</li>
-</ul>
-<p>Complete documentation can be found in the <a href="#Appendix_B_-_nsAString_Reference">Appendix</a>.</p>
-<h3 id="Read-only_strings" name="Read-only_strings">Read-only strings</h3>
-<p>The <code>const</code> attribute on a string determines if the string is writable. If a string is defined as a <code>const nsAString</code> then the data in the string cannot be manipulated. If one tries to call a non-<code>const</code> method on a <code>const</code> string the compiler will flag this as an error at build time.</p>
-<p>For example:</p>
-<pre class="eval">void nsFoo::ReverseCharacters(nsAString&amp; str) {
-      ...
-     str.Assign(reversedStr); // modifies the string
-}
-</pre>
-<p>This should not compile, because you're assigning to a <code>const</code> class:</p>
-<pre class="eval">void nsFoo::ReverseCharacters(const nsAString&amp; str) {
-      ...
-     <b>str.Assign(reversedStr);</b>
-}
-</pre>
-<h3 id="As_function_parameters" name="As_function_parameters">As function parameters</h3>
-<p>It is recommended that you use the most abstract interface possible as a function parameter, instead of using concrete classes. The convention is to use C++ references (the '&amp;' character) instead of pointers (the '*' character) when passing string references around. For example:</p>
-<pre class="eval">// abstract reference
-nsFoo::PrintString(<b>const nsAString&amp;</b> str) {..}
-
-// using a concrete class!
-nsFoo::PrintString(<b>const nsString&amp;</b> str) {..}
-
-// using a pointer!
-nsFoo::PrintString(<b>const nsAString*</b> str) {..}
-</pre>
-<p>The abstract classes are also sometimes used to store temporary references to objects. You can see both of these uses in <a href="#Common_Patterns">Common Patterns</a>, below.</p>
-<p><b>NOTE:</b> While using abstract string classes increases the re-usability of your methods, it also incurs a codesize and performance penalty. Therefore, when writing methods that will only ever be used within the confines of your source file or module, it is better to use <b><code>const nsSubstring&amp;</code></b> for input parameters and <b><code>nsString&amp;</code></b> for output parameters. --Darin</p>
-<h2 id="The_Concrete_Classes_-_which_classes_to_use_when" name="The_Concrete_Classes_-_which_classes_to_use_when">The Concrete Classes - which classes to use when</h2>
-<p>The concrete classes are for use in code that actually needs to store string data. The most common uses of the concrete classes are as local variables, and members in classes or structs. Whereas the abstract classes differ in storage mechansim, for the most part the concrete classes differ in storage policy.</p>
-<p>The following is a list of the most common concrete classes. Once you are familiar with them, see the appendix describing <a href="#Appendix_A_-_What_class_to_use_when">What Class to Use When.</a></p>
-<ul>
- <li><code><b>nsString / nsCString</b></code>- a null-terminated string whose buffer is allocated on the heap. Destroys its buffer when the string object goes away.</li>
- <li><code><b>nsAutoString / nsCAutoString</b></code>- derived from <code>nsString</code>, a string which owns a 64 code unit buffer in the same storage space as the string itself. If a string less than 64 code units is assigned to an <code>nsAutoString</code>, then no extra storage will be allocated. For larger strings, a new buffer is allocated on the heap.</li>
- <li><code><b>nsXPIDLString / nsXPIDLCString</b></code>- derived from <code>nsString</code>, this class supports the <code>getter_Copies()</code> operator which allows easy access to XPIDL <code>out wstring / string</code> parameters. This class also supports the notion of a null-valued buffer, whereas <code>nsString</code>'s buffer is never null.</li>
- <li><code><b>nsDependentString</b></code>- derived from <code>nsString</code>, this string does
-  <i>
-   not</i>
-  own its buffer. It is useful for converting a raw string (<code>const PRUnichar*</code> or <code>const char*</code>) into a class of type <code>nsAString</code>.</li>
- <li><code><b>nsPrintfCString</b></code>- derived from <code>nsCString</code>, this string behaves like an <code>nsCAutoString</code>. The constructor takes parameters which allows it to construct a 8-bit string from a <code>printf</code>-style format string and parameter list.</li>
- <li><code><b>NS_LITERAL_STRING/NS_NAMED_LITERAL_STRING</b></code>- these convert a literal string (such as "abc") to a <code>nsString</code> or a subclass of <code>nsString</code>. On platforms supporting double-byte string literals (e.g., MSVC++ or GCC with the -fshort-wchar option), these are simply macros around the <code>nsDependentString</code> class. They are slightly faster than just wrapping them with an <code>nsDependentString</code> because they use the compiler to calculate their length, and they also hide the messy cross-platform details of non-byte literal strings.</li>
-</ul>
-<p>There are also a number of concrete classes that are created as a side-effect of helper routines, etc. You should avoid direct use of these classes. Let the string library create the class for you.</p>
-<ul>
- <li><code><b>nsSubstringTuple</b></code> - created via <a href="#String_Concatenation">string concatenation</a></li>
- <li><code><b>nsDependentSubstring</b></code> - created through <a href="#Substrings_.28string_fragments.29">Substring</a></li>
- <li><code><b>nsPromiseFlatString</b></code> - created through <code><b><a href="#Raw_Character_Pointers">PromiseFlatString()</a></b></code></li>
-</ul>
-<p>Of course, there are times when it is necessary to reference these string classes in your code, but as a general rule they should be avoided.</p>
-<h2 id="Iterators" name="Iterators">Iterators</h2>
-<p>Iterators are objects that retain a reference to a position in a string. In some ways they are like a number which refers to an index in an array, or a character-pointer that refers to a position in a character string. They also provide a syntactic means to distinguish between reading and writing to a string.</p>
-<p>Iterators are most often used to extract substrings of a string. They provide the capability to modify the contents of a string, but often helper routines, or the string's own methods are quicker at complex string transformations.</p>
-<p>Iterators are declared from the string class which they are iterating:</p>
-<pre class="eval">nsAString::const_iterator start, end; // reading-only iterators for nsAString
-nsString::iterator substr_start, substr_end; // writing iterators for nsString
-</pre>
-<p>Iterators are initialized with one of 4 methods on the string you wish to reference:</p>
-<pre class="eval">// let's read from 'str'
-str.BeginReading(start); // initialize 'start' to the beginning of 'str'
-str.EndReading(end); // 'end' will be at the end of the string
-
-// say we also want to write to 'url'
-url.BeginWriting(substr_start);
-url.EndWriting(substr_end);
-</pre>
-<p>You can access the code unit that an iterator points to with the dereference operator *.</p>
-<pre class="eval">if (*start == '[')
-     printf("Starts with a bracket\n");
-</pre>
-<p>Note in the above examples, that '<code>end</code>' and '<code>substr_end</code>' will actually point to the code unit past the end of the string, so you should never dereference the direct result of <code>.EndReading()</code>.</p>
-<p>You can test if two iterators point to the same position with == or !=. You can advance iterators with ++. Putting the ++ before your iterator is preferred, and will prevent creation of a temporary iterator.</p>
-<pre class="eval">while (start != end) // iterate through the whole string
-     ++start;
-</pre>
-<p>You can effectively write to a string with writing iterators (as opposed to const-iterators):</p>
-<pre class="eval">// change all * to !
-while (substr_start != substr_end) {
-     if (*substr_start == '*')
-           *substr_start = '!';
-     ++substr_start;
-}
-</pre>
-<p>With the patch for <a class="external" href="http://bugzilla.mozilla.org/show_bug.cgi?id=231995">bug 231995</a>, this loop is now as efficient as iterating with raw character pointers.</p>
-<h2 id="Helper_Classes_and_Functions" name="Helper_Classes_and_Functions">Helper Classes and Functions</h2>
-<h3 id="Searching_strings_-_looking_for_substrings.2C_characters.2C_etc." name="Searching_strings_-_looking_for_substrings.2C_characters.2C_etc.">Searching strings - looking for substrings, characters, etc.</h3>
-<p><code>FindInReadable()</code> is the replacement for the old <code>string.Find(..)</code>. The syntax is:</p>
-<pre class="eval">PRBool FindInReadable(const nsAString&amp; pattern,
-                      nsAString::const_iterator start, nsAString::const_iterator end,
-                      nsStringComparator&amp; aComparator = nsDefaultStringComparator());
-</pre>
-<p>To use this, <code>start</code> and <code>end</code> should point to the beginning and end of a string that you would like to search. If the search string is found, <code>start</code> and <code>end</code> will be adjusted to point to the beginning and end of the found pattern. The return value is PR_TRUE or PR_FALSE, indicating whether or not the string was found.</p>
-<p>An example:</p>
-<pre class="eval">const nsAString&amp; str = GetSomeString();
-nsAString::const_iterator start, end;
-
-str.BeginReading(start);
-str.EndReading(end);
-
-NS_NAMED_LITERAL_STRING(valuePrefix, "value=");
-
-if (FindInReadable(valuePrefix, start, end)) {
-    // end now points to the character after the pattern
-    valueStart = end;
-
-}
-</pre>
-<h3 id="Memory_Allocation_-_how_to_avoid_it.2C_which_methods_to_use" name="Memory_Allocation_-_how_to_avoid_it.2C_which_methods_to_use">Memory Allocation - how to avoid it, which methods to use</h3>
-<p>The preferred method to allocate a new character buffer (<code>PRUnichar*</code>/<code>char*</code>) from an existing string is with one of the following methods:</p>
-<ul>
- <li><code><b>PRUnichar* ToNewUnicode(
-  <i>
-   nsAString&amp;</i>
-  )</b></code> - Allocates a <code>PRUnichar*</code>buffer from an <code>nsAString</code>.</li>
- <li><code><b>char *ToNewCString(
-  <i>
-   nsACString&amp;</i>
-  )</b></code> - Allocates a <code>char*</code>buffer from an <code>nsACString</code>. Note that this method will also work on nsAStrings, but it will do an implicit <a href="#Lossy_Conversion">lossy conversion</a>. This function should only be used if the input is known to be strictly ASCII. Often a conversion to UTF-8 is more appropriate. See <code><b>ToNewUTF8String</b></code> below.</li>
- <li><code><b>char* ToNewUTF8String(
-  <i>
-   nsAString&amp;</i>
-  )</b></code> - Allocates a new <code>char*</code> buffer containing the UTF-8 encoded version of the given nsAString. See <a href="#Unicode_Conversion_ns.2ACString_vs._ns.2AString">Unicode Conversion</a> for more details.</li>
-</ul>
-<p>These methods return a buffer allocated using XPCOM's allocator (<code>nsMemory::Alloc</code>) instead of the traditional allocator (<code>malloc</code>, etc.). You should use <code>nsMemory::Free</code> to deallocate the result when you no longer need it.</p>
-<h3 id="Substrings_.28string_fragments.29" name="Substrings_.28string_fragments.29">Substrings (string fragments)</h3>
-<p>It is very simple to refer to a substring of an existing string without actually allocating new space and copying the characters into that substring. <code>Substring()</code> is the preferred method to create a reference to such a string.</p>
-<pre class="eval">void ProcessString(const nsAString&amp; str) {
-    const nsAString&amp; firstFive = Substring(str, 0, 5);
-    // firstFive is now a string representing the first 5 characters
-}
-</pre>
-<h2 id="Unicode_Conversion_ns.2ACString_vs._ns.2AString" name="Unicode_Conversion_ns.2ACString_vs._ns.2AString">Unicode Conversion ns*CString vs. ns*String</h2>
-<p>Strings can be
- <i>
-  stored</i>
- in two basic formats: 8-bit code unit (byte/<code>char</code>) strings, or 16-bit code unit (<code>PRUnichar</code>) strings. Any string class with a capital "C" in the classname contains 8-bit bytes. These classes include <code>nsCString</code>, <code>nsDependentCString</code>, and so forth. Any string class
- <i>
-  without</i>
- the "C" contains 16-bit code units.</p>
-<p>A 8-bit string can be in one of many character encodings while a 16-bit string is always in UTF-16. The most common encodings are:</p>
-<ul>
- <li>ASCII - 8-bit encoding for basic English-only strings. Each ASCII value is stored in exactly one byte in the array.</li>
- <li><a class="external" href="http://www.unicode.org/glossary/#UCS_2">UCS2</a> - 16-bit encoding for a
-  <i>
-   subset</i>
-  of Unicode, <a class="external" href="http://www.unicode.org/glossary/#BMP">BMP</a>. The Unicode value of a character stored in UCS2 is stored in exactly one 16-bit <code>PRUnichar</code> in a string class.</li>
- <li><a class="external" href="http://www.faqs.org/rfcs/rfc3629.html">UTF-8</a> - 8-bit encoding for Unicode characters. Each Unicode characters is stored in up to 4 bytes in a string class. UTF-8 is capable of representing the entire Unicode character repertoire, and it efficiently maps to <a class="external" href="http://www.unicode.org/glossary/#UTF_32">UTF-32</a>.</li>
- <li><a class="external" href="http://www.unicode.org/glossary/#UTF_16">UTF-16</a> - 16-bit encoding for Unicode storage, backwards compatible with UCS2. The Unicode value of a character stored in UTF-16 may require
-  <i>
-   one or two</i>
-  16-bit <code>PRUnichar</code>s in a string class. The contents of <code>nsAString</code> always has to be regarded as in this encoding instead of UCS2. UTF-16 is capable of representing the entire Unicode character repertoire, and it efficiently maps to UTF-32. (Win32 W APIs and Mac OS X natively use UTF-16.)</li>
-</ul>
-<p>In addition, there are literally hundreds of encodings that are provided by internationalization libraries. Access to these libraries may be part of the application (such as <code>nsICharsetConversionManager</code> in Mozilla) or built into the operating system (such as <code>iconv()</code> in UNIX operating systems and <code>MultiByteToWideChar</code>/<code>WideCharToMultiByte</code> on Windows).</p>
-<p>When working with existing code, it is important to examine the current usage of the strings that you are manipulating, to determine the correct conversion mechanism.</p>
-<p>When writing new code, it can be confusing to know which storage class and encoding is the most appropriate. There is no single answer to this question, but there are a few important guidelines:</p>
-<ul>
- <li><b>Is the string always ASCII?</b> First and foremost, you need to determine what kinds of values will be stored in the string. If the strings are always internal, ASCII strings such as "left", "true", "background" and so forth, then straight C-strings are probably the way to go.</li>
- <li><b>If the string is ASCII, will it be compared to, assigned to, or otherwise interact with non-ASCII strings?</b> When assigning or comparing an 8-bit ASCII value (in)to a 16-bit UCS2 string, an "inflation" needs to happen at runtime. If your strings are small enough (say, less than 64 bytes) then it may make sense to store your string in a 16-bit unicode class as well, to avoid the extra conversion. The tradeoff is that your ASCII string takes up twice as much space as a 16-bit Unicode string than it would as an 8-bit string.</li>
- <li><b>Is the string usually ASCII, but needs to support unicode?</b> If your string is most often ASCII but needs to be able to store Unicode characters, then UTF-8 may be the right encoding. ASCII characters will still be stored in 8-bit storage but other Unicode characters will take up 2 to 4 bytes. However if the string ever needs to be compared or assigned to a 16-bit string, a runtime conversion will be necessary.</li>
- <li><b>Are you storing large strings of non-ASCII data?</b> Up until this point, UTF-8 might seem like the ideal encoding. The drawback is that for most non-European characters (such as Chinese, Indian and Japanese) in BMP, UTF-8 takes 50% more space than UTF-16. For characters in plane 1 and above, both UTF-8 and UTF-16 take 4 bytes.</li>
- <li><b>Do you need to manipulate the contents of a Unicode string?</b> One problem with encoding Unicode characters in UTF-8 or other 8-bit storage formats is that the actual Unicode character can span multiple bytes in a string. In most encodings, the actual number of bytes varies from character to character. When you need to iterate over each character, you must take the encoding into account. This is vastly simplified when iterating 16-bit strings because each 16-bit code unit (<code>PRUnichar</code>) corresponds to a Unicode character as long as all characters are in BMP, which is often the case. However, you have to keep in mind that a single Unicode character in plane 1 and beyond is represented in two 16-bit code units in 16-bit strings so that the number of <code>PRUnichar</code>'s is
-  <i>
-   not</i>
-  always equal to the number of Unicode characters. For the same reason, the position and the index in terms of 16-bit code units are not always the same as the position and the index in terms of Unicode characters.</li>
-</ul>
-<p><br>
- To assist with ASCII, UTF-8, and UTF-16 conversions, there are some helper methods and classes. Some of these classes look like functions, because they are most often used as temporary objects on the stack.</p>
-<h3 id="UTF-8_.2F_UTF-16_conversion" name="UTF-8_.2F_UTF-16_conversion">UTF-8 / UTF-16 conversion</h3>
-<p><code><b>NS_ConvertUTF8toUTF16(
- <i>
-  const nsACString&amp;</i>
- )</b></code> - a <code>nsAutoString</code> subclass that converts a UTF-8 encoded <code>nsACString</code> or <code>const char*</code> to a 16-bit UTF-16 string. If you need a <code>const PRUnichar*</code> buffer, you can use the <code>.get()</code> method. For example:</p>
-<pre class="eval">/* signature: void HandleUnicodeString(const nsAString&amp; str); */
-object-&gt;HandleUnicodeString(<b>NS_ConvertUTF8toUTF16</b>(utf8String));
-
-/* signature: void HandleUnicodeBuffer(const PRUnichar* str); */
-object-&gt;HandleUnicodeBuffer(<b>NS_ConvertUTF8toUTF16</b>(utf8String).get());
-</pre>
-<p><code><b>NS_ConvertUTF16toUTF8(
- <i>
-  const nsAString&amp;</i>
- )</b></code> - a <code>nsCAutoString</code> which converts a 16-bit UTF-16 string (<code>nsAString</code>) to a UTF-8 encoded string. As above, you can use <code>.get()</code> to access a <code>const char*</code> buffer.</p>
-<pre class="eval">/* signature: void HandleUTF8String(const nsACString&amp; str); */
-object-&gt;HandleUTF8String(<b>NS_ConvertUTF16toUTF8</b>(utf16String));
-
-/* signature: void HandleUTF8Buffer(const char* str); */
-object-&gt;HandleUTF8Buffer(<b>NS_ConvertUTF16toUTF8</b>(utf16String).get());
-</pre>
-<p><code><b>CopyUTF8toUTF16(
- <i>
-  const nsACString&amp;, nsAString&amp;</i>
- )</b></code> - converts and copies:</p>
-<pre class="eval">// return a UTF-16 value
-void Foo::GetUnicodeValue(nsAString&amp; result) {
-    <b>CopyUTF8toUTF16</b>(mLocalUTF8Value, result);
- }
-</pre>
-<p><code><b>AppendUTF8toUTF16(
- <i>
-  const nsACString&amp;, nsAString&amp;</i>
- )</b></code> - converts and appends:</p>
-<pre class="eval">// return a UTF-16 value
-void Foo::GetUnicodeValue(nsAString&amp; result) {
-    result.AssignLiteral("prefix:");
-    <b>AppendUTF8toUTF16</b>(mLocalUTF8Value, result);
-}
-</pre>
-<p><br>
- <code><b>UTF8ToNewUnicode(
- <i>
-  const nsACString&amp;, PRUint32* aUTF16Count = nsnull</i>
- )</b></code> - allocates and converts (the optional parameter will contain the number of 16-byte units upon return, if non-null):</p>
-<pre class="eval">void Foo::GetUTF16Value(PRUnichar** result) {
-    *result = <b>UTF8ToNewUnicode</b>(mLocalUTF8Value);
-}
-</pre>
-<p><br>
- <code><b>CopyUTF16toUTF8(
- <i>
-  const nsAString&amp;, nsACString&amp;</i>
- )</b></code> - converts and copies:</p>
-<pre class="eval">// return a UTF-8 value
-void Foo::GetUTF8Value(nsACString&amp; result) {
-    <b>CopyUTF16toUTF8</b>(mLocalUTF16Value, result);
-}
-</pre>
-<p><code><b>AppendUTF16toUTF8(
- <i>
-  const nsAString&amp;, nsACString&amp;</i>
- )</b></code> - converts and appends:</p>
-<pre class="eval">// return a UTF-8 value
-void Foo::GetUnicodeValue(nsACString&amp; result) {
-    result.AssignLiteral("prefix:");
-    <b>AppendUTF16toUTF8</b>(mLocalUTF16Value, result);
-}
-</pre>
-<p><code><b>ToNewUTF8String(
- <i>
-  const nsAString&amp;</i>
- )</b></code> - allocates and converts:</p>
-<pre class="eval">void Foo::GetUTF8Value(char** result) {
-    *result = <b>ToNewUTF8String</b>(mLocalUTF16Value);
-}
-</pre>
-<h3 id="Lossy_Conversion" name="Lossy_Conversion">Lossy Conversion</h3>
-<p>The following should only be used when you can guarantee that the original string is ASCII. These helpers are very similar to the UTF-8 / UTF-16 conversion helpers above.</p>
-<h4 id="UTF-16_to_ASCII_converters" name="UTF-16_to_ASCII_converters">UTF-16 to ASCII converters</h4>
-<p>These converters are
- <i>
-  <b>very dangerous</b></i>
- because they
- <i>
-  <b>lose information</b></i>
- during the conversion process. You should
- <i>
-  <b>avoid UTF-16 to ASCII conversions</b></i>
- unless your strings are guaranteed to be ASCII. Each 16-bit code unit in 16-bit string is simply cast to an 8-bit byte, which means all Unicode character values above 0xFF are converted to an arbitrary 8-bit byte.</p>
-<ul>
- <li><code><b>NS_LossyConvertUTF16toASCII(
-  <i>
-   nsAString</i>
-  )</b></code> - a <code>nsCAutoString</code> which holds a temporary buffer containing the deflated value of the string.</li>
- <li><code><b>LossyCopyUTF16toASCII(
-  <i>
-   nsAString, nsACString</i>
-  )</b></code> - does an in-place conversion from UTF-16 into an ASCII string object.</li>
- <li><code><b>LossyAppendUTF16toASCII(
-  <i>
-   nsAString, nsACString</i>
-  )</b></code> - appends an UTF-16 string to an ASCII string, losing non-ASCII values.</li>
- <li><code><b>ToNewCString(
-  <i>
-   nsAString</i>
-  )</b></code> - allocates a new <code>char*</code> string.</li>
-</ul>
-<h4 id="ASCII_to_UTF-16_converters" name="ASCII_to_UTF-16_converters">ASCII to UTF-16 converters</h4>
-<p>These converters are
- <i>
-  <b>very dangerous</b></i>
- because they will
- <i>
-  <b>mangle any non-ASCII string</b></i>
- into a meaningless UTF-16 string. You should
- <i>
-  <b>avoid ASCII to UTF-16 conversions</b></i>
- unless your strings are guaranteed to be ASCII. For instance, if you have an 8-bit string encoded in a multibyte character encoding, each byte of the string will be "inflated" to a 16-bit number by simple casting.</p>
-<p>For example, imagine a UTF-8 string where the first Unicode character of the string is represented with a 3-byte UTF-8 sequence, the "inflated" UTF-16 string will contain the 3 <code>PRUnichar</code>'s instead of the single <code>PRUnichar</code> that represents the first character. These <code>PRUnichar</code>'s have nothing to do with the first Unicode character in the UTF-8 string.</p>
-<ul>
- <li><code><b>NS_ConvertASCIItoUTF16(
-  <i>
-   nsACString</i>
-  )</b></code> - a <code>nsAutoString</code> which holds a temporary buffer containing the inflated value of the string.</li>
- <li><code><b>CopyASCIItoUTF16(
-  <i>
-   nsACString, nsAString</i>
-  )</b></code> - does an in-place conversion from one string into a Unicode string object.</li>
- <li><code><b>AppendASCIItoUTF16(
-  <i>
-   nsACString, nsAString</i>
-  )</b></code> - appends an ASCII string to a Unicode string.</li>
- <li><code><b>ToNewUnicode(
-  <i>
-   nsACString</i>
-  )</b></code> - Creates a new <code>PRUnichar*</code> string which contains the inflated value.</li>
-</ul>
-<h2 id="Common_Patterns" name="Common_Patterns">Common Patterns</h2>
-<h3 id="Callee-allocated_Parameters" name="Callee-allocated_Parameters">Callee-allocated Parameters</h3>
-<p>Many APIs result in a method allocating a buffer in order to return strings to its caller. This can be tricky because the caller has to remember to free the string when they have finished using it. Fortunately, the <code>nsXPIDLString</code> class makes this very easy.</p>
-<p>A method may look like this:</p>
-<pre class="eval">void GetValue(PRUnichar** aValue)
-{
-    *aValue = ToNewUnicode(foo);
-}
-</pre>
-<p>Without the string classes, the caller would need to free the string:</p>
-<pre>{
-    PRUnichar* val;
-    GetValue(&amp;val);
-
-    if (someCondition) {
-        // don't forget to free the value!
-        nsMemory::Free(val);
-        return NS_ERROR_FAILURE;
-    }
-
-    ...
-    // and later, still don't forget to free!
-    nsMemory::Free(val);
-}
-</pre>
-<p>With <code>nsXPIDLString</code> you never have to worry about this. You can just use <code>getter_Copies()</code> to wrap the string class, and the class will remember to free the buffer when it goes out of scope:</p>
-<pre>{
-    nsXPIDLString val;
-    GetValue(getter_Copies(val));
-
-    // val will free itself here
-    if (someCondition)
-        return NS_ERROR_FAILURE;
-    ...
-    // and later, still nothing to free
-}
-</pre>
-<p>The resulting code is much simpler, and easy to read.</p>
-<h3 id="Literal_Strings" name="Literal_Strings">Literal Strings</h3>
-<p>A
- <i>
-  literal string</i>
- is a raw string value that is written in some C++ code. For example, in the statement <code>printf("Hello World\n");</code> the value <code>"Hello World\n"</code> is a literal string. It is often necessary to insert literal string values when an <code>nsAString</code> or <code>nsACString</code> is required. These four macros will provide you with the necessary conversion:</p>
-<ul>
- <li><code><b>NS_LITERAL_CSTRING(
-  <i>
-   literal string</i>
-  )</b></code> - a temporary <code>nsCString</code></li>
- <li><code><b>NS_NAMED_LITERAL_CSTRING(
-  <i>
-   variable,literal string</i>
-  )</b></code> - declares a <code>nsCString</code> variable named
-  <i>
-   variable</i>
- </li>
- <li><code><b>NS_LITERAL_STRING(
-  <i>
-   literal string</i>
-  )</b></code> - a temporary <code>nsString</code> with the unicode version of
-  <i>
-   literal string</i>
- </li>
- <li><code><b>NS_NAMED_LITERAL_STRING(
-  <i>
-   variable,literal string</i>
-  )</b></code> - declares a <code>nsString</code> variable named
-  <i>
-   variable</i>
-  with the unicode version of
-  <i>
-   literal string</i>
- </li>
-</ul>
-<p>The purpose of the <code>CSTRING</code> versions of these macros may seem unnecessary, given that <code>nsDependentCString</code> will also wrap a string value in an <code>nsCString</code>. The advantage to these macros is that the length of these strings is calculated at compile time, so the string does not need to be scanned at runtime to determine its length.</p>
-<p>The <code>STRING</code> versions of these macros provide a portable way of declaring UTF-16 versions of the given literal string, avoiding runtime conversion on platforms which support literal UTF-16 strings (e.g., MSVC++ and GCC with the -fshort-wchar option).</p>
-<pre>// call Init(const PRUnichar*)
-Init(L"start value"); // bad - L"..." is not portable!
-Init(NS_ConvertASCIItoUTF16("start value").get()); // bad - runtime ASCII-&gt;UTF-16 conversion!
-
-// call Init(const nsAString&amp;)
-Init(nsDependentString(L"start value")); // bad - not portable!
-Init(NS_ConvertASCIItoUTF16("start value")); // bad - runtime ASCII-&gt;UTF-16 conversion!
-
-// call Init(const nsACString&amp;)
-Init(nsDependentCString("start value")); // bad - length determined at runtime
-</pre>
-<p>Here are some examples of proper <code>NS_LITERAL_[C]STRING</code> usage.</p>
-<pre>// call Init(const PRUnichar*)
-Init(NS_LITERAL_STRING("start value").get());
-
-// call Init(const nsAString&amp;)
-Init(NS_LITERAL_STRING("start value"));
-
-// call Init(const nsACString&amp;)
-Init(NS_LITERAL_CSTRING("start value"));
-</pre>
-<p>There are a few details which can be useful in tracking down issues with these macros:</p>
-<p><code>NS_LITERAL_STRING</code> does compile-time conversion to UTF-16 on some platforms (e.g. Windows, Linux, and Mac) but does runtime conversion on other platforms. By using <code>NS_LITERAL_STRING</code> your code is guaranteed to use the best possible conversion for the platform in question.</p>
-<p>Because some platforms do runtime conversion, the use of literal string concatenation inside a <code>NS_LITERAL_STRING/NS_NAMED_LITERAL_STRING</code> macro will compile on these platforms, but not on platforms which support compile-time conversion.</p>
-<p>For example:</p>
-<pre>// call Init(nsAString&amp;)
-Init(NS_LITERAL_STRING("start "
-     "value")); // only compiles on some platforms
-</pre>
-<p>The reason for this is that on some platforms, the <code>L"..."</code> syntax is used, but it is only applied to the first string in the concatenation (<code>"start "</code>). When the compiler attempts to concatenate this with the non-Unicode string <code>"value"</code> it gets confused.</p>
-<p>Also, using preprocessor macros as the string literal is unsupported:</p>
-<pre>#define some_string "See Mozilla Run"
-...
-Init(NS_LITERAL_STRING( some_string )); // only compiles on some platforms/with some compilers.
-
-</pre>
-<h3 id="String_Concatenation" name="String_Concatenation">String Concatenation</h3>
-<p>Strings can be concatenated together using the + operator. The resulting string is a <code>const nsSubstringTuple</code> object. The resulting object can be treated and referenced similarly to a <code>nsAString</code> object. Concatenation
- <i>
-  does not copy the substrings</i>
- . The strings are only copied when the concatenation is assigned into another string object. The <code>nsSubstringTuple</code> object holds pointers to the original strings. Therefore, the <code>nsSubstringTuple</code> object is dependent on all of its substrings, meaning that their lifetime must be at least as long as the <code>nsSubstringTuple</code> object.</p>
-<p>For example, you can use the value of two strings and pass their concatenation on to another function which takes an <code>const nsAString&amp;:</code></p>
-<pre class="eval">void HandleTwoStrings(const nsAString&amp; one, const nsAString&amp; two) {
-    // call HandleString(const nsAString&amp;)
-    HandleString(one + two);
-}
-</pre>
-<p>NOTE: The two strings are implicitly combined into a temporary <code>nsString</code> in this case, and the temporary string is passed into <code>HandleString</code>. If <code>HandleString</code> assigns its input into another <code>nsString</code>, then the string buffer will be shared in this case negating the cost of the intermediate temporary. You can concatenate N strings and store the result in a temporary variable:</p>
-<pre class="eval">NS_NAMED_LITERAL_STRING(start, "start ");
-NS_NAMED_LITERAL_STRING(middle, "middle ");
-NS_NAMED_LITERAL_STRING(end, "end");
-// create a string with 3 dependent fragments - no copying involved!
-nsString combinedString = start + middle + end;
-
-// call void HandleString(const nsAString&amp;);
-HandleString(combinedString);
-</pre>
-<p>If you are using <code>NS_LITERAL_STRING</code> to create a temporary that is only used once, then it is safe to define it inside a concatenation because the string buffer will live as long as the temporary concatenation object (of type <code>nsSubstringTuple</code>).</p>
-<pre class="eval">// call HandlePage(const nsAString&amp;);
-// safe because the concatenated-string will live as long as its substrings
-HandlePage(NS_LITERAL_STRING("start ") + NS_LITERAL_STRING("end"));
-</pre>
-<h3 id="Local_variables" name="Local_variables">Local variables</h3>
-<p>Local variables within a function are usually stored on the stack. The <code>nsAutoString/nsCAutoString</code> classes are derivatives of the <code>nsString/nsCString classes</code>. They own a 64-character buffer allocated in the same storage space as the string itself. If the <code>nsAutoString</code> is allocated on the stack, then it has at its disposal a 64-character stack buffer. This allows the implementation to avoid allocating extra memory when dealing with small strings.</p>
-<pre class="eval">...
-nsAutoString value;
-GetValue(value); // if the result is less than 64 code units,
-                 // then this just saved us an allocation
-...
-</pre>
-<h3 id="Member_variables" name="Member_variables">Member variables</h3>
-<p>In general, you should use the concrete classes <code>nsString</code> and <code>nsCString</code> for member variables.</p>
-<pre class="eval">class Foo {
-    ...
-    // these store UTF-8 and UTF-16 values respectively
-    nsCString mLocalName;
-    nsString mTitle;
-};
-</pre>
-<p>Note that the strings are declared directly in the class, not as pointers to strings. Don't do this:</p>
-<pre>class Foo {
-public:
-    Foo() {
-        mLocalName = new nsCString();
-        mTitle = new nsString();
-    }
-    ~Foo() { delete mLocalName; delete mTitle; }
-
-private:
-    // these store UTF-8 and UTF-16 values respectively
-    nsCString* mLocalName;
-    nsString*  mTitle;
-};
-</pre>
-<p>The above code may appear to save the cost of the string objects, but <code>nsString/nsCString</code> are small objects - the overhead of the allocation outweighs the few bytes you'd save by keeping a pointer.</p>
-<p>Another common incorrect pattern is to use <code>nsAutoString/nsCAutoString</code> for member variables. As described in <a href="#Local_variables">Local Variables</a>, these classes have a built in buffer that make them very large. This means that if you include them in a class, they bloat the class by 64 bytes (<code>nsCAutoString</code>) or 128 bytes (<code>nsAutoString</code>).</p>
-<p>An example:</p>
-<pre>class Foo {
-    ...
-
-    // bloats 'Foo' by 128 bytes!
-    nsAutoString mLocalName;
-};
-</pre>
-<h3 id="Raw_Character_Pointers" name="Raw_Character_Pointers">Raw Character Pointers</h3>
-<p><code>PromiseFlatString()</code> can be used to create a temporary buffer which holds a null-terminated buffer containing the same value as the source string. <code>PromiseFlatString()</code> will create a temporary buffer if necessary. This is most often used in order to pass an <code>nsAString</code> to an API which requires a null-terminated string.</p>
-<p>In the following example, an <code>nsAString</code> is combined with a literal string, and the result is passed to an API which requires a simple character buffer.</p>
-<pre class="eval">// Modify the URL and pass to AddPage(const PRUnichar* url)
-void AddModifiedPage(const nsAString&amp; url) {
-    NS_NAMED_LITERAL_STRING(httpPrefix, <span class="nowiki">"http://"</span>);
-    const nsAString&amp; modifiedURL = httpPrefix + url;
-
-    // creates a temporary buffer
-    AddPage(PromiseFlatString(modifiedURL).get());
-}
-</pre>
-<p><code>PromiseFlatString()</code> is smart when handed a string that is already null-terminated. It avoids creating the temporary buffer in such cases.</p>
-<pre class="eval">// Modify the URL and pass to AddPage(const PRUnichar* url)
-void AddModifiedPage(const nsAString&amp; url, PRBool addPrefix) {
-    if (addPrefix) {
-        // MUST create a temporary buffer - string is multi-fragmented
-        NS_NAMED_LITERAL_STRING(httpPrefix, <span class="nowiki">"http://"</span>);
-        AddPage(PromiseFlatString(httpPrefix + modifiedURL));
-    } else {
-        // MIGHT create a temporary buffer, does a runtime check
-        AddPage(PromiseFlatString(url).get());
-    }
-}
-</pre>
-<h3 id="printf_and_a_UTF-16_string" name="printf_and_a_UTF-16_string"><code>printf</code> and a UTF-16 string</h3>
-<p>For debugging, it's useful to <code>printf</code> a UTF-16 string (nsString, nsAutoString, nsXPIDLString, etc). To do this usually requires converting it to an 8-bit string, because that's what printf expects. However, on Windows, the following should work:</p>
-<pre class="eval">printf("%S\n", yourString.get());
-</pre>
-<p>(Note: I didn't test this. Also, I'm not sure what exactly this does to non-ASCII characters, especially when they are outside the system codepage). The reason that this doesn't work on Unix is because a wchar_t, which is what %S expects, is usually 4 bytes there (even when Mozilla is compiled with -fshort-wchar, because this would require libc to be compiled with -fshort-wchar).</p>
-<p>If non-ASCII characters aren't important, use:</p>
-<pre class="eval">printf("%s\n", NS_LossyConvertUTF16toASCII(yourString).get());
-</pre>
-<p>On platforms that use UTF-8 for console output (most Linux distributions), this works:</p>
-<pre class="eval">printf("%s\n", NS_ConvertUTF16toUTF8(yourString).get());
-</pre>
-<h2 id="IDL" name="IDL">IDL</h2>
-<p>The string library is also available through IDL. By declaring attributes and methods using the specially defined IDL types, string classes are used as parameters to the corresponding methods.</p>
-<h3 id="IDL_String_types" name="IDL_String_types">IDL String types</h3>
-<p>The C++ signatures follow the abstract-type convention described above, such that all method parameters are based on the <a href="#The_Abstract_Classes">abstract classes</a>. The following table describes the purpose of each string type in IDL.</p>
-<table class="standard-table">
- <tbody>
-  <tr>
-   <th class="header">IDL type</th>
-   <th class="header">C++ Type</th>
-   <th class="header">Purpose</th>
-  </tr>
-  <tr>
-   <td><code>string</code></td>
-   <td><code>char*</code></td>
-   <td>Raw character pointer to ASCII (7-bit) string, no string classes used. High bit is not guaranteed across XPConnect boundaries</td>
-  </tr>
-  <tr>
-   <td><code>wstring</code></td>
-   <td><code>PRUnichar*</code></td>
-   <td>Raw character pointer to UTF-16 string, no string classes used</td>
-  </tr>
-  <tr>
-   <td><code>AString</code></td>
-   <td><code>nsAString</code></td>
-   <td>UTF-16 string</td>
-  </tr>
-  <tr>
-   <td><code>ACString</code></td>
-   <td><code>nsACString</code></td>
-   <td>8-bit string, all bits are preserved across XPConnect boundaries</td>
-  </tr>
-  <tr>
-   <td><code>AUTF8String</code></td>
-   <td><code>nsACString</code></td>
-   <td>UTF-8 string - converted to UTF-16 as necessary when value is used across XPConnect boundaries</td>
-  </tr>
-  <tr>
-   <td><code>DOMString</code></td>
-   <td><code>nsAString</code></td>
-   <td>UTF-16 string used in the DOM. More or less the same as <code>AString</code>, but in JavaScript it has no distinction between whether the string is void or just empty. (not sure on this, looking for corrections.</td>
-  </tr>
- </tbody>
-</table>
-<h3 id="C.2B.2B_Signatures" name="C.2B.2B_Signatures">C++ Signatures</h3>
-<p>In IDL, <code>in</code> parameters are read-only, and the C++ signatures for <code>*String</code> parameters follows the above guidelines by using <code>const nsAString&amp;</code> for these parameters. <code>out</code> and <code>inout</code> parameters are defined simply as <code>nsAString</code> so that the callee can write to them.</p>
-<table class="standard-table">
- <tbody>
-  <tr>
-   <th class="header">IDL</th>
-   <th class="header">C++</th>
-  </tr>
-  <tr>
-   <td>
-    <pre class="eval">
-interface nsIFoo : nsISupports {
-
-    attribute AString utf16String;
-
-
-
-
-    AUTF8String getValue(in ACString key);
-
-};
-</pre>
-   </td>
-   <td>
-    <pre class="eval">
-class nsIFoo : public nsISupports {
-
-     NS_IMETHOD GetUtf16String(nsAString&amp;
-                               aResult) = 0;
-     NS_IMETHOD SetUtf16String(const nsAString&amp;
-                              aValue) = 0;
-
-     NS_IMETHOD GetValue(const nsACString&amp; aKey,
-                     nsACString&amp; aResult) = 0;
-};
-</pre>
-   </td>
-  </tr>
- </tbody>
-</table>
-<p>In the above example, <code>utf16String</code> is treated as a UTF-16 string. The implementation of <code>GetUtf16String()</code> will use <code>aResult.Assign</code> to "return" the value. In <code>SetUtf16String()</code> the value of the string can be used through a variety of methods including <a href="#Iterators">Iterators</a>, <code><a href="#Raw_Character_Pointers">PromiseFlatString</a></code>, and assignment to other strings.</p>
-<p>In <code>GetValue()</code>, the first parameter, <code>aKey</code>, is treated as a raw sequence of 8-bit values. Any non-ASCII characters in <code>aKey</code> will be preserved when crossing XPConnect boundaries. The implementation of <code>GetValue()</code> will assign a UTF-8 encoded 8-bit string into <code>aResult</code>. If the <code>this</code> method is called across XPConnect boundaries, such as from a script, then the result will be decoded from UTF-8 into UTF-16 and used as a Unicode value.</p>
-<h3 id="Choosing_a_string_type" name="Choosing_a_string_type">Choosing a string type</h3>
-<p>It can be difficult to determine the correct string type to use for IDL. The following points should help determine the appropriate string type.</p>
-<ul>
- <li>Using string classes may avoid new memory allocation for <code>out</code> parameters. For example, if the caller is using an <code>nsAutoString</code> to receive the value for an <code>out</code> parameter, (defined in C++ as simply <code>nsAString&amp;</code> then assignment of short (less than 64-characters) values to an <code>out</code> parameter will only copy the value into the <code>nsAutoString</code>'s buffer. Moreover, using the string classes allows for sharing of string buffers. In many cases, assigning from one string object to another avoids copying in favor of simply incrementing a reference count.</li>
- <li><code>in</code> strings using string classes often have their length pre-calculated. This can be a performance win.</li>
- <li>In cases where a raw-character buffer is required, <code>string</code> and <code>wstring</code> provide faster access than <code>PromiseFlatString</code>.</li>
- <li>UTF-8 strings defined with <code>AUTF8String</code> may need to be decoded when crossing XPConnect boundaries. This can be a performance hit. On the other hand, UTF-8 strings take up less space for strings that are commonly ASCII.</li>
- <li>UTF-16 strings defined with <code>wstring</code> or <code>AString</code> are fast when the unicode value is required. However, if the value is more often ASCII, then half of the storage space of the underlying string may be wasted.</li>
-</ul>
-<h2 id="Appendix_A_-_What_class_to_use_when" name="Appendix_A_-_What_class_to_use_when">Appendix A - What class to use when</h2>
-<p>This table provides a quick reference for what classes you should be using.</p>
-<table class="standard-table">
- <tbody>
-  <tr>
-   <th class="header">Context</th>
-   <th class="header">class</th>
-   <th class="header">Notes</th>
-  </tr>
-  <tr>
-   <td>Local Variables</td>
-   <td><code>nsAutoString<br>
-    nsCAutoString</code></td>
-   <td> </td>
-  </tr>
-  <tr>
-   <td>Class Member Variables</td>
-   <td><code>nsString<br>
-    nsCString</code></td>
-   <td> </td>
-  </tr>
-  <tr>
-   <td>Method Parameter types</td>
-   <td><code>nsAString<br>
-    nsACString</code></td>
-   <td>Use abstract classes for parameters. Use <code>const nsAString&amp;</code> for "in" parameters and <code>nsAString&amp;</code> for "out" parameters.</td>
-  </tr>
-  <tr>
-   <td>Retrieving "out" string/wstrings</td>
-   <td><code>nsXPIDLString<br>
-    nsXPIDLCString</code></td>
-   <td>Use <code>getter_Copies()</code>. Similar to <code>nsString / nsCString</code>.</td>
-  </tr>
-  <tr>
-   <td>Wrapping character buffers</td>
-   <td><code>nsDependentString<br>
-    nsDependentCString</code></td>
-   <td>Wrap <code>const char* / const PRUnichar*</code> buffers.</td>
-  </tr>
-  <tr>
-   <td>Literal strings</td>
-   <td><code>NS_LITERAL_STRING<br>
-    NS_LITERAL_CSTRING</code></td>
-   <td>Similar to <code>nsDependent[C]String</code>, but pre-calculates length at build time.</td>
-  </tr>
- </tbody>
-</table>
-<h2 id="Appendix_B_-_nsAString_Reference" name="Appendix_B_-_nsAString_Reference">Appendix B - nsAString Reference</h2>
-<p>Read-only methods.</p>
-<ul>
- <li><code><b>Length()</b></code></li>
- <li><code><b>IsEmpty()</b></code></li>
- <li><code><b>IsVoid()</b></code> - XPConnect will convert void nsAStrings to JavaScript <code>null</code>.</li>
- <li><code><b>BeginReading(
-  <i>
-   iterator</i>
-  )</b></code></li>
- <li><code><b>EndReading(
-  <i>
-   iterator</i>
-  )</b></code></li>
- <li><code><b>Equals(
-  <i>
-   string[, comparator]</i>
-  )</b></code></li>
- <li><code><b>First()</b></code></li>
- <li><code><b>Last()</b></code></li>
- <li><code><b>CountChar()</b></code></li>
- <li><code><b>Left(
-  <i>
-   outstring, length</i>
-  )</b></code></li>
- <li><code><b>Mid(
-  <i>
-   outstring, position, length</i>
-  )</b></code></li>
- <li><code><b>Right(
-  <i>
-   outstring, length</i>
-  )</b></code></li>
- <li><code><b>FindChar(
-  <i>
-   character</i>
-  )</b></code></li>
-</ul>
-<p>Methods that modify the string.</p>
-<ul>
- <li><code><b>Assign(
-  <i>
-   string</i>
-  )</b></code></li>
- <li><code><b>Append(
-  <i>
-   string</i>
-  )</b></code></li>
- <li><code><b>Insert(
-  <i>
-   string</i>
-  )</b></code></li>
- <li><code><b>Cut(
-  <i>
-   start, length</i>
-  )</b></code></li>
- <li><code><b>Replace(
-  <i>
-   start, length, string</i>
-  )</b></code></li>
- <li><code><b>Truncate(
-  <i>
-   length</i>
-  )</b></code></li>
- <li><code><b>SetIsVoid(
-  <i>
-   state</i>
-  )</b></code> - XPConnect will convert void nsAStrings to JavaScript <code>null</code>.</li>
- <li><code><b>BeginWriting(
-  <i>
-   iterator</i>
-  )</b></code></li>
- <li><code><b>EndWriting(
-  <i>
-   iterator</i>
-  )</b></code></li>
- <li><code><b>SetCapacity()</b></code></li>
-</ul>

+ 0 - 801
files/zh-cn/mozilla/tech/xpcom/guide/internal_strings/raw.html

@@ -1,801 +0,0 @@
-<p> </p>
-<h2 id="Preface" name="Preface">Preface</h2>
-<div>
- <p>by Alec Flett<br>
-  Thanks to David Baron for <a class="external" href="http://dbaron.org/mozilla/coding-practices">actual docs</a>,<br>
-  Peter Annema for lots of direction,<br>
-  Myk Melez for some more docs, and<br>
-  David Bradley for a diagram<br>
-  Revised by Darin Fisher for Mozilla 1.7<br>
-  Revised by Jungshik Shin to clarify character encoding issues</p>
-</div>
-<p>This guide will attempt to document the plethora of string classes, and hopefully provide an answer to the age old question, "what string class should I use here?"</p>
-<div style="border: solid thin steelblue; padding: 0.5em;">
- <p>If you are a Mozilla embedder or if you are writing an XPCOM component that will be distributed separately from the Mozilla code base, then this string guide is most likely not for you! Provided you are developing against Mozilla 1.7 or later, you should instead be using the new minimal <a href="https://dxr.mozilla.org/mozilla-central/source/xpcom/glue/nsStringAPI.h" rel="custom">Mozilla string API</a> and in particular the <a href="https://dxr.mozilla.org/mozilla-central/source/xpcom/string/public/nsEmbedString.h" rel="custom">nsEmbedString</a> class.</p>
-</div>
-<p>In a hurry? Go check out the <a href="cn/XPCOM/String_Quick_Reference">String Quick-Reference</a> (<a class="external" href="http://www.mozilla.org/projects/xpcom/string-quickref.html">). </a></p>
-<h2 id="Introduction" name="Introduction"><a class="external" href="http://www.mozilla.org/projects/xpcom/string-quickref.html">Introduction </a></h2>
-<p><a class="external" href="http://www.mozilla.org/projects/xpcom/string-quickref.html">The string classes are a library of C++ classes which are used to manage buffers of unicode and single-byte character strings. They reside in the mozilla codebase in the <code></code></a><code><a href="https://dxr.mozilla.org/mozilla-central/source/xpcom/string" rel="custom">xpcom/string</a></code> directory. </p>
-<p><a class="external" href="http://www.mozilla.org/projects/xpcom/string-quickref.html">Abstract (interface) classes begin with "nsA" and concrete classes simply begin with "ns". Classes with a "<code>CString</code>" in the name store 8-bit bytes (<code>char</code>'s) which may refer to single byte ASCII strings, or multibyte Unicode strings encoded in UTF-8 or a (multibyte or single byte) legacy character encoding (e.g. ISO-8859-1, Shift_JIS, GB2312, KOI8-R). All other classes simply have "<code>String</code>" in their name and refer to 16-bit strings made up of <code>PRUnichar</code>'s, For example: <code>nsAString</code> is an abstract class for storing Unicode characters in UTF-16 encoding, and <code>nsDependentCString</code> is a concrete class which stores a 8-bit string. Every 16-bit string class has an equivalent 8-bit string class. For example: <code>nsCString</code> is the 8-bit string class which corresponds to <code>nsString</code>. </a></p>
-<p><a class="external" href="http://www.mozilla.org/projects/xpcom/string-quickref.html">8-bit and 16-bit string classes have completely separate base classes, but share the same APIs. As a result, you cannot assign a 8-bit string to a 16-bit string without some kind of conversion helper class or routine. For the purpose of this document, we will refer to the 16-bit string classes in class documentation. It is safe to assume that every 16-bit class has an equivalent 8-bit class. </a></p>
-<h2 id="String_Guidelines" name="String_Guidelines"><a class="external" href="http://www.mozilla.org/projects/xpcom/string-quickref.html">String Guidelines </a></h2>
-<p><a class="external" href="http://www.mozilla.org/projects/xpcom/string-quickref.html">Follow these simple rules in your code to keep your fellow developers, reviewers, and users happy. </a></p>
-<ul>
- <li><a class="external" href="http://www.mozilla.org/projects/xpcom/string-quickref.html">Avoid </a><code><a href="#Unicode_Conversion_ns.2ACString_vs._ns.2AString">*WithConversion</a></code> functions at all costs: <code>AssignWithConversion</code>, <code>AppendWithConversion</code>, <code>EqualsWithConversion</code>, etc</li>
- <li>Use the most abstract string class that you can. Usually this is:
-  <ul>
-   <li><code><a href="#The_Abstract_Classes">nsAString</a></code> for function parameters</li>
-   <li><code><a href="#The_Concrete_Classes_-_which_classes_to_use_when">nsString</a></code> for member variables</li>
-   <li><a href="#The_Concrete_Classes_-_which_classes_to_use_when"><code>nsAutoString</code> or <code>nsXPIDLString</code></a> for local (stack-based) variables</li>
-  </ul>
- </li>
- <li>Use <a href="#Literal_Strings"><code>NS_LITERAL_[C]STRING</code> / <code>NS_NAMED_LITERAL_[C]STRING</code></a> to represent literal strings (i.e. "foo") as nsAString-compatible objects.</li>
- <li>Use <a href="#String_Concatenation">string concatenation</a> (i.e. the "+" operator) when combining strings.</li>
- <li>Use <code><a href="#Raw_Character_Pointers">nsDependentString</a></code> when you have a raw character pointer that you need to convert to an nsAString-compatible string.</li>
- <li>Use <code><a href="#Substrings_.28string_fragments.29">Substring()</a></code> to extract fragments of existing strings.</li>
- <li>Use <a href="#Iterators">iterators</a> to parse and extract string fragments.</li>
-</ul>
-<h2 id="The_Abstract_Classes" name="The_Abstract_Classes">The Abstract Classes</h2>
-<p>Every string class derives from <code>nsAString</code> (or <code>nsACString</code>). This class provides the fundamental interface for access and manipulation of strings. While concrete classes derive from <code>nsAString</code>, <code>nsAString</code> itself cannot be instantiated.</p>
-<p>This is very similar to the idea of an "interface" that mozilla uses to describe abstract object descriptions in the rest of the codebase. In the case of interfaces, class names begin with "nsI" where "I" refers to "Interface". In the case of strings, abstract classes begin with "nsA" and the "A" means "Abstract".</p>
-<p>There are a number of abstract classes which derive from <code>nsAString</code>. These abstract subclasses also cannot be instantiated, but they describe a string in slightly more detail than <code>nsAString</code>. They guarantee that the underlying implementation behind the abstract class provides specific capabilities above and beyond <code>nsAString</code>.</p>
-<p>The list below describes the main base classes. Once you are familiar with them, see the appendix describing What Class to Use When.</p>
-<ul>
- <li><b><code>nsAString</code></b>: the abstract base class for all strings. It provides an API for assignment, individual character access, basic manipulation of characters in the string, and string comparison. This class corresponds to the XPIDL <code>AString</code> parameter type.</li>
- <li><b><code>nsSubstring</code></b>: the common base class for all of the string classes. Provides optimized access to data within the string. A <code>nsSubstring</code> is not necessarily null-terminated. (For backwards compatibility, <code>nsASingleFragmentString</code> is a typedef for this string class.)</li>
- <li><b><code>nsString</code></b>: builds on <code>nsSubstring</code> by guaranteeing a null-terminated storage. This allows for a method (<code>.get()</code>) to access the underlying character buffer. (For backwards compatibility, <code>nsAFlatString</code> is a typedef for this string class.)</li>
-</ul>
-<p>The remainder of the string classes inherit from either <code>nsSubstring</code> or <code>nsString</code>. Thus, every string class is compatible with <code>nsAString</code>.</p>
-<p>It's important to note that <code>nsSubstring</code> and <code>nsAString</code> both represent a contiguous array of characters that are not necessarily null-terminated. One might ask then ask why two different yet similar string classes need to exist. Well, <code>nsSubstring</code> exists primarily as an optimization since <code>nsAString</code> must retain binary compatibility with the frozen <code>nsAString</code> class that shipped with Mozilla 1.0. Up until the release of Mozilla 1.7, <code>nsAString</code> was capable of representing a string broken into multiple fragments. The cost associated with supporting multi-fragment strings was high and offered limited benefits. It was decided to eliminate support for multi-fragment strings in an effort to reduce the complexity of the string classes and improve performance. See <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=231995" title='FIXED: Exploring nsAString "defragmentation"'>bug 231995</a> for more details.</p>
-<p>Though <code>nsSubstring</code> provides a more efficient interface to its underlying buffer than <code>nsAString</code>, <code>nsAString</code> is still the most commonly used class for parameter passing. This is because it is the string class corresponding to <code>AString</code> in XPIDL. Therefore, this string guide will continue to discuss the string classes with an emphasis on <code>nsAString</code>.</p>
-<p>Since every string derives from <code>nsAString</code> (or <code>nsACString</code>), they all share a simple API. Common read-only methods:</p>
-<ul>
- <li><b><code>.Length()</code></b> - the number of code units (bytes for 8-bit string classes and PRUnichar's for 16-bit string classes) in the string.</li>
- <li><b><code>.IsEmpty()</code></b> - the fastest way of determining if the string has any value. Use this instead of testing <code>string.Length</code> == 0</li>
- <li><b><code>.Equals(string)</code></b> - TRUE if the given string has the same value as the current string.</li>
-</ul>
-<p>Common methods that modify the string:</p>
-<ul>
- <li><b><code>.Assign(string)</code></b> - Assigns a new value to the string.</li>
- <li><b><code>.Append(string)</code></b> - Appends a value to the string.</li>
- <li><b><code>.Insert(string, position)</code></b> - Inserts the given string before the code unit at position.</li>
- <li><b><code>.Truncate(length)</code></b> - shortens the string to the given length.</li>
-</ul>
-<p>Complete documentation can be found in the <a href="#Appendix_B_-_nsAString_Reference">Appendix</a>.</p>
-<h3 id="Read-only_strings" name="Read-only_strings">Read-only strings</h3>
-<p>The <code>const</code> attribute on a string determines if the string is writable. If a string is defined as a <code>const nsAString</code> then the data in the string cannot be manipulated. If one tries to call a non-<code>const</code> method on a <code>const</code> string the compiler will flag this as an error at build time.</p>
-<p>For example:</p>
-<pre class="eval">void nsFoo::ReverseCharacters(nsAString&amp; str) {
-      ...
-     str.Assign(reversedStr); // modifies the string
-}
-</pre>
-<p>This should not compile, because you're assigning to a <code>const</code> class:</p>
-<pre class="eval">void nsFoo::ReverseCharacters(const nsAString&amp; str) {
-      ...
-     <b>str.Assign(reversedStr);</b>
-}
-</pre>
-<h3 id="As_function_parameters" name="As_function_parameters">As function parameters</h3>
-<p>It is recommended that you use the most abstract interface possible as a function parameter, instead of using concrete classes. The convention is to use C++ references (the '&amp;' character) instead of pointers (the '*' character) when passing string references around. For example:</p>
-<pre class="eval">// abstract reference
-nsFoo::PrintString(<b>const nsAString&amp;</b> str) {..}
-
-// using a concrete class!
-nsFoo::PrintString(<b>const nsString&amp;</b> str) {..}
-
-// using a pointer!
-nsFoo::PrintString(<b>const nsAString*</b> str) {..}
-</pre>
-<p>The abstract classes are also sometimes used to store temporary references to objects. You can see both of these uses in <a href="#Common_Patterns">Common Patterns</a>, below.</p>
-<p><b>NOTE:</b> While using abstract string classes increases the re-usability of your methods, it also incurs a codesize and performance penalty. Therefore, when writing methods that will only ever be used within the confines of your source file or module, it is better to use <b><code>const nsSubstring&amp;</code></b> for input parameters and <b><code>nsString&amp;</code></b> for output parameters. --Darin</p>
-<h2 id="The_Concrete_Classes_-_which_classes_to_use_when" name="The_Concrete_Classes_-_which_classes_to_use_when">The Concrete Classes - which classes to use when</h2>
-<p>The concrete classes are for use in code that actually needs to store string data. The most common uses of the concrete classes are as local variables, and members in classes or structs. Whereas the abstract classes differ in storage mechansim, for the most part the concrete classes differ in storage policy.</p>
-<p>The following is a list of the most common concrete classes. Once you are familiar with them, see the appendix describing <a href="#Appendix_A_-_What_class_to_use_when">What Class to Use When.</a></p>
-<ul>
- <li><code><b>nsString / nsCString</b></code>- a null-terminated string whose buffer is allocated on the heap. Destroys its buffer when the string object goes away.</li>
- <li><code><b>nsAutoString / nsCAutoString</b></code>- derived from <code>nsString</code>, a string which owns a 64 code unit buffer in the same storage space as the string itself. If a string less than 64 code units is assigned to an <code>nsAutoString</code>, then no extra storage will be allocated. For larger strings, a new buffer is allocated on the heap.</li>
- <li><code><b>nsXPIDLString / nsXPIDLCString</b></code>- derived from <code>nsString</code>, this class supports the <code>getter_Copies()</code> operator which allows easy access to XPIDL <code>out wstring / string</code> parameters. This class also supports the notion of a null-valued buffer, whereas <code>nsString</code>'s buffer is never null.</li>
- <li><code><b>nsDependentString</b></code>- derived from <code>nsString</code>, this string does
-  <i>
-   not</i>
-  own its buffer. It is useful for converting a raw string (<code>const PRUnichar*</code> or <code>const char*</code>) into a class of type <code>nsAString</code>.</li>
- <li><code><b>nsPrintfCString</b></code>- derived from <code>nsCString</code>, this string behaves like an <code>nsCAutoString</code>. The constructor takes parameters which allows it to construct a 8-bit string from a <code>printf</code>-style format string and parameter list.</li>
- <li><code><b>NS_LITERAL_STRING/NS_NAMED_LITERAL_STRING</b></code>- these convert a literal string (such as "abc") to a <code>nsString</code> or a subclass of <code>nsString</code>. On platforms supporting double-byte string literals (e.g., MSVC++ or GCC with the -fshort-wchar option), these are simply macros around the <code>nsDependentString</code> class. They are slightly faster than just wrapping them with an <code>nsDependentString</code> because they use the compiler to calculate their length, and they also hide the messy cross-platform details of non-byte literal strings.</li>
-</ul>
-<p>There are also a number of concrete classes that are created as a side-effect of helper routines, etc. You should avoid direct use of these classes. Let the string library create the class for you.</p>
-<ul>
- <li><code><b>nsSubstringTuple</b></code> - created via <a href="#String_Concatenation">string concatenation</a></li>
- <li><code><b>nsDependentSubstring</b></code> - created through <a href="#Substrings_.28string_fragments.29">Substring</a></li>
- <li><code><b>nsPromiseFlatString</b></code> - created through <code><b><a href="#Raw_Character_Pointers">PromiseFlatString()</a></b></code></li>
-</ul>
-<p>Of course, there are times when it is necessary to reference these string classes in your code, but as a general rule they should be avoided.</p>
-<h2 id="Iterators" name="Iterators">Iterators</h2>
-<p>Iterators are objects that retain a reference to a position in a string. In some ways they are like a number which refers to an index in an array, or a character-pointer that refers to a position in a character string. They also provide a syntactic means to distinguish between reading and writing to a string.</p>
-<p>Iterators are most often used to extract substrings of a string. They provide the capability to modify the contents of a string, but often helper routines, or the string's own methods are quicker at complex string transformations.</p>
-<p>Iterators are declared from the string class which they are iterating:</p>
-<pre class="eval">nsAString::const_iterator start, end; // reading-only iterators for nsAString
-nsString::iterator substr_start, substr_end; // writing iterators for nsString
-</pre>
-<p>Iterators are initialized with one of 4 methods on the string you wish to reference:</p>
-<pre class="eval">// let's read from 'str'
-str.BeginReading(start); // initialize 'start' to the beginning of 'str'
-str.EndReading(end); // 'end' will be at the end of the string
-
-// say we also want to write to 'url'
-url.BeginWriting(substr_start);
-url.EndWriting(substr_end);
-</pre>
-<p>You can access the code unit that an iterator points to with the dereference operator *.</p>
-<pre class="eval">if (*start == '[')
-     printf("Starts with a bracket\n");
-</pre>
-<p>Note in the above examples, that '<code>end</code>' and '<code>substr_end</code>' will actually point to the code unit past the end of the string, so you should never dereference the direct result of <code>.EndReading()</code>.</p>
-<p>You can test if two iterators point to the same position with == or !=. You can advance iterators with ++. Putting the ++ before your iterator is preferred, and will prevent creation of a temporary iterator.</p>
-<pre class="eval">while (start != end) // iterate through the whole string
-     ++start;
-</pre>
-<p>You can effectively write to a string with writing iterators (as opposed to const-iterators):</p>
-<pre class="eval">// change all * to !
-while (substr_start != substr_end) {
-     if (*substr_start == '*')
-           *substr_start = '!';
-     ++substr_start;
-}
-</pre>
-<p>With the patch for <a class="external" href="http://bugzilla.mozilla.org/show_bug.cgi?id=231995">bug 231995</a>, this loop is now as efficient as iterating with raw character pointers.</p>
-<h2 id="Helper_Classes_and_Functions" name="Helper_Classes_and_Functions">Helper Classes and Functions</h2>
-<h3 id="Searching_strings_-_looking_for_substrings.2C_characters.2C_etc." name="Searching_strings_-_looking_for_substrings.2C_characters.2C_etc.">Searching strings - looking for substrings, characters, etc.</h3>
-<p><code>FindInReadable()</code> is the replacement for the old <code>string.Find(..)</code>. The syntax is:</p>
-<pre class="eval">PRBool FindInReadable(const nsAString&amp; pattern,
-                      nsAString::const_iterator start, nsAString::const_iterator end,
-                      nsStringComparator&amp; aComparator = nsDefaultStringComparator());
-</pre>
-<p>To use this, <code>start</code> and <code>end</code> should point to the beginning and end of a string that you would like to search. If the search string is found, <code>start</code> and <code>end</code> will be adjusted to point to the beginning and end of the found pattern. The return value is PR_TRUE or PR_FALSE, indicating whether or not the string was found.</p>
-<p>An example:</p>
-<pre class="eval">const nsAString&amp; str = GetSomeString();
-nsAString::const_iterator start, end;
-
-str.BeginReading(start);
-str.EndReading(end);
-
-NS_NAMED_LITERAL_STRING(valuePrefix, "value=");
-
-if (FindInReadable(valuePrefix, start, end)) {
-    // end now points to the character after the pattern
-    valueStart = end;
-
-}
-</pre>
-<h3 id="Memory_Allocation_-_how_to_avoid_it.2C_which_methods_to_use" name="Memory_Allocation_-_how_to_avoid_it.2C_which_methods_to_use">Memory Allocation - how to avoid it, which methods to use</h3>
-<p>The preferred method to allocate a new character buffer (<code>PRUnichar*</code>/<code>char*</code>) from an existing string is with one of the following methods:</p>
-<ul>
- <li><code><b>PRUnichar* ToNewUnicode(
-  <i>
-   nsAString&amp;</i>
-  )</b></code> - Allocates a <code>PRUnichar*</code>buffer from an <code>nsAString</code>.</li>
- <li><code><b>char *ToNewCString(
-  <i>
-   nsACString&amp;</i>
-  )</b></code> - Allocates a <code>char*</code>buffer from an <code>nsACString</code>. Note that this method will also work on nsAStrings, but it will do an implicit <a href="#Lossy_Conversion">lossy conversion</a>. This function should only be used if the input is known to be strictly ASCII. Often a conversion to UTF-8 is more appropriate. See <code><b>ToNewUTF8String</b></code> below.</li>
- <li><code><b>char* ToNewUTF8String(
-  <i>
-   nsAString&amp;</i>
-  )</b></code> - Allocates a new <code>char*</code> buffer containing the UTF-8 encoded version of the given nsAString. See <a href="#Unicode_Conversion_ns.2ACString_vs._ns.2AString">Unicode Conversion</a> for more details.</li>
-</ul>
-<p>These methods return a buffer allocated using XPCOM's allocator (<code>nsMemory::Alloc</code>) instead of the traditional allocator (<code>malloc</code>, etc.). You should use <code>nsMemory::Free</code> to deallocate the result when you no longer need it.</p>
-<h3 id="Substrings_.28string_fragments.29" name="Substrings_.28string_fragments.29">Substrings (string fragments)</h3>
-<p>It is very simple to refer to a substring of an existing string without actually allocating new space and copying the characters into that substring. <code>Substring()</code> is the preferred method to create a reference to such a string.</p>
-<pre class="eval">void ProcessString(const nsAString&amp; str) {
-    const nsAString&amp; firstFive = Substring(str, 0, 5);
-    // firstFive is now a string representing the first 5 characters
-}
-</pre>
-<h2 id="Unicode_Conversion_ns.2ACString_vs._ns.2AString" name="Unicode_Conversion_ns.2ACString_vs._ns.2AString">Unicode Conversion ns*CString vs. ns*String</h2>
-<p>Strings can be
- <i>
-  stored</i>
- in two basic formats: 8-bit code unit (byte/<code>char</code>) strings, or 16-bit code unit (<code>PRUnichar</code>) strings. Any string class with a capital "C" in the classname contains 8-bit bytes. These classes include <code>nsCString</code>, <code>nsDependentCString</code>, and so forth. Any string class
- <i>
-  without</i>
- the "C" contains 16-bit code units.</p>
-<p>A 8-bit string can be in one of many character encodings while a 16-bit string is always in UTF-16. The most common encodings are:</p>
-<ul>
- <li>ASCII - 8-bit encoding for basic English-only strings. Each ASCII value is stored in exactly one byte in the array.</li>
- <li><a class="external" href="http://www.unicode.org/glossary/#UCS_2">UCS2</a> - 16-bit encoding for a
-  <i>
-   subset</i>
-  of Unicode, <a class="external" href="http://www.unicode.org/glossary/#BMP">BMP</a>. The Unicode value of a character stored in UCS2 is stored in exactly one 16-bit <code>PRUnichar</code> in a string class.</li>
- <li><a class="external" href="http://www.faqs.org/rfcs/rfc3629.html">UTF-8</a> - 8-bit encoding for Unicode characters. Each Unicode characters is stored in up to 4 bytes in a string class. UTF-8 is capable of representing the entire Unicode character repertoire, and it efficiently maps to <a class="external" href="http://www.unicode.org/glossary/#UTF_32">UTF-32</a>.</li>
- <li><a class="external" href="http://www.unicode.org/glossary/#UTF_16">UTF-16</a> - 16-bit encoding for Unicode storage, backwards compatible with UCS2. The Unicode value of a character stored in UTF-16 may require
-  <i>
-   one or two</i>
-  16-bit <code>PRUnichar</code>s in a string class. The contents of <code>nsAString</code> always has to be regarded as in this encoding instead of UCS2. UTF-16 is capable of representing the entire Unicode character repertoire, and it efficiently maps to UTF-32. (Win32 W APIs and Mac OS X natively use UTF-16.)</li>
-</ul>
-<p>In addition, there are literally hundreds of encodings that are provided by internationalization libraries. Access to these libraries may be part of the application (such as <code>nsICharsetConversionManager</code> in Mozilla) or built into the operating system (such as <code>iconv()</code> in UNIX operating systems and <code>MultiByteToWideChar</code>/<code>WideCharToMultiByte</code> on Windows).</p>
-<p>When working with existing code, it is important to examine the current usage of the strings that you are manipulating, to determine the correct conversion mechanism.</p>
-<p>When writing new code, it can be confusing to know which storage class and encoding is the most appropriate. There is no single answer to this question, but there are a few important guidelines:</p>
-<ul>
- <li><b>Is the string always ASCII?</b> First and foremost, you need to determine what kinds of values will be stored in the string. If the strings are always internal, ASCII strings such as "left", "true", "background" and so forth, then straight C-strings are probably the way to go.</li>
- <li><b>If the string is ASCII, will it be compared to, assigned to, or otherwise interact with non-ASCII strings?</b> When assigning or comparing an 8-bit ASCII value (in)to a 16-bit UCS2 string, an "inflation" needs to happen at runtime. If your strings are small enough (say, less than 64 bytes) then it may make sense to store your string in a 16-bit unicode class as well, to avoid the extra conversion. The tradeoff is that your ASCII string takes up twice as much space as a 16-bit Unicode string than it would as an 8-bit string.</li>
- <li><b>Is the string usually ASCII, but needs to support unicode?</b> If your string is most often ASCII but needs to be able to store Unicode characters, then UTF-8 may be the right encoding. ASCII characters will still be stored in 8-bit storage but other Unicode characters will take up 2 to 4 bytes. However if the string ever needs to be compared or assigned to a 16-bit string, a runtime conversion will be necessary.</li>
- <li><b>Are you storing large strings of non-ASCII data?</b> Up until this point, UTF-8 might seem like the ideal encoding. The drawback is that for most non-European characters (such as Chinese, Indian and Japanese) in BMP, UTF-8 takes 50% more space than UTF-16. For characters in plane 1 and above, both UTF-8 and UTF-16 take 4 bytes.</li>
- <li><b>Do you need to manipulate the contents of a Unicode string?</b> One problem with encoding Unicode characters in UTF-8 or other 8-bit storage formats is that the actual Unicode character can span multiple bytes in a string. In most encodings, the actual number of bytes varies from character to character. When you need to iterate over each character, you must take the encoding into account. This is vastly simplified when iterating 16-bit strings because each 16-bit code unit (<code>PRUnichar</code>) corresponds to a Unicode character as long as all characters are in BMP, which is often the case. However, you have to keep in mind that a single Unicode character in plane 1 and beyond is represented in two 16-bit code units in 16-bit strings so that the number of <code>PRUnichar</code>'s is
-  <i>
-   not</i>
-  always equal to the number of Unicode characters. For the same reason, the position and the index in terms of 16-bit code units are not always the same as the position and the index in terms of Unicode characters.</li>
-</ul>
-<p><br>
- To assist with ASCII, UTF-8, and UTF-16 conversions, there are some helper methods and classes. Some of these classes look like functions, because they are most often used as temporary objects on the stack.</p>
-<h3 id="UTF-8_.2F_UTF-16_conversion" name="UTF-8_.2F_UTF-16_conversion">UTF-8 / UTF-16 conversion</h3>
-<p><code><b>NS_ConvertUTF8toUTF16(
- <i>
-  const nsACString&amp;</i>
- )</b></code> - a <code>nsAutoString</code> subclass that converts a UTF-8 encoded <code>nsACString</code> or <code>const char*</code> to a 16-bit UTF-16 string. If you need a <code>const PRUnichar*</code> buffer, you can use the <code>.get()</code> method. For example:</p>
-<pre class="eval">/* signature: void HandleUnicodeString(const nsAString&amp; str); */
-object-&gt;HandleUnicodeString(<b>NS_ConvertUTF8toUTF16</b>(utf8String));
-
-/* signature: void HandleUnicodeBuffer(const PRUnichar* str); */
-object-&gt;HandleUnicodeBuffer(<b>NS_ConvertUTF8toUTF16</b>(utf8String).get());
-</pre>
-<p><code><b>NS_ConvertUTF16toUTF8(
- <i>
-  const nsAString&amp;</i>
- )</b></code> - a <code>nsCAutoString</code> which converts a 16-bit UTF-16 string (<code>nsAString</code>) to a UTF-8 encoded string. As above, you can use <code>.get()</code> to access a <code>const char*</code> buffer.</p>
-<pre class="eval">/* signature: void HandleUTF8String(const nsACString&amp; str); */
-object-&gt;HandleUTF8String(<b>NS_ConvertUTF16toUTF8</b>(utf16String));
-
-/* signature: void HandleUTF8Buffer(const char* str); */
-object-&gt;HandleUTF8Buffer(<b>NS_ConvertUTF16toUTF8</b>(utf16String).get());
-</pre>
-<p><code><b>CopyUTF8toUTF16(
- <i>
-  const nsACString&amp;, nsAString&amp;</i>
- )</b></code> - converts and copies:</p>
-<pre class="eval">// return a UTF-16 value
-void Foo::GetUnicodeValue(nsAString&amp; result) {
-    <b>CopyUTF8toUTF16</b>(mLocalUTF8Value, result);
- }
-</pre>
-<p><code><b>AppendUTF8toUTF16(
- <i>
-  const nsACString&amp;, nsAString&amp;</i>
- )</b></code> - converts and appends:</p>
-<pre class="eval">// return a UTF-16 value
-void Foo::GetUnicodeValue(nsAString&amp; result) {
-    result.AssignLiteral("prefix:");
-    <b>AppendUTF8toUTF16</b>(mLocalUTF8Value, result);
-}
-</pre>
-<p><br>
- <code><b>UTF8ToNewUnicode(
- <i>
-  const nsACString&amp;, PRUint32* aUTF16Count = nsnull</i>
- )</b></code> - allocates and converts (the optional parameter will contain the number of 16-byte units upon return, if non-null):</p>
-<pre class="eval">void Foo::GetUTF16Value(PRUnichar** result) {
-    *result = <b>UTF8ToNewUnicode</b>(mLocalUTF8Value);
-}
-</pre>
-<p><br>
- <code><b>CopyUTF16toUTF8(
- <i>
-  const nsAString&amp;, nsACString&amp;</i>
- )</b></code> - converts and copies:</p>
-<pre class="eval">// return a UTF-8 value
-void Foo::GetUTF8Value(nsACString&amp; result) {
-    <b>CopyUTF16toUTF8</b>(mLocalUTF16Value, result);
-}
-</pre>
-<p><code><b>AppendUTF16toUTF8(
- <i>
-  const nsAString&amp;, nsACString&amp;</i>
- )</b></code> - converts and appends:</p>
-<pre class="eval">// return a UTF-8 value
-void Foo::GetUnicodeValue(nsACString&amp; result) {
-    result.AssignLiteral("prefix:");
-    <b>AppendUTF16toUTF8</b>(mLocalUTF16Value, result);
-}
-</pre>
-<p><code><b>ToNewUTF8String(
- <i>
-  const nsAString&amp;</i>
- )</b></code> - allocates and converts:</p>
-<pre class="eval">void Foo::GetUTF8Value(char** result) {
-    *result = <b>ToNewUTF8String</b>(mLocalUTF16Value);
-}
-</pre>
-<h3 id="Lossy_Conversion" name="Lossy_Conversion">Lossy Conversion</h3>
-<p>The following should only be used when you can guarantee that the original string is ASCII. These helpers are very similar to the UTF-8 / UTF-16 conversion helpers above.</p>
-<h4 id="UTF-16_to_ASCII_converters" name="UTF-16_to_ASCII_converters">UTF-16 to ASCII converters</h4>
-<p>These converters are
- <i>
-  <b>very dangerous</b></i>
- because they
- <i>
-  <b>lose information</b></i>
- during the conversion process. You should
- <i>
-  <b>avoid UTF-16 to ASCII conversions</b></i>
- unless your strings are guaranteed to be ASCII. Each 16-bit code unit in 16-bit string is simply cast to an 8-bit byte, which means all Unicode character values above 0xFF are converted to an arbitrary 8-bit byte.</p>
-<ul>
- <li><code><b>NS_LossyConvertUTF16toASCII(
-  <i>
-   nsAString</i>
-  )</b></code> - a <code>nsCAutoString</code> which holds a temporary buffer containing the deflated value of the string.</li>
- <li><code><b>LossyCopyUTF16toASCII(
-  <i>
-   nsAString, nsACString</i>
-  )</b></code> - does an in-place conversion from UTF-16 into an ASCII string object.</li>
- <li><code><b>LossyAppendUTF16toASCII(
-  <i>
-   nsAString, nsACString</i>
-  )</b></code> - appends an UTF-16 string to an ASCII string, losing non-ASCII values.</li>
- <li><code><b>ToNewCString(
-  <i>
-   nsAString</i>
-  )</b></code> - allocates a new <code>char*</code> string.</li>
-</ul>
-<h4 id="ASCII_to_UTF-16_converters" name="ASCII_to_UTF-16_converters">ASCII to UTF-16 converters</h4>
-<p>These converters are
- <i>
-  <b>very dangerous</b></i>
- because they will
- <i>
-  <b>mangle any non-ASCII string</b></i>
- into a meaningless UTF-16 string. You should
- <i>
-  <b>avoid ASCII to UTF-16 conversions</b></i>
- unless your strings are guaranteed to be ASCII. For instance, if you have an 8-bit string encoded in a multibyte character encoding, each byte of the string will be "inflated" to a 16-bit number by simple casting.</p>
-<p>For example, imagine a UTF-8 string where the first Unicode character of the string is represented with a 3-byte UTF-8 sequence, the "inflated" UTF-16 string will contain the 3 <code>PRUnichar</code>'s instead of the single <code>PRUnichar</code> that represents the first character. These <code>PRUnichar</code>'s have nothing to do with the first Unicode character in the UTF-8 string.</p>
-<ul>
- <li><code><b>NS_ConvertASCIItoUTF16(
-  <i>
-   nsACString</i>
-  )</b></code> - a <code>nsAutoString</code> which holds a temporary buffer containing the inflated value of the string.</li>
- <li><code><b>CopyASCIItoUTF16(
-  <i>
-   nsACString, nsAString</i>
-  )</b></code> - does an in-place conversion from one string into a Unicode string object.</li>
- <li><code><b>AppendASCIItoUTF16(
-  <i>
-   nsACString, nsAString</i>
-  )</b></code> - appends an ASCII string to a Unicode string.</li>
- <li><code><b>ToNewUnicode(
-  <i>
-   nsACString</i>
-  )</b></code> - Creates a new <code>PRUnichar*</code> string which contains the inflated value.</li>
-</ul>
-<h2 id="Common_Patterns" name="Common_Patterns">Common Patterns</h2>
-<h3 id="Callee-allocated_Parameters" name="Callee-allocated_Parameters">Callee-allocated Parameters</h3>
-<p>Many APIs result in a method allocating a buffer in order to return strings to its caller. This can be tricky because the caller has to remember to free the string when they have finished using it. Fortunately, the <code>nsXPIDLString</code> class makes this very easy.</p>
-<p>A method may look like this:</p>
-<pre class="eval">void GetValue(PRUnichar** aValue)
-{
-    *aValue = ToNewUnicode(foo);
-}
-</pre>
-<p>Without the string classes, the caller would need to free the string:</p>
-<pre>{
-    PRUnichar* val;
-    GetValue(&amp;val);
-
-    if (someCondition) {
-        // don't forget to free the value!
-        nsMemory::Free(val);
-        return NS_ERROR_FAILURE;
-    }
-
-    ...
-    // and later, still don't forget to free!
-    nsMemory::Free(val);
-}
-</pre>
-<p>With <code>nsXPIDLString</code> you never have to worry about this. You can just use <code>getter_Copies()</code> to wrap the string class, and the class will remember to free the buffer when it goes out of scope:</p>
-<pre>{
-    nsXPIDLString val;
-    GetValue(getter_Copies(val));
-
-    // val will free itself here
-    if (someCondition)
-        return NS_ERROR_FAILURE;
-    ...
-    // and later, still nothing to free
-}
-</pre>
-<p>The resulting code is much simpler, and easy to read.</p>
-<h3 id="Literal_Strings" name="Literal_Strings">Literal Strings</h3>
-<p>A
- <i>
-  literal string</i>
- is a raw string value that is written in some C++ code. For example, in the statement <code>printf("Hello World\n");</code> the value <code>"Hello World\n"</code> is a literal string. It is often necessary to insert literal string values when an <code>nsAString</code> or <code>nsACString</code> is required. These four macros will provide you with the necessary conversion:</p>
-<ul>
- <li><code><b>NS_LITERAL_CSTRING(
-  <i>
-   literal string</i>
-  )</b></code> - a temporary <code>nsCString</code></li>
- <li><code><b>NS_NAMED_LITERAL_CSTRING(
-  <i>
-   variable,literal string</i>
-  )</b></code> - declares a <code>nsCString</code> variable named
-  <i>
-   variable</i>
- </li>
- <li><code><b>NS_LITERAL_STRING(
-  <i>
-   literal string</i>
-  )</b></code> - a temporary <code>nsString</code> with the unicode version of
-  <i>
-   literal string</i>
- </li>
- <li><code><b>NS_NAMED_LITERAL_STRING(
-  <i>
-   variable,literal string</i>
-  )</b></code> - declares a <code>nsString</code> variable named
-  <i>
-   variable</i>
-  with the unicode version of
-  <i>
-   literal string</i>
- </li>
-</ul>
-<p>The purpose of the <code>CSTRING</code> versions of these macros may seem unnecessary, given that <code>nsDependentCString</code> will also wrap a string value in an <code>nsCString</code>. The advantage to these macros is that the length of these strings is calculated at compile time, so the string does not need to be scanned at runtime to determine its length.</p>
-<p>The <code>STRING</code> versions of these macros provide a portable way of declaring UTF-16 versions of the given literal string, avoiding runtime conversion on platforms which support literal UTF-16 strings (e.g., MSVC++ and GCC with the -fshort-wchar option).</p>
-<pre>// call Init(const PRUnichar*)
-Init(L"start value"); // bad - L"..." is not portable!
-Init(NS_ConvertASCIItoUTF16("start value").get()); // bad - runtime ASCII-&gt;UTF-16 conversion!
-
-// call Init(const nsAString&amp;)
-Init(nsDependentString(L"start value")); // bad - not portable!
-Init(NS_ConvertASCIItoUTF16("start value")); // bad - runtime ASCII-&gt;UTF-16 conversion!
-
-// call Init(const nsACString&amp;)
-Init(nsDependentCString("start value")); // bad - length determined at runtime
-</pre>
-<p>Here are some examples of proper <code>NS_LITERAL_[C]STRING</code> usage.</p>
-<pre>// call Init(const PRUnichar*)
-Init(NS_LITERAL_STRING("start value").get());
-
-// call Init(const nsAString&amp;)
-Init(NS_LITERAL_STRING("start value"));
-
-// call Init(const nsACString&amp;)
-Init(NS_LITERAL_CSTRING("start value"));
-</pre>
-<p>There are a few details which can be useful in tracking down issues with these macros:</p>
-<p><code>NS_LITERAL_STRING</code> does compile-time conversion to UTF-16 on some platforms (e.g. Windows, Linux, and Mac) but does runtime conversion on other platforms. By using <code>NS_LITERAL_STRING</code> your code is guaranteed to use the best possible conversion for the platform in question.</p>
-<p>Because some platforms do runtime conversion, the use of literal string concatenation inside a <code>NS_LITERAL_STRING/NS_NAMED_LITERAL_STRING</code> macro will compile on these platforms, but not on platforms which support compile-time conversion.</p>
-<p>For example:</p>
-<pre>// call Init(nsAString&amp;)
-Init(NS_LITERAL_STRING("start "
-     "value")); // only compiles on some platforms
-</pre>
-<p>The reason for this is that on some platforms, the <code>L"..."</code> syntax is used, but it is only applied to the first string in the concatenation (<code>"start "</code>). When the compiler attempts to concatenate this with the non-Unicode string <code>"value"</code> it gets confused.</p>
-<p>Also, using preprocessor macros as the string literal is unsupported:</p>
-<pre>#define some_string "See Mozilla Run"
-...
-Init(NS_LITERAL_STRING( some_string )); // only compiles on some platforms/with some compilers.
-
-</pre>
-<h3 id="String_Concatenation" name="String_Concatenation">String Concatenation</h3>
-<p>Strings can be concatenated together using the + operator. The resulting string is a <code>const nsSubstringTuple</code> object. The resulting object can be treated and referenced similarly to a <code>nsAString</code> object. Concatenation
- <i>
-  does not copy the substrings</i>
- . The strings are only copied when the concatenation is assigned into another string object. The <code>nsSubstringTuple</code> object holds pointers to the original strings. Therefore, the <code>nsSubstringTuple</code> object is dependent on all of its substrings, meaning that their lifetime must be at least as long as the <code>nsSubstringTuple</code> object.</p>
-<p>For example, you can use the value of two strings and pass their concatenation on to another function which takes an <code>const nsAString&amp;:</code></p>
-<pre class="eval">void HandleTwoStrings(const nsAString&amp; one, const nsAString&amp; two) {
-    // call HandleString(const nsAString&amp;)
-    HandleString(one + two);
-}
-</pre>
-<p>NOTE: The two strings are implicitly combined into a temporary <code>nsString</code> in this case, and the temporary string is passed into <code>HandleString</code>. If <code>HandleString</code> assigns its input into another <code>nsString</code>, then the string buffer will be shared in this case negating the cost of the intermediate temporary. You can concatenate N strings and store the result in a temporary variable:</p>
-<pre class="eval">NS_NAMED_LITERAL_STRING(start, "start ");
-NS_NAMED_LITERAL_STRING(middle, "middle ");
-NS_NAMED_LITERAL_STRING(end, "end");
-// create a string with 3 dependent fragments - no copying involved!
-nsString combinedString = start + middle + end;
-
-// call void HandleString(const nsAString&amp;);
-HandleString(combinedString);
-</pre>
-<p>If you are using <code>NS_LITERAL_STRING</code> to create a temporary that is only used once, then it is safe to define it inside a concatenation because the string buffer will live as long as the temporary concatenation object (of type <code>nsSubstringTuple</code>).</p>
-<pre class="eval">// call HandlePage(const nsAString&amp;);
-// safe because the concatenated-string will live as long as its substrings
-HandlePage(NS_LITERAL_STRING("start ") + NS_LITERAL_STRING("end"));
-</pre>
-<h3 id="Local_variables" name="Local_variables">Local variables</h3>
-<p>Local variables within a function are usually stored on the stack. The <code>nsAutoString/nsCAutoString</code> classes are derivatives of the <code>nsString/nsCString classes</code>. They own a 64-character buffer allocated in the same storage space as the string itself. If the <code>nsAutoString</code> is allocated on the stack, then it has at its disposal a 64-character stack buffer. This allows the implementation to avoid allocating extra memory when dealing with small strings.</p>
-<pre class="eval">...
-nsAutoString value;
-GetValue(value); // if the result is less than 64 code units,
-                 // then this just saved us an allocation
-...
-</pre>
-<h3 id="Member_variables" name="Member_variables">Member variables</h3>
-<p>In general, you should use the concrete classes <code>nsString</code> and <code>nsCString</code> for member variables.</p>
-<pre class="eval">class Foo {
-    ...
-    // these store UTF-8 and UTF-16 values respectively
-    nsCString mLocalName;
-    nsString mTitle;
-};
-</pre>
-<p>Note that the strings are declared directly in the class, not as pointers to strings. Don't do this:</p>
-<pre>class Foo {
-public:
-    Foo() {
-        mLocalName = new nsCString();
-        mTitle = new nsString();
-    }
-    ~Foo() { delete mLocalName; delete mTitle; }
-
-private:
-    // these store UTF-8 and UTF-16 values respectively
-    nsCString* mLocalName;
-    nsString*  mTitle;
-};
-</pre>
-<p>The above code may appear to save the cost of the string objects, but <code>nsString/nsCString</code> are small objects - the overhead of the allocation outweighs the few bytes you'd save by keeping a pointer.</p>
-<p>Another common incorrect pattern is to use <code>nsAutoString/nsCAutoString</code> for member variables. As described in <a href="#Local_variables">Local Variables</a>, these classes have a built in buffer that make them very large. This means that if you include them in a class, they bloat the class by 64 bytes (<code>nsCAutoString</code>) or 128 bytes (<code>nsAutoString</code>).</p>
-<p>An example:</p>
-<pre>class Foo {
-    ...
-
-    // bloats 'Foo' by 128 bytes!
-    nsAutoString mLocalName;
-};
-</pre>
-<h3 id="Raw_Character_Pointers" name="Raw_Character_Pointers">Raw Character Pointers</h3>
-<p><code>PromiseFlatString()</code> can be used to create a temporary buffer which holds a null-terminated buffer containing the same value as the source string. <code>PromiseFlatString()</code> will create a temporary buffer if necessary. This is most often used in order to pass an <code>nsAString</code> to an API which requires a null-terminated string.</p>
-<p>In the following example, an <code>nsAString</code> is combined with a literal string, and the result is passed to an API which requires a simple character buffer.</p>
-<pre class="eval">// Modify the URL and pass to AddPage(const PRUnichar* url)
-void AddModifiedPage(const nsAString&amp; url) {
-    NS_NAMED_LITERAL_STRING(httpPrefix, <span class="nowiki">"http://"</span>);
-    const nsAString&amp; modifiedURL = httpPrefix + url;
-
-    // creates a temporary buffer
-    AddPage(PromiseFlatString(modifiedURL).get());
-}
-</pre>
-<p><code>PromiseFlatString()</code> is smart when handed a string that is already null-terminated. It avoids creating the temporary buffer in such cases.</p>
-<pre class="eval">// Modify the URL and pass to AddPage(const PRUnichar* url)
-void AddModifiedPage(const nsAString&amp; url, PRBool addPrefix) {
-    if (addPrefix) {
-        // MUST create a temporary buffer - string is multi-fragmented
-        NS_NAMED_LITERAL_STRING(httpPrefix, <span class="nowiki">"http://"</span>);
-        AddPage(PromiseFlatString(httpPrefix + modifiedURL));
-    } else {
-        // MIGHT create a temporary buffer, does a runtime check
-        AddPage(PromiseFlatString(url).get());
-    }
-}
-</pre>
-<h3 id="printf_and_a_UTF-16_string" name="printf_and_a_UTF-16_string"><code>printf</code> and a UTF-16 string</h3>
-<p>For debugging, it's useful to <code>printf</code> a UTF-16 string (nsString, nsAutoString, nsXPIDLString, etc). To do this usually requires converting it to an 8-bit string, because that's what printf expects. However, on Windows, the following should work:</p>
-<pre class="eval">printf("%S\n", yourString.get());
-</pre>
-<p>(Note: I didn't test this. Also, I'm not sure what exactly this does to non-ASCII characters, especially when they are outside the system codepage). The reason that this doesn't work on Unix is because a wchar_t, which is what %S expects, is usually 4 bytes there (even when Mozilla is compiled with -fshort-wchar, because this would require libc to be compiled with -fshort-wchar).</p>
-<p>If non-ASCII characters aren't important, use:</p>
-<pre class="eval">printf("%s\n", NS_LossyConvertUTF16toASCII(yourString).get());
-</pre>
-<p>On platforms that use UTF-8 for console output (most Linux distributions), this works:</p>
-<pre class="eval">printf("%s\n", NS_ConvertUTF16toUTF8(yourString).get());
-</pre>
-<h2 id="IDL" name="IDL">IDL</h2>
-<p>The string library is also available through IDL. By declaring attributes and methods using the specially defined IDL types, string classes are used as parameters to the corresponding methods.</p>
-<h3 id="IDL_String_types" name="IDL_String_types">IDL String types</h3>
-<p>The C++ signatures follow the abstract-type convention described above, such that all method parameters are based on the <a href="#The_Abstract_Classes">abstract classes</a>. The following table describes the purpose of each string type in IDL.</p>
-<table class="standard-table">
- <tbody>
-  <tr>
-   <th class="header">IDL type</th>
-   <th class="header">C++ Type</th>
-   <th class="header">Purpose</th>
-  </tr>
-  <tr>
-   <td><code>string</code></td>
-   <td><code>char*</code></td>
-   <td>Raw character pointer to ASCII (7-bit) string, no string classes used. High bit is not guaranteed across XPConnect boundaries</td>
-  </tr>
-  <tr>
-   <td><code>wstring</code></td>
-   <td><code>PRUnichar*</code></td>
-   <td>Raw character pointer to UTF-16 string, no string classes used</td>
-  </tr>
-  <tr>
-   <td><code>AString</code></td>
-   <td><code>nsAString</code></td>
-   <td>UTF-16 string</td>
-  </tr>
-  <tr>
-   <td><code>ACString</code></td>
-   <td><code>nsACString</code></td>
-   <td>8-bit string, all bits are preserved across XPConnect boundaries</td>
-  </tr>
-  <tr>
-   <td><code>AUTF8String</code></td>
-   <td><code>nsACString</code></td>
-   <td>UTF-8 string - converted to UTF-16 as necessary when value is used across XPConnect boundaries</td>
-  </tr>
-  <tr>
-   <td><code>DOMString</code></td>
-   <td><code>nsAString</code></td>
-   <td>UTF-16 string used in the DOM. More or less the same as <code>AString</code>, but in JavaScript it has no distinction between whether the string is void or just empty. (not sure on this, looking for corrections.</td>
-  </tr>
- </tbody>
-</table>
-<h3 id="C.2B.2B_Signatures" name="C.2B.2B_Signatures">C++ Signatures</h3>
-<p>In IDL, <code>in</code> parameters are read-only, and the C++ signatures for <code>*String</code> parameters follows the above guidelines by using <code>const nsAString&amp;</code> for these parameters. <code>out</code> and <code>inout</code> parameters are defined simply as <code>nsAString</code> so that the callee can write to them.</p>
-<table class="standard-table">
- <tbody>
-  <tr>
-   <th class="header">IDL</th>
-   <th class="header">C++</th>
-  </tr>
-  <tr>
-   <td>
-    <pre class="eval">
-interface nsIFoo : nsISupports {
-
-    attribute AString utf16String;
-
-
-
-
-    AUTF8String getValue(in ACString key);
-
-};
-</pre>
-   </td>
-   <td>
-    <pre class="eval">
-class nsIFoo : public nsISupports {
-
-     NS_IMETHOD GetUtf16String(nsAString&amp;
-                               aResult) = 0;
-     NS_IMETHOD SetUtf16String(const nsAString&amp;
-                              aValue) = 0;
-
-     NS_IMETHOD GetValue(const nsACString&amp; aKey,
-                     nsACString&amp; aResult) = 0;
-};
-</pre>
-   </td>
-  </tr>
- </tbody>
-</table>
-<p>In the above example, <code>utf16String</code> is treated as a UTF-16 string. The implementation of <code>GetUtf16String()</code> will use <code>aResult.Assign</code> to "return" the value. In <code>SetUtf16String()</code> the value of the string can be used through a variety of methods including <a href="#Iterators">Iterators</a>, <code><a href="#Raw_Character_Pointers">PromiseFlatString</a></code>, and assignment to other strings.</p>
-<p>In <code>GetValue()</code>, the first parameter, <code>aKey</code>, is treated as a raw sequence of 8-bit values. Any non-ASCII characters in <code>aKey</code> will be preserved when crossing XPConnect boundaries. The implementation of <code>GetValue()</code> will assign a UTF-8 encoded 8-bit string into <code>aResult</code>. If the <code>this</code> method is called across XPConnect boundaries, such as from a script, then the result will be decoded from UTF-8 into UTF-16 and used as a Unicode value.</p>
-<h3 id="Choosing_a_string_type" name="Choosing_a_string_type">Choosing a string type</h3>
-<p>It can be difficult to determine the correct string type to use for IDL. The following points should help determine the appropriate string type.</p>
-<ul>
- <li>Using string classes may avoid new memory allocation for <code>out</code> parameters. For example, if the caller is using an <code>nsAutoString</code> to receive the value for an <code>out</code> parameter, (defined in C++ as simply <code>nsAString&amp;</code> then assignment of short (less than 64-characters) values to an <code>out</code> parameter will only copy the value into the <code>nsAutoString</code>'s buffer. Moreover, using the string classes allows for sharing of string buffers. In many cases, assigning from one string object to another avoids copying in favor of simply incrementing a reference count.</li>
- <li><code>in</code> strings using string classes often have their length pre-calculated. This can be a performance win.</li>
- <li>In cases where a raw-character buffer is required, <code>string</code> and <code>wstring</code> provide faster access than <code>PromiseFlatString</code>.</li>
- <li>UTF-8 strings defined with <code>AUTF8String</code> may need to be decoded when crossing XPConnect boundaries. This can be a performance hit. On the other hand, UTF-8 strings take up less space for strings that are commonly ASCII.</li>
- <li>UTF-16 strings defined with <code>wstring</code> or <code>AString</code> are fast when the unicode value is required. However, if the value is more often ASCII, then half of the storage space of the underlying string may be wasted.</li>
-</ul>
-<h2 id="Appendix_A_-_What_class_to_use_when" name="Appendix_A_-_What_class_to_use_when">Appendix A - What class to use when</h2>
-<p>This table provides a quick reference for what classes you should be using.</p>
-<table class="standard-table">
- <tbody>
-  <tr>
-   <th class="header">Context</th>
-   <th class="header">class</th>
-   <th class="header">Notes</th>
-  </tr>
-  <tr>
-   <td>Local Variables</td>
-   <td><code>nsAutoString<br>
-    nsCAutoString</code></td>
-   <td> </td>
-  </tr>
-  <tr>
-   <td>Class Member Variables</td>
-   <td><code>nsString<br>
-    nsCString</code></td>
-   <td> </td>
-  </tr>
-  <tr>
-   <td>Method Parameter types</td>
-   <td><code>nsAString<br>
-    nsACString</code></td>
-   <td>Use abstract classes for parameters. Use <code>const nsAString&amp;</code> for "in" parameters and <code>nsAString&amp;</code> for "out" parameters.</td>
-  </tr>
-  <tr>
-   <td>Retrieving "out" string/wstrings</td>
-   <td><code>nsXPIDLString<br>
-    nsXPIDLCString</code></td>
-   <td>Use <code>getter_Copies()</code>. Similar to <code>nsString / nsCString</code>.</td>
-  </tr>
-  <tr>
-   <td>Wrapping character buffers</td>
-   <td><code>nsDependentString<br>
-    nsDependentCString</code></td>
-   <td>Wrap <code>const char* / const PRUnichar*</code> buffers.</td>
-  </tr>
-  <tr>
-   <td>Literal strings</td>
-   <td><code>NS_LITERAL_STRING<br>
-    NS_LITERAL_CSTRING</code></td>
-   <td>Similar to <code>nsDependent[C]String</code>, but pre-calculates length at build time.</td>
-  </tr>
- </tbody>
-</table>
-<h2 id="Appendix_B_-_nsAString_Reference" name="Appendix_B_-_nsAString_Reference">Appendix B - nsAString Reference</h2>
-<p>Read-only methods.</p>
-<ul>
- <li><code><b>Length()</b></code></li>
- <li><code><b>IsEmpty()</b></code></li>
- <li><code><b>IsVoid()</b></code> - XPConnect will convert void nsAStrings to JavaScript <code>null</code>.</li>
- <li><code><b>BeginReading(
-  <i>
-   iterator</i>
-  )</b></code></li>
- <li><code><b>EndReading(
-  <i>
-   iterator</i>
-  )</b></code></li>
- <li><code><b>Equals(
-  <i>
-   string[, comparator]</i>
-  )</b></code></li>
- <li><code><b>First()</b></code></li>
- <li><code><b>Last()</b></code></li>
- <li><code><b>CountChar()</b></code></li>
- <li><code><b>Left(
-  <i>
-   outstring, length</i>
-  )</b></code></li>
- <li><code><b>Mid(
-  <i>
-   outstring, position, length</i>
-  )</b></code></li>
- <li><code><b>Right(
-  <i>
-   outstring, length</i>
-  )</b></code></li>
- <li><code><b>FindChar(
-  <i>
-   character</i>
-  )</b></code></li>
-</ul>
-<p>Methods that modify the string.</p>
-<ul>
- <li><code><b>Assign(
-  <i>
-   string</i>
-  )</b></code></li>
- <li><code><b>Append(
-  <i>
-   string</i>
-  )</b></code></li>
- <li><code><b>Insert(
-  <i>
-   string</i>
-  )</b></code></li>
- <li><code><b>Cut(
-  <i>
-   start, length</i>
-  )</b></code></li>
- <li><code><b>Replace(
-  <i>
-   start, length, string</i>
-  )</b></code></li>
- <li><code><b>Truncate(
-  <i>
-   length</i>
-  )</b></code></li>
- <li><code><b>SetIsVoid(
-  <i>
-   state</i>
-  )</b></code> - XPConnect will convert void nsAStrings to JavaScript <code>null</code>.</li>
- <li><code><b>BeginWriting(
-  <i>
-   iterator</i>
-  )</b></code></li>
- <li><code><b>EndWriting(
-  <i>
-   iterator</i>
-  )</b></code></li>
- <li><code><b>SetCapacity()</b></code></li>
-</ul>