string: title() and small fixes

pull/1752/head
Swastik Baranwal 2019-08-26 16:02:53 +05:30 committed by Alexander Medvednikov
parent 754b8082fb
commit db525524ee
4 changed files with 77 additions and 22 deletions

View File

@ -818,7 +818,6 @@ fn new_v(args[]string) *V {
dir: dir
lang_dir: vroot
table: new_table(obfuscate)
out_name: out_name
out_name_c: out_name_c
cgen: new_cgen(out_name_c)
vroot: vroot

View File

@ -321,7 +321,7 @@ pub fn (s string) left(n int) string {
}
return s.substr(0, n)
}
// 'hello'.right(2) => 'llo'
pub fn (s string) right(n int) string {
if n >= s.len {
return ''
@ -447,6 +447,9 @@ pub fn (s string) count(substr string) int {
if s.len == 0 || substr.len == 0 {
return 0
}
if substr.len > s.len {
return 0
}
mut n := 0
mut i := 0
for {
@ -480,7 +483,7 @@ pub fn (s string) ends_with(p string) bool {
// TODO only works with ASCII
pub fn (s string) to_lower() string {
mut b := malloc(s.len)// TODO + 1 ??
mut b := malloc(s.len + 1)
for i := 0; i < s.len; i++ {
b[i] = C.tolower(s.str[i])
}
@ -488,13 +491,31 @@ pub fn (s string) to_lower() string {
}
pub fn (s string) to_upper() string {
mut b := malloc(s.len)// TODO + 1 ??
mut b := malloc(s.len + 1)
for i := 0; i < s.len; i++ {
b[i] = C.toupper(s.str[i])
}
return tos(b, s.len)
}
pub fn (s string) capitalize() string {
sl := s.to_lower()
cap := sl[0].str().to_upper() + sl.right(1)
return cap
}
pub fn (s string) title() string {
words := s.split(' ')
mut tit := []string
for word in words {
tit << word.capitalize()
}
title := tit.join(' ')
return title
}
// 'hey [man] how you doin'
// find_between('[', ']') == 'man'
pub fn (s string) find_between(start, end string) string {

View File

@ -39,12 +39,12 @@ fn test_lt() {
d := 'b'
e := 'aa'
f := 'ab'
assert a<(b)
assert !(b<c)
assert c<(d)
assert !(d<e)
assert c<(e)
assert e<(f)
assert a < (b)
assert !(b < c)
assert c < (d)
assert !(d < e)
assert c < (e)
assert e < (f)
}
fn test_ge() {
@ -53,11 +53,11 @@ fn test_ge() {
c := 'ab'
d := 'abc'
e := 'aaa'
assert b>=(a)
assert c>=(b)
assert d>=(c)
assert !(c>=d)
assert e>=(a)
assert b >= (a)
assert c >= (b)
assert d >= (c)
assert !(c >= d)
assert e >= (a)
}
fn test_compare_strings() {
@ -91,15 +91,15 @@ fn test_split() {
mut s := 'volt/twitch.v:34'
mut vals := s.split(':')
assert vals.len == 2
assert vals[0]== 'volt/twitch.v'
assert vals[1]== '34'
// /////////
assert vals[0] == 'volt/twitch.v'
assert vals[1] == '34'
// /////////
s = '2018-01-01z13:01:02'
vals = s.split('z')
assert vals.len == 2
assert vals[0]=='2018-01-01'
assert vals[1]== '13:01:02'
// /////////////
assert vals[0] =='2018-01-01'
assert vals[1] == '13:01:02'
// //////////
s = '4627a862c3dec29fb3182a06b8965e0025759e18___1530207969___blue'
vals = s.split('___')
assert vals.len == 3
@ -214,7 +214,6 @@ fn test_runes() {
assert u.substr(1, 2) == 'р'
assert s2.ustring().at(1) == 'r'
assert u.at(1) == 'р'
// ///////
first := u.at(0)
last := u.at(u.len - 1)
assert first.len == 2
@ -236,6 +235,22 @@ fn test_lower() {
assert s.to_lower() == 'hi'
}
fn test_upper() {
mut s := 'a'
assert s.to_upper() == 'A'
assert s.to_upper().len == 1
s = 'hello'
assert s.to_upper() == 'HELLO'
assert s.to_upper().len == 5
s = 'Aloha'
assert s.to_upper() == 'ALOHA'
s = 'have a nice day!'
assert s.to_upper() == 'HAVE A NICE DAY!'
s = 'hi'
assert s.to_upper() == 'HI'
}
fn test_left_right() {
s := 'ALOHA'
assert s.left(3) == 'ALO'
@ -355,3 +370,21 @@ fn test_count() {
assert 'aabbaa'.count('aa') == 2
assert 'bbaabb'.count('aa') == 1
}
fn test_capitalize() {
mut s := 'hello'
assert s.capitalize() == 'Hello'
s = 'test'
assert s.capitalize() == 'Test'
s = 'i am ray'
assert s.capitalize() == 'I am ray'
}
fn test_title() {
mut s := 'hello world'
assert s.title() == 'Hello World'
s.to_upper()
assert s.title() == 'Hello World'
s.to_lower()
assert s.title() == 'Hello World'
}

View File

@ -2,6 +2,8 @@ module stats
import math
// TODO: Implement all of them with generics
// This module defines the following statistical operations on f64 array
// ---------------------------
// | Summary of Functions |