[ruby/psych] Add support for ruby 3.2 Data objects
[ruby.git] / string.rb
blobf925cc583363790e94135c8ce31eb6ece3719236
1 # A +String+ object has an arbitrary sequence of bytes,
2 # typically representing text or binary data.
3 # A +String+ object may be created using String::new or as literals.
5 # String objects differ from Symbol objects in that Symbol objects are
6 # designed to be used as identifiers, instead of text or data.
8 # You can create a +String+ object explicitly with:
10 # - A {string literal}[rdoc-ref:syntax/literals.rdoc@String+Literals].
11 # - A {heredoc literal}[rdoc-ref:syntax/literals.rdoc@Here+Document+Literals].
13 # You can convert certain objects to Strings with:
15 # - Method #String.
17 # Some +String+ methods modify +self+.
18 # Typically, a method whose name ends with <tt>!</tt> modifies +self+
19 # and returns +self+;
20 # often, a similarly named method (without the <tt>!</tt>)
21 # returns a new string.
23 # In general, if both bang and non-bang versions of a method exist,
24 # the bang method mutates and the non-bang method does not.
25 # However, a method without a bang can also mutate, such as String#replace.
27 # == Substitution Methods
29 # These methods perform substitutions:
31 # - String#sub: One substitution (or none); returns a new string.
32 # - String#sub!: One substitution (or none); returns +self+ if any changes,
33 #   +nil+ otherwise.
34 # - String#gsub: Zero or more substitutions; returns a new string.
35 # - String#gsub!: Zero or more substitutions; returns +self+ if any changes,
36 #   +nil+ otherwise.
38 # Each of these methods takes:
40 # - A first argument, +pattern+ (String or Regexp),
41 #   that specifies the substring(s) to be replaced.
43 # - Either of the following:
45 #   - A second argument, +replacement+ (String or Hash),
46 #     that determines the replacing string.
47 #   - A block that will determine the replacing string.
49 # The examples in this section mostly use the String#sub and String#gsub methods;
50 # the principles illustrated apply to all four substitution methods.
52 # <b>Argument +pattern+</b>
54 # Argument +pattern+ is commonly a regular expression:
56 #   s = 'hello'
57 #   s.sub(/[aeiou]/, '*') # => "h*llo"
58 #   s.gsub(/[aeiou]/, '*') # => "h*ll*"
59 #   s.gsub(/[aeiou]/, '')  # => "hll"
60 #   s.sub(/ell/, 'al')     # => "halo"
61 #   s.gsub(/xyzzy/, '*')   # => "hello"
62 #   'THX1138'.gsub(/\d+/, '00') # => "THX00"
64 # When +pattern+ is a string, all its characters are treated
65 # as ordinary characters (not as Regexp special characters):
67 #   'THX1138'.gsub('\d+', '00') # => "THX1138"
69 # <b>+String+ +replacement+</b>
71 # If +replacement+ is a string, that string determines
72 # the replacing string that is substituted for the matched text.
74 # Each of the examples above uses a simple string as the replacing string.
76 # +String+ +replacement+ may contain back-references to the pattern's captures:
78 # - <tt>\n</tt> (_n_ is a non-negative integer) refers to <tt>$n</tt>.
79 # - <tt>\k<name></tt> refers to the named capture +name+.
81 # See Regexp for details.
83 # Note that within the string +replacement+, a character combination
84 # such as <tt>$&</tt> is treated as ordinary text, not as
85 # a special match variable.
86 # However, you may refer to some special match variables using these
87 # combinations:
89 # - <tt>\&</tt> and <tt>\0</tt> correspond to <tt>$&</tt>,
90 #   which contains the complete matched text.
91 # - <tt>\'</tt> corresponds to <tt>$'</tt>,
92 #   which contains the string after the match.
93 # - <tt>\`</tt> corresponds to <tt>$`</tt>,
94 #   which contains the string before the match.
95 # - <tt>\\+</tt> corresponds to <tt>$+</tt>,
96 #   which contains the last capture group.
98 # See Regexp for details.
100 # Note that <tt>\\\\</tt> is interpreted as an escape, i.e., a single backslash.
102 # Note also that a string literal consumes backslashes.
103 # See {String Literals}[rdoc-ref:syntax/literals.rdoc@String+Literals] for details about string literals.
105 # A back-reference is typically preceded by an additional backslash.
106 # For example, if you want to write a back-reference <tt>\&</tt> in
107 # +replacement+ with a double-quoted string literal, you need to write
108 # <tt>"..\\\\&.."</tt>.
110 # If you want to write a non-back-reference string <tt>\&</tt> in
111 # +replacement+, you need to first escape the backslash to prevent
112 # this method from interpreting it as a back-reference, and then you
113 # need to escape the backslashes again to prevent a string literal from
114 # consuming them: <tt>"..\\\\\\\\&.."</tt>.
116 # You may want to use the block form to avoid excessive backslashes.
118 # <b>\Hash +replacement+</b>
120 # If the argument +replacement+ is a hash, and +pattern+ matches one of its keys,
121 # the replacing string is the value for that key:
123 #   h = {'foo' => 'bar', 'baz' => 'bat'}
124 #   'food'.sub('foo', h) # => "bard"
126 # Note that a symbol key does not match:
128 #   h = {foo: 'bar', baz: 'bat'}
129 #   'food'.sub('foo', h) # => "d"
131 # <b>Block</b>
133 # In the block form, the current match string is passed to the block;
134 # the block's return value becomes the replacing string:
136 #   s = '@'
137 #   '1234'.gsub(/\d/) { |match| s.succ! } # => "ABCD"
139 # Special match variables such as <tt>$1</tt>, <tt>$2</tt>, <tt>$`</tt>,
140 # <tt>$&</tt>, and <tt>$'</tt> are set appropriately.
142 # == Whitespace in Strings
144 # In the class +String+, _whitespace_ is defined as a contiguous sequence of characters
145 # consisting of any mixture of the following:
147 # - NL (null): <tt>"\x00"</tt>, <tt>"\u0000"</tt>.
148 # - HT (horizontal tab): <tt>"\x09"</tt>, <tt>"\t"</tt>.
149 # - LF (line feed): <tt>"\x0a"</tt>, <tt>"\n"</tt>.
150 # - VT (vertical tab): <tt>"\x0b"</tt>, <tt>"\v"</tt>.
151 # - FF (form feed): <tt>"\x0c"</tt>, <tt>"\f"</tt>.
152 # - CR (carriage return): <tt>"\x0d"</tt>, <tt>"\r"</tt>.
153 # - SP (space): <tt>"\x20"</tt>, <tt>" "</tt>.
156 # Whitespace is relevant for the following methods:
158 # - #lstrip, #lstrip!: Strip leading whitespace.
159 # - #rstrip, #rstrip!: Strip trailing whitespace.
160 # - #strip, #strip!: Strip leading and trailing whitespace.
162 # == +String+ Slices
164 # A _slice_ of a string is a substring selected by certain criteria.
166 # These instance methods utilize slicing:
168 # - String#[] (aliased as String#slice): Returns a slice copied from +self+.
169 # - String#[]=: Mutates +self+ with the slice replaced.
170 # - String#slice!: Mutates +self+ with the slice removed and returns the removed slice.
172 # Each of the above methods takes arguments that determine the slice
173 # to be copied or replaced.
175 # The arguments have several forms.
176 # For a string +string+, the forms are:
178 # - <tt>string[index]</tt>
179 # - <tt>string[start, length]</tt>
180 # - <tt>string[range]</tt>
181 # - <tt>string[regexp, capture = 0]</tt>
182 # - <tt>string[substring]</tt>
184 # <b><tt>string[index]</tt></b>
186 # When a non-negative integer argument +index+ is given,
187 # the slice is the 1-character substring found in +self+ at character offset +index+:
189 #   'bar'[0]      # => "b"
190 #   'bar'[2]      # => "r"
191 #   'bar'[20]     # => nil
192 #   'тест'[2]     # => "с"
193 #   'こんにちは'[4] # => "は"
195 # When a negative integer +index+ is given,
196 # the slice begins at the offset given by counting backward from the end of +self+:
198 #   'bar'[-3]      # => "b"
199 #   'bar'[-1]      # => "r"
200 #   'bar'[-20]     # => nil
202 # <b><tt>string[start, length]</tt></b>
204 # When non-negative integer arguments +start+ and +length+ are given,
205 # the slice begins at character offset +start+, if it exists,
206 # and continues for +length+ characters, if available:
208 #   'foo'[0, 2]      # => "fo"
209 #   'тест'[1, 2]     # => "ес"
210 #   'こんにちは'[2, 2] # => "にち"
211 #   # Zero length.
212 #   'foo'[2, 0]      # => ""
213 #   # Length not entirely available.
214 #   'foo'[1, 200]    # => "oo"
215 #   # Start out of range.
216 #   'foo'[4, 2]      # => nil
218 # Special case: if +start+ equals the length of +self+,
219 # the slice is a new empty string:
221 #   'foo'[3, 2]    # => ""
222 #   'foo'[3, 200]  # => ""
224 # When a negative +start+ and non-negative +length+ are given,
225 # the slice begins by counting backward from the end of +self+,
226 # and continues for +length+ characters, if available:
228 #   'foo'[-2, 2]     # => "oo"
229 #   'foo'[-2, 200]   # => "oo"
230 #   # Start out of range.
231 #   'foo'[-4, 2]     # => nil
233 # When a negative +length+ is given, there is no slice:
235 #   'foo'[1, -1]   # => nil
236 #   'foo'[-2, -1]  # => nil
238 # <b><tt>string[range]</tt></b>
240 # When a Range argument +range+ is given,
241 # it creates a substring of +string+ using the indices in +range+.
242 # The slice is then determined as above:
244 #   'foo'[0..1]     # => "fo"
245 #   'foo'[0, 2]     # => "fo"
247 #   'foo'[2...2]    # => ""
248 #   'foo'[2, 0]     # => ""
250 #   'foo'[1..200]   # => "oo"
251 #   'foo'[1, 200]   # => "oo"
253 #   'foo'[4..5]     # => nil
254 #   'foo'[4, 2]     # => nil
256 #   'foo'[-4..-3]   # => nil
257 #   'foo'[-4, 2]    # => nil
259 #   'foo'[3..4]     # => ""
260 #   'foo'[3, 2]     # => ""
262 #   'foo'[-2..-1]   # => "oo"
263 #   'foo'[-2, 2]    # => "oo"
265 #   'foo'[-2..197]  # => "oo"
266 #   'foo'[-2, 200]  # => "oo"
268 # <b><tt>string[regexp, capture = 0]</tt></b>
270 # When the Regexp argument +regexp+ is given,
271 # and the +capture+ argument is <tt>0</tt>,
272 # the slice is the first matching substring found in +self+:
274 #   'foo'[/o/]                # => "o"
275 #   'foo'[/x/]                # => nil
276 #   s = 'hello there'
277 #   s[/[aeiou](.)\1/]        # => "ell"
278 #   s[/[aeiou](.)\1/, 0]     # => "ell"
280 # If the argument +capture+ is provided and not <tt>0</tt>,
281 # it should be either a capture group index (integer)
282 # or a capture group name (String or Symbol);
283 # the slice is the specified capture (see Regexp@Groups and Captures):
285 #   s = 'hello there'
286 #   s[/[aeiou](.)\1/, 1] # => "l"
287 #   s[/(?<vowel>[aeiou])(?<non_vowel>[^aeiou])/, "non_vowel"] # => "l"
288 #   s[/(?<vowel>[aeiou])(?<non_vowel>[^aeiou])/, :vowel]      # => "e"
290 # If an invalid capture group index is given, there is no slice.
291 # If an invalid capture group name is given, +IndexError+ is raised.
293 # <b><tt>string[substring]</tt></b>
295 # When the single +String+ argument +substring+ is given,
296 # it returns the substring from +self+ if found, otherwise +nil+:
298 #   'foo'['oo'] # => "oo"
299 #   'foo'['xx'] # => nil
301 # == What's Here
303 # First, what's elsewhere. Class +String+:
305 # - Inherits from the {Object class}[rdoc-ref:Object@What-27s+Here].
306 # - Includes the {Comparable module}[rdoc-ref:Comparable@What-27s+Here].
308 # Here, class +String+ provides methods that are useful for:
310 # - {Creating a String}[rdoc-ref:String@Methods+for+Creating+a+String]
311 # - {Frozen/Unfrozen Strings}[rdoc-ref:String@Methods+for+a+Frozen-2FUnfrozen+String]
312 # - {Querying}[rdoc-ref:String@Methods+for+Querying]
313 # - {Comparing}[rdoc-ref:String@Methods+for+Comparing]
314 # - {Modifying a String}[rdoc-ref:String@Methods+for+Modifying+a+String]
315 # - {Converting to New String}[rdoc-ref:String@Methods+for+Converting+to+New+String]
316 # - {Converting to Non-String}[rdoc-ref:String@Methods+for+Converting+to+Non-String]
317 # - {Iterating}[rdoc-ref:String@Methods+for+Iterating]
319 # === Methods for Creating a +String+
321 # - ::new: Returns a new string.
322 # - ::try_convert: Returns a new string created from a given object.
324 # === Methods for a Frozen/Unfrozen String
326 # - #+@: Returns a string that is not frozen: +self+ if not frozen;
327 #   +self.dup+ otherwise.
328 # - #-@ (aliased as #dedup): Returns a string that is frozen: +self+ if already frozen;
329 #   +self.freeze+ otherwise.
330 # - #freeze: Freezes +self+ if not already frozen; returns +self+.
332 # === Methods for Querying
334 # _Counts_
336 # - #length (aliased as #size): Returns the count of characters (not bytes).
337 # - #empty?: Returns +true+ if +self.length+ is zero; +false+ otherwise.
338 # - #bytesize: Returns the count of bytes.
339 # - #count: Returns the count of substrings matching given strings.
341 # _Substrings_
343 # - #=~: Returns the index of the first substring that matches a given
344 #   Regexp or other object; returns +nil+ if no match is found.
345 # - #index: Returns the index of the _first_ occurrence of a given substring;
346 #   returns +nil+ if none found.
347 # - #rindex: Returns the index of the _last_ occurrence of a given substring;
348 #   returns +nil+ if none found.
349 # - #include?: Returns +true+ if the string contains a given substring; +false+ otherwise.
350 # - #match: Returns a MatchData object if the string matches a given Regexp; +nil+ otherwise.
351 # - #match?: Returns +true+ if the string matches a given Regexp; +false+ otherwise.
352 # - #start_with?: Returns +true+ if the string begins with any of the given substrings.
353 # - #end_with?: Returns +true+ if the string ends with any of the given substrings.
355 # _Encodings_
357 # - #encoding\: Returns the Encoding object that represents the encoding of the string.
358 # - #unicode_normalized?: Returns +true+ if the string is in Unicode normalized form; +false+ otherwise.
359 # - #valid_encoding?: Returns +true+ if the string contains only characters that are valid
360 #   for its encoding.
361 # - #ascii_only?: Returns +true+ if the string has only ASCII characters; +false+ otherwise.
363 # _Other_
365 # - #sum: Returns a basic checksum for the string: the sum of each byte.
366 # - #hash: Returns the integer hash code.
368 # === Methods for Comparing
370 # - #== (aliased as #===): Returns +true+ if a given other string has the same content as +self+.
371 # - #eql?: Returns +true+ if the content is the same as the given other string.
372 # - #<=>: Returns -1, 0, or 1 as a given other string is smaller than,
373 #   equal to, or larger than +self+.
374 # - #casecmp: Ignoring case, returns -1, 0, or 1 as a given
375 #   other string is smaller than, equal to, or larger than +self+.
376 # - #casecmp?: Returns +true+ if the string is equal to a given string after Unicode case folding;
377 #   +false+ otherwise.
379 # === Methods for Modifying a +String+
381 # Each of these methods modifies +self+.
383 # _Insertion_
385 # - #insert: Returns +self+ with a given string inserted at a specified offset.
386 # - #<<: Returns +self+ concatenated with a given string or integer.
387 # - #append_as_bytes: Returns +self+ concatenated with strings without performing any
388 #   encoding validation or conversion.
390 # _Substitution_
392 # - #sub!: Replaces the first substring that matches a given pattern with a given replacement string;
393 #   returns +self+ if any changes, +nil+ otherwise.
394 # - #gsub!: Replaces each substring that matches a given pattern with a given replacement string;
395 #   returns +self+ if any changes, +nil+ otherwise.
396 # - #succ! (aliased as #next!): Returns +self+ modified to become its own successor.
397 # - #initialize_copy (aliased as #replace): Returns +self+ with its entire content replaced by a given string.
398 # - #reverse!: Returns +self+ with its characters in reverse order.
399 # - #setbyte: Sets the byte at a given integer offset to a given value; returns the argument.
400 # - #tr!: Replaces specified characters in +self+ with specified replacement characters;
401 #   returns +self+ if any changes, +nil+ otherwise.
402 # - #tr_s!: Replaces specified characters in +self+ with specified replacement characters,
403 #   removing duplicates from the substrings that were modified;
404 #   returns +self+ if any changes, +nil+ otherwise.
406 # _Casing_
408 # - #capitalize!: Upcases the initial character and downcases all others;
409 #   returns +self+ if any changes, +nil+ otherwise.
410 # - #downcase!: Downcases all characters; returns +self+ if any changes, +nil+ otherwise.
411 # - #upcase!: Upcases all characters; returns +self+ if any changes, +nil+ otherwise.
412 # - #swapcase!: Upcases each downcase character and downcases each upcase character;
413 #   returns +self+ if any changes, +nil+ otherwise.
415 # _Encoding_
417 # - #encode!: Returns +self+ with all characters transcoded from one encoding to another.
418 # - #unicode_normalize!: Unicode-normalizes +self+; returns +self+.
419 # - #scrub!: Replaces each invalid byte with a given character; returns +self+.
420 # - #force_encoding: Changes the encoding to a given encoding; returns +self+.
422 # _Deletion_
424 # - #clear: Removes all content, so that +self+ is empty; returns +self+.
425 # - #slice!, #[]=: Removes a substring determined by a given index, start/length, range, regexp, or substring.
426 # - #squeeze!: Removes contiguous duplicate characters; returns +self+.
427 # - #delete!: Removes characters as determined by the intersection of substring arguments.
428 # - #lstrip!: Removes leading whitespace; returns +self+ if any changes, +nil+ otherwise.
429 # - #rstrip!: Removes trailing whitespace; returns +self+ if any changes, +nil+ otherwise.
430 # - #strip!: Removes leading and trailing whitespace; returns +self+ if any changes, +nil+ otherwise.
431 # - #chomp!: Removes the trailing record separator, if found; returns +self+ if any changes, +nil+ otherwise.
432 # - #chop!: Removes trailing newline characters if found; otherwise removes the last character;
433 #   returns +self+ if any changes, +nil+ otherwise.
435 # === Methods for Converting to New +String+
437 # Each of these methods returns a new +String+ based on +self+,
438 # often just a modified copy of +self+.
440 # _Extension_
442 # - #*: Returns the concatenation of multiple copies of +self+.
443 # - #+: Returns the concatenation of +self+ and a given other string.
444 # - #center: Returns a copy of +self+ centered between pad substrings.
445 # - #concat: Returns the concatenation of +self+ with given other strings.
446 # - #prepend: Returns the concatenation of a given other string with +self+.
447 # - #ljust: Returns a copy of +self+ of a given length, right-padded with a given other string.
448 # - #rjust: Returns a copy of +self+ of a given length, left-padded with a given other string.
450 # _Encoding_
452 # - #b: Returns a copy of +self+ with ASCII-8BIT encoding.
453 # - #scrub: Returns a copy of +self+ with each invalid byte replaced with a given character.
454 # - #unicode_normalize: Returns a copy of +self+ with each character Unicode-normalized.
455 # - #encode: Returns a copy of +self+ with all characters transcoded from one encoding to another.
457 # _Substitution_
459 # - #dump: Returns a copy of +self+ with all non-printing characters replaced by \xHH notation
460 #   and all special characters escaped.
461 # - #undump: Returns a copy of +self+ with all <tt>\xNN</tt> notations replaced by <tt>\uNNNN</tt> notations
462 #   and all escaped characters unescaped.
463 # - #sub: Returns a copy of +self+ with the first substring matching a given pattern
464 #   replaced with a given replacement string.
465 # - #gsub: Returns a copy of +self+ with each substring that matches a given pattern
466 #   replaced with a given replacement string.
467 # - #succ (aliased as #next): Returns the string that is the successor to +self+.
468 # - #reverse: Returns a copy of +self+ with its characters in reverse order.
469 # - #tr: Returns a copy of +self+ with specified characters replaced with specified replacement characters.
470 # - #tr_s: Returns a copy of +self+ with specified characters replaced with
471 #   specified replacement characters,
472 #   removing duplicates from the substrings that were modified.
473 # - #%: Returns the string resulting from formatting a given object into +self+.
475 # _Casing_
477 # - #capitalize: Returns a copy of +self+ with the first character upcased
478 #   and all other characters downcased.
479 # - #downcase: Returns a copy of +self+ with all characters downcased.
480 # - #upcase: Returns a copy of +self+ with all characters upcased.
481 # - #swapcase: Returns a copy of +self+ with all upcase characters downcased
482 #   and all downcase characters upcased.
484 # _Deletion_
486 # - #delete: Returns a copy of +self+ with characters removed.
487 # - #delete_prefix: Returns a copy of +self+ with a given prefix removed.
488 # - #delete_suffix: Returns a copy of +self+ with a given suffix removed.
489 # - #lstrip: Returns a copy of +self+ with leading whitespace removed.
490 # - #rstrip: Returns a copy of +self+ with trailing whitespace removed.
491 # - #strip: Returns a copy of +self+ with leading and trailing whitespace removed.
492 # - #chomp: Returns a copy of +self+ with a trailing record separator removed, if found.
493 # - #chop: Returns a copy of +self+ with trailing newline characters or the last character removed.
494 # - #squeeze: Returns a copy of +self+ with contiguous duplicate characters removed.
495 # - #[] (aliased as #slice): Returns a substring determined by a given index, start/length, range, regexp, or string.
496 # - #byteslice: Returns a substring determined by a given index, start/length, or range.
497 # - #chr: Returns the first character.
499 # _Duplication_
501 # - #to_s (aliased as #to_str): If +self+ is a subclass of +String+, returns +self+ copied into a +String+;
502 #   otherwise, returns +self+.
504 # === Methods for Converting to Non-+String+
506 # Each of these methods converts the contents of +self+ to a non-+String+.
508 # <em>Characters, Bytes, and Clusters</em>
510 # - #bytes: Returns an array of the bytes in +self+.
511 # - #chars: Returns an array of the characters in +self+.
512 # - #codepoints: Returns an array of the integer ordinals in +self+.
513 # - #getbyte: Returns the integer byte at the given index in +self+.
514 # - #grapheme_clusters: Returns an array of the grapheme clusters in +self+.
516 # _Splitting_
518 # - #lines: Returns an array of the lines in +self+, as determined by a given record separator.
519 # - #partition: Returns a 3-element array determined by the first substring that matches
520 #   a given substring or regexp.
521 # - #rpartition: Returns a 3-element array determined by the last substring that matches
522 #   a given substring or regexp.
523 # - #split: Returns an array of substrings determined by a given delimiter -- regexp or string --
524 #   or, if a block is given, passes those substrings to the block.
526 # _Matching_
528 # - #scan: Returns an array of substrings matching a given regexp or string, or,
529 #   if a block is given, passes each matching substring to the block.
530 # - #unpack: Returns an array of substrings extracted from +self+ according to a given format.
531 # - #unpack1: Returns the first substring extracted from +self+ according to a given format.
533 # _Numerics_
535 # - #hex: Returns the integer value of the leading characters, interpreted as hexadecimal digits.
536 # - #oct: Returns the integer value of the leading characters, interpreted as octal digits.
537 # - #ord: Returns the integer ordinal of the first character in +self+.
538 # - #to_i: Returns the integer value of leading characters, interpreted as an integer.
539 # - #to_f: Returns the floating-point value of leading characters, interpreted as a floating-point number.
541 # <em>Strings and Symbols</em>
543 # - #inspect: Returns a copy of +self+, enclosed in double quotes, with special characters escaped.
544 # - #intern (aliased as #to_sym): Returns the symbol corresponding to +self+.
546 # === Methods for Iterating
548 # - #each_byte: Calls the given block with each successive byte in +self+.
549 # - #each_char: Calls the given block with each successive character in +self+.
550 # - #each_codepoint: Calls the given block with each successive integer codepoint in +self+.
551 # - #each_grapheme_cluster: Calls the given block with each successive grapheme cluster in +self+.
552 # - #each_line: Calls the given block with each successive line in +self+,
553 #   as determined by a given record separator.
554 # - #upto: Calls the given block with each string value returned by successive calls to #succ.
556 class String; end