Access a character at index in the string.

assert "abcd"[2]         == 'c'

Property definitions

core $ Text :: []
	# Access a character at `index` in the string.
	#
	# ~~~
	# assert "abcd"[2]         == 'c'
	# ~~~
	fun [](index: Int): Char do return self.chars[index]
lib/core/text/abstract_text.nit:104,2--109,53

core :: flat $ FlatText :: []
	redef fun [](index) do
		var len = _length

		# Statistically:
		# * ~70% want the next char
		# * ~23% want the previous
		# * ~7% want the same char
		#
		# So it makes sense to shortcut early. And early is here.
		var dpos = index - _position
		var b = _bytepos
		if dpos == 1 and index < len - 1 then
			var its = _items
			var c = its[b]
			if c & 0x80 == 0x00 then
				# We want the next, and current is easy.
				# So next is easy to find!
				b += 1
				_position = index
				_bytepos = b
				# The rest will be done by `dpos==0` bellow.
				dpos = 0
			end
		else if dpos == -1 and index > 1 then
			var its = _items
			var c = its[b-1]
			if c & 0x80 == 0x00 then
				# We want the previous, and it is easy.
				b -= 1
				dpos = 0
				_position = index
				_bytepos = b
				return c.code_point
			end
		end
		if dpos == 0 then
			# We know what we want (+0 or +1) just get it now!
			var its = _items
			var c = its[b]
			if c & 0x80 == 0x00 then return c.code_point
			return items.char_at(b)
		end

		assert index >= 0 and index < len
		return fetch_char_at(index)
	end
lib/core/text/flat.nit:328,2--373,4

core $ U16String :: []
	redef fun[](index: Int): Char do
		assert index >= 0 and index < length
		var offset = 0
		var c = '\0'

		for i in [0..index] do
			c = uchar_string.char_at_offset(offset, code_units)
			if c.to_i > 0xFFFF then offset += 2 else offset +=1
		end
		return c
	end
lib/core/text/u16_string.nit:92,2--102,4

core $ Concat :: []
	redef fun [](i) do
		assert i >= 0 and i < _length
		var flps = _flat_last_pos_start
		if i >= flps then
			var fc = _flat_cache
			if i < flps + fc._length then return fc.fetch_char_at(i - flps)
		end
		var lf = get_leaf_at(i)
		return lf.fetch_char_at(i - _flat_last_pos_start)
	end
lib/core/text/ropes.nit:132,2--141,4

text_stat :: text_stat $ Concat :: []
	redef fun [](i) do
		sys.index_call.inc "Concat"
		if flat_last_pos_start != -1 then
			var fsp = i - flat_last_pos_start
			if fsp >= 0 and fsp < flat_cache.length then return flat_cache[fsp]
		end
		sys.concat_cache_miss += 1
		var s: String = self
		var st = i
		loop
			if s isa FlatString then break
			s = s.as(Concat)
			var lft = s.left
			var llen = lft.length
			if i >= llen then
				s = s.right
				i -= llen
			else
				s = s.left
			end
		end
		flat_last_pos_start = st - i
		flat_cache = s
		return s[i]
	end
lib/text_stat/text_stat.nit:143,2--167,4

text_stat :: text_stat $ FlatBuffer :: []
	redef fun [](i) do
		sys.index_call.inc "FlatBuffer"
		return super
	end
lib/text_stat/text_stat.nit:251,2--254,4

core $ ASCIIFlatString :: []
	redef fun [](idx) do
		assert idx < _byte_length and idx >= 0
		return _items[idx + _first_byte].code_point
	end
lib/core/text/flat.nit:697,2--700,4