+ # Gives the formatted string back as a Nit string with `args` in place
+ #
+ # assert "This %1 is a %2.".format("String", "formatted String") == "This String is a formatted String."
+ # assert "\\%1 This string".format("String") == "\\%1 This string"
+ fun format(args: Object...): String do
+ var s = new Array[Text]
+ var curr_st = 0
+ var i = 0
+ while i < length do
+ # Skip escaped characters
+ if self[i] == '\\' then
+ i += 1
+ # In case of format
+ else if self[i] == '%' then
+ var fmt_st = i
+ i += 1
+ var ciph_st = i
+ while i < length and self[i].is_numeric do
+ i += 1
+ end
+ i -= 1
+ var fmt_end = i
+ var ciph_len = fmt_end - ciph_st + 1
+ s.push substring(curr_st, fmt_st - curr_st)
+ s.push args[substring(ciph_st, ciph_len).to_i - 1].to_s
+ curr_st = i + 1
+ end
+ i += 1
+ end
+ s.push substring(curr_st, length - curr_st)
+ return s.to_s
+ end
+
+ # Copies `n` bytes from `self` at `src_offset` into `dest` starting at `dest_offset`
+ #
+ # Basically a high-level synonym of NativeString::copy_to
+ #
+ # REQUIRE: `n` must be large enough to contain `len` bytes
+ #
+ # var ns = new NativeString(8)
+ # "Text is String".copy_to_native(ns, 8, 2, 0)
+ # assert ns.to_s_with_length(8) == "xt is St"
+ #
+ fun copy_to_native(dest: NativeString, n, src_offset, dest_offset: Int) do
+ var mypos = src_offset
+ var itspos = dest_offset
+ while n > 0 do
+ dest[itspos] = self.chars[mypos]
+ itspos += 1
+ mypos += 1
+ n -= 1
+ end
+ end
+