+ # Trims trailing and preceding white spaces
+ # A whitespace is defined as any character which ascii value is less than or equal to 32
+ #
+ # assert " Hello World ! ".trim == "Hello World !"
+ # assert "\na\nb\tc\t".trim == "a\nb\tc"
+ fun trim: String
+ do
+ if self.length == 0 then return self.to_s
+ # find position of the first non white space char (ascii < 32) from the start of the string
+ var start_pos = 0
+ while self.chars[start_pos].ascii <= 32 do
+ start_pos += 1
+ if start_pos == length then return ""
+ end
+ # find position of the first non white space char from the end of the string
+ var end_pos = length - 1
+ while self.chars[end_pos].ascii <= 32 do
+ end_pos -= 1
+ if end_pos == start_pos then return self.chars[start_pos].to_s
+ end
+ return self.substring(start_pos, end_pos - start_pos + 1)
+ end
+
+ # Mangle a string to be a unique string only made of alphanumeric characters
+ fun to_cmangle: String
+ do
+ var res = new FlatBuffer
+ var underscore = false
+ for c in self.chars do
+ if (c >= 'a' and c <= 'z') or (c >='A' and c <= 'Z') then
+ res.add(c)
+ underscore = false
+ continue
+ end
+ if underscore then
+ res.append('_'.ascii.to_s)
+ res.add('d')
+ end
+ if c >= '0' and c <= '9' then
+ res.add(c)
+ underscore = false
+ else if c == '_' then
+ res.add(c)
+ underscore = true
+ else
+ res.add('_')
+ res.append(c.ascii.to_s)
+ res.add('d')
+ underscore = false
+ end
+ end
+ return res.to_s
+ end
+
+ # Escape " \ ' and non printable characters using the rules of literal C strings and characters
+ #
+ # assert "abAB12<>&".escape_to_c == "abAB12<>&"
+ # assert "\n\"'\\".escape_to_c == "\\n\\\"\\'\\\\"
+ fun escape_to_c: String
+ do
+ var b = new FlatBuffer
+ for c in self.chars do
+ if c == '\n' then
+ b.append("\\n")
+ else if c == '\0' then
+ b.append("\\0")
+ else if c == '"' then
+ b.append("\\\"")
+ else if c == '\'' then
+ b.append("\\\'")
+ else if c == '\\' then
+ b.append("\\\\")
+ else if c.ascii < 32 then
+ b.append("\\{c.ascii.to_base(8, false)}")
+ else
+ b.add(c)
+ end
+ end
+ return b.to_s
+ end
+
+ # Escape additionnal characters
+ # The result might no be legal in C but be used in other languages
+ #
+ # assert "ab|\{\}".escape_more_to_c("|\{\}") == "ab\\|\\\{\\\}"
+ fun escape_more_to_c(chars: String): String
+ do
+ var b = new FlatBuffer
+ for c in escape_to_c do
+ if chars.chars.has(c) then
+ b.add('\\')
+ end
+ b.add(c)
+ end
+ return b.to_s
+ end
+
+ # Escape to c plus braces
+ #
+ # assert "\n\"'\\\{\}".escape_to_nit == "\\n\\\"\\'\\\\\\\{\\\}"
+ fun escape_to_nit: String do return escape_more_to_c("\{\}")
+
+ # Return a string where Nit escape sequences are transformed.
+ #
+ # Example:
+ # var s = "\\n"
+ # assert s.length == 2
+ # var u = s.unescape_nit
+ # assert u.length == 1
+ # assert u[0].ascii == 10 # (the ASCII value of the "new line" character)
+ fun unescape_nit: String
+ do
+ var res = new FlatBuffer.with_capacity(self.length)
+ var was_slash = false
+ for c in self do
+ if not was_slash then
+ if c == '\\' then
+ was_slash = true
+ else
+ res.add(c)
+ end
+ continue
+ end
+ was_slash = false
+ if c == 'n' then
+ res.add('\n')
+ else if c == 'r' then
+ res.add('\r')
+ else if c == 't' then
+ res.add('\t')
+ else if c == '0' then
+ res.add('\0')
+ else
+ res.add(c)
+ end
+ end
+ return res.to_s
+ end
+
+end
+
+# Common subclass for String and Buffer
+abstract class AbstractString
+ super Text
+
+ private var items: NativeString
+
+ redef var length: Int
+
+ init do end