From a409a60b11063c22d71bf75a69e97176c5d3c64e Mon Sep 17 00:00:00 2001 From: Robin Martijn Date: Thu, 27 Jun 2019 02:03:19 +0200 Subject: [PATCH] string.reverse() (#641) --- builtin/string.v | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/builtin/string.v b/builtin/string.v index c7db31df53..54f93d0b79 100644 --- a/builtin/string.v +++ b/builtin/string.v @@ -771,6 +771,19 @@ fn (s[]string) join_lines() string { return s.join('\n') } +fn (s string) reverse() string { + mut res := string { + len: s.len + str: malloc(s.len + 1) + } + + for i := s.len - 1; i >= 0; i-- { + res[s.len-i-1] = s[i] + } + + return res +} + // 'hello'.limit(2) => 'he' // 'hi'.limit(10) => 'hi' fn (s string) limit(max int) string {