Documentation

Init.Data.String.Modify

Modification operations on strings #

This file contains operations on strings which modify the string, like set or map. There will usually not be a String.Slice version of these operations.

@[extern lean_string_utf8_set]
def String.Pos.set {s : String} (p : s.Pos) (c : Char) (hp : p s.endPos) :

Replaces the character at a specified position in a string with a new character.

If both the replacement character and the replaced character are 7-bit ASCII characters and the string is not shared, then it is updated in-place and not copied.

Examples:

  • ("abc".pos ⟨1⟩ (by decide)).set 'B' (by decide) = "aBc"
  • ("L∃∀N".pos ⟨4⟩ (by decide)).set 'X' (by decide) = "L∃XN"
Equations
    Instances For
      theorem String.Pos.set_eq_append {s : String} {p : s.Pos} {c : Char} {hp : p s.endPos} :
      p.set c hp = (s.sliceTo p).copy ++ singleton c ++ (s.sliceFrom (p.next hp)).copy
      theorem String.Pos.Raw.IsValid.set_of_le {s : String} {p : s.Pos} {c : Char} {hp : p s.endPos} {q : Raw} (hq : IsValid s q) (hpq : q p.offset) :
      IsValid (p.set c hp) q
      @[inline]
      def String.Pos.toSetOfLE {s : String} (q p : s.Pos) (c : Char) (hp : p s.endPos) (hpq : q p) :
      (p.set c hp).Pos

      Given a valid position in a string, obtain the corresponding position after setting a character on that string, provided that the position was before the changed position.

      Equations
        Instances For
          @[simp]
          theorem String.Pos.offset_toSetOfLE {s : String} {q p : s.Pos} {c : Char} {hp : p s.endPos} {hpq : q p} :
          (q.toSetOfLE p c hp hpq).offset = q.offset
          theorem String.Pos.Raw.isValid_add_char_set {s : String} {p : s.Pos} {c : Char} {hp : p s.endPos} :
          IsValid (p.set c hp) (p.offset + c)
          @[inline]
          def String.Pos.pastSet {s : String} (p : s.Pos) (c : Char) (hp : p s.endPos) :
          (p.set c hp).Pos

          The position just after the position that changed in a Pos.set call.

          Equations
            Instances For
              @[simp]
              theorem String.Pos.offset_pastSet {s : String} {p : s.Pos} {c : Char} {hp : p s.endPos} :
              (p.pastSet c hp).offset = p.offset + c
              @[inline]
              def String.Pos.appendRight {s : String} (p : s.Pos) (t : String) :
              (s ++ t).Pos
              Equations
                Instances For
                  theorem String.Pos.splits_pastSet {s : String} {p : s.Pos} {c : Char} {hp : p s.endPos} :
                  (p.pastSet c hp).Splits ((s.sliceTo p).copy ++ singleton c) (s.sliceFrom (p.next hp)).copy
                  theorem String.remainingBytes_pastSet {s : String} {p : s.Pos} {c : Char} {hp : p s.endPos} :
                  @[inline]
                  def String.Pos.modify {s : String} (p : s.Pos) (f : CharChar) (hp : p s.endPos) :

                  Replaces the character at position p in the string s with the result of applying f to that character.

                  If both the replacement character and the replaced character are 7-bit ASCII characters and the string is not shared, then it is updated in-place and not copied.

                  Examples:

                  Equations
                    Instances For
                      theorem String.Pos.Raw.IsValid.modify_of_le {s : String} {p : s.Pos} {f : CharChar} {hp : p s.endPos} {q : Raw} (hq : IsValid s q) (hpq : q p.offset) :
                      IsValid (p.modify f hp) q
                      @[inline]
                      def String.Pos.toModifyOfLE {s : String} (q p : s.Pos) (f : CharChar) (hp : p s.endPos) (hpq : q p) :
                      (p.modify f hp).Pos

                      Given a valid position in a string, obtain the corresponding position after modifying a character in that string, provided that the position was before the changed position.

                      Equations
                        Instances For
                          @[simp]
                          theorem String.Pos.offset_toModifyOfLE {s : String} {q p : s.Pos} {f : CharChar} {hp : p s.endPos} {hpq : q p} :
                          (q.toModifyOfLE p f hp hpq).offset = q.offset
                          @[inline]
                          def String.Pos.pastModify {s : String} (p : s.Pos) (f : CharChar) (hp : p s.endPos) :
                          (p.modify f hp).Pos

                          The position just after the position that was modified in a Pos.modify call.

                          Equations
                            Instances For
                              @[extern lean_string_utf8_set]

                              Replaces the character at a specified position in a string with a new character. If the position is invalid, the string is returned unchanged.

                              If both the replacement character and the replaced character are 7-bit ASCII characters and the string is not shared, then it is updated in-place and not copied.

                              This is a legacy function. The recommended alternative is String.Pos.set, combined with String.pos or another means of obtaining a String.Pos.

                              Examples:

                              • "abc".set ⟨1⟩ 'B' = "aBc"
                              • "abc".set ⟨3⟩ 'D' = "abc"
                              • "L∃∀N".set ⟨4⟩ 'X' = "L∃XN"
                              • "L∃∀N".set ⟨2⟩ 'X' = "L∃∀N" because '∃' is a multi-byte character, so the byte index 2 is an invalid position.
                              Equations
                                Instances For
                                  @[extern lean_string_utf8_set, deprecated String.Pos.Raw.set (since := "2025-10-14")]
                                  Equations
                                    Instances For
                                      def String.Pos.Raw.modify (s : String) (i : Raw) (f : CharChar) :

                                      Replaces the character at position p in the string s with the result of applying f to that character. If p is an invalid position, the string is returned unchanged.

                                      If both the replacement character and the replaced character are 7-bit ASCII characters and the string is not shared, then it is updated in-place and not copied.

                                      This is a legacy function. The recommended alternative is String.Pos.set, combined with String.pos or another means of obtaining a String.Pos.

                                      Examples:

                                      Equations
                                        Instances For
                                          @[deprecated String.Pos.Raw.modify (since := "2025-10-10")]
                                          def String.modify (s : String) (i : Pos.Raw) (f : CharChar) :

                                          Replaces the character at position p in the string s with the result of applying f to that character. If p is an invalid position, the string is returned unchanged.

                                          If both the replacement character and the replaced character are 7-bit ASCII characters and the string is not shared, then it is updated in-place and not copied.

                                          This is a legacy function. The recommended alternative is String.Pos.set, combined with String.pos or another means of obtaining a String.Pos.

                                          Examples:

                                          Equations
                                            Instances For
                                              @[irreducible, specialize #[]]
                                              def String.mapAux (f : CharChar) (s : String) (p : s.Pos) :
                                              Equations
                                                Instances For
                                                  @[inline]
                                                  def String.map (f : CharChar) (s : String) :

                                                  Applies the function f to every character in a string, returning a string that contains the resulting characters.

                                                  Examples:

                                                  Equations
                                                    Instances For
                                                      @[inline]

                                                      Replaces each character in s with the result of applying Char.toUpper to it.

                                                      Char.toUpper has no effect on characters outside of the range 'a''z'.

                                                      Examples:

                                                      Equations
                                                        Instances For
                                                          @[inline]

                                                          Replaces each character in s with the result of applying Char.toLower to it.

                                                          Char.toLower has no effect on characters outside of the range 'A''Z'.

                                                          Examples:

                                                          Equations
                                                            Instances For
                                                              @[inline]

                                                              Replaces the first character in s with the result of applying Char.toUpper to it. Returns the empty string if the string is empty.

                                                              Char.toUpper has no effect on characters outside of the range 'a''z'.

                                                              Examples:

                                                              Equations
                                                                Instances For
                                                                  @[export lean_string_capitalize]
                                                                  Equations
                                                                    Instances For
                                                                      @[inline]

                                                                      Replaces the first character in s with the result of applying Char.toLower to it. Returns the empty string if the string is empty.

                                                                      Char.toLower has no effect on characters outside of the range 'A''Z'.

                                                                      Examples:

                                                                      Equations
                                                                        Instances For