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.ValidPos.set {s : String} (p : s.ValidPos) (c : Char) (hp : p s.endValidPos) :

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
  • One or more equations did not get rendered due to their size.
Instances For
    theorem String.ValidPos.set_eq_append {s : String} {p : s.ValidPos} {c : Char} {hp : p s.endValidPos} :
    p.set c hp = (s.replaceEnd p).copy ++ singleton c ++ (s.replaceStart (p.next hp)).copy
    theorem String.Pos.Raw.IsValid.set_of_le {s : String} {p : s.ValidPos} {c : Char} {hp : p s.endValidPos} {q : Raw} (hq : IsValid s q) (hpq : q p.offset) :
    IsValid (p.set c hp) q
    @[inline]
    def String.ValidPos.toSetOfLE {s : String} (q p : s.ValidPos) (c : Char) (hp : p s.endValidPos) (hpq : q p) :
    (p.set c hp).ValidPos

    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.ValidPos.offset_toSetOfLE {s : String} {q p : s.ValidPos} {c : Char} {hp : p s.endValidPos} {hpq : q p} :
      (q.toSetOfLE p c hp hpq).offset = q.offset
      theorem String.Pos.Raw.isValid_add_char_set {s : String} {p : s.ValidPos} {c : Char} {hp : p s.endValidPos} :
      IsValid (p.set c hp) (p.offset + c)
      @[inline]
      def String.ValidPos.pastSet {s : String} (p : s.ValidPos) (c : Char) (hp : p s.endValidPos) :
      (p.set c hp).ValidPos

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

      Equations
      Instances For
        @[simp]
        theorem String.ValidPos.offset_pastSet {s : String} {p : s.ValidPos} {c : Char} {hp : p s.endValidPos} :
        (p.pastSet c hp).offset = p.offset + c
        @[inline]
        Equations
        Instances For
          theorem String.ValidPos.splits_pastSet {s : String} {p : s.ValidPos} {c : Char} {hp : p s.endValidPos} :
          @[inline]
          def String.ValidPos.modify {s : String} (p : s.ValidPos) (f : CharChar) (hp : p s.endValidPos) :

          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.ValidPos} {f : CharChar} {hp : p s.endValidPos} {q : Raw} (hq : IsValid s q) (hpq : q p.offset) :
            IsValid (p.modify f hp) q
            @[inline]
            def String.ValidPos.toModifyOfLE {s : String} (q p : s.ValidPos) (f : CharChar) (hp : p s.endValidPos) (hpq : q p) :
            (p.modify f hp).ValidPos

            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.ValidPos.offset_toModifyOfLE {s : String} {q p : s.ValidPos} {f : CharChar} {hp : p s.endValidPos} {hpq : q p} :
              (q.toModifyOfLE p f hp hpq).offset = q.offset
              @[inline]
              def String.ValidPos.pastModify {s : String} (p : s.ValidPos) (f : CharChar) (hp : p s.endValidPos) :
              (p.modify f hp).ValidPos

              The position just after the position that was modified in a ValidPos.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.ValidPos.set, combined with String.pos or another means of obtaining a String.ValidPos.

                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.ValidPos.set, combined with String.pos or another means of obtaining a String.ValidPos.

                    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.ValidPos.set, combined with String.pos or another means of obtaining a String.ValidPos.

                      Examples:

                      Equations
                      Instances For
                        @[irreducible, specialize #[]]
                        def String.mapAux (f : CharChar) (s : String) (p : s.ValidPos) :
                        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