/
PrependTillN.go
43 lines (40 loc) · 1.29 KB
/
PrependTillN.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package str
// PrependTillN () repeatedly prepends a prepend to a subject, until the length of the subject is at
// least N chars in size.
//
// Inputs
//
// input 0: The subject.
//
// input 1: The prepend. If value is an empty string, input 0 is returned.
//
// input 2: Mininum length (N) input 0 must attain before the function stops preprending input 1 to
// input 0. The highest value of N is the highest value of type int. However, when the value of N is
// very high (like 1000000000 (1 billion)), this function could block indefinitely, due to the way Go
// works (behaviour has been confirmed in Go versions <= 1.12.4). If this function starts to block
// with high values of N, you can lower your N, but increase the number of times the function is
// called. So, instead of this:
//
// text = str.PrependTillN (text, "hi!", 1000000000)
//
// you can do this:
//
// text = str.PrependTillN (text, "hi!", 500000000)
//
// text = str.PrependTillN (text, "hi!", 500000000)
//
// Outpts
//
// outpt 0: Result of operation.
func PrependTillN (subject, prepend string, minLen int) (string) {
if prepend == "" {
return subject
}
for {
if len (subject) >= minLen {
break
}
subject = prepend + subject
}
return subject
}