move: string projects
This commit is contained in:
Before Width: | Height: | Size: 80 B After Width: | Height: | Size: 80 B |
Before Width: | Height: | Size: 66 KiB After Width: | Height: | Size: 66 KiB |
@ -1,69 +0,0 @@
|
|||||||
// For more tutorials: https://blog.learngoprogramming.com
|
|
||||||
//
|
|
||||||
// Copyright © 2018 Inanc Gumus
|
|
||||||
// Learn Go Programming Course
|
|
||||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
|
||||||
//
|
|
||||||
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
var start, stop int
|
|
||||||
|
|
||||||
if args := os.Args[1:]; len(args) == 2 {
|
|
||||||
start, _ = strconv.Atoi(args[0])
|
|
||||||
stop, _ = strconv.Atoi(args[1])
|
|
||||||
}
|
|
||||||
|
|
||||||
if start == 0 || stop == 0 {
|
|
||||||
start, stop = 'A', 'Z'
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Printf("%-10s %-10s %-10s %-12s\n%s\n",
|
|
||||||
"literal", "dec", "hex", "encoded",
|
|
||||||
strings.Repeat("-", 45))
|
|
||||||
|
|
||||||
for n := start; n <= stop; n++ {
|
|
||||||
fmt.Printf("%-10c %-10[1]d %-10[1]x % -12x\n", n, string(n))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
EXAMPLE UNICODE BLOCKS
|
|
||||||
|
|
||||||
1 byte
|
|
||||||
------------------------------------------------------------
|
|
||||||
asciiStart = '\u0001' -> 32
|
|
||||||
asciiStop = '\u007f' -> 127
|
|
||||||
|
|
||||||
upperCaseStart = '\u0041' -> 65
|
|
||||||
upperCaseStop = '\u005a' -> 90
|
|
||||||
|
|
||||||
lowerCaseStart = '\u0061' -> 97
|
|
||||||
lowerCaseStop = '\u007a' -> 122
|
|
||||||
|
|
||||||
|
|
||||||
2 bytes
|
|
||||||
------------------------------------------------------------
|
|
||||||
latin1Start = '\u0080' -> 161
|
|
||||||
latin1Stop = '\u00ff' -> 255
|
|
||||||
|
|
||||||
|
|
||||||
3 bytes
|
|
||||||
------------------------------------------------------------
|
|
||||||
dingbatStart = '\u2700' -> 9984
|
|
||||||
dingbatStop = '\u27bf' -> 10175
|
|
||||||
|
|
||||||
|
|
||||||
4 bytes
|
|
||||||
------------------------------------------------------------
|
|
||||||
emojiStart = '\U0001f600' -> 128512
|
|
||||||
emojiStop = '\U0001f64f' -> 128591
|
|
||||||
*/
|
|
@ -1,61 +0,0 @@
|
|||||||
// For more tutorials: https://blog.learngoprogramming.com
|
|
||||||
//
|
|
||||||
// Copyright © 2018 Inanc Gumus
|
|
||||||
// Learn Go Programming Course
|
|
||||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
|
||||||
//
|
|
||||||
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"unicode/utf8"
|
|
||||||
"unsafe"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
str := "Yūgen ☯ 💀"
|
|
||||||
|
|
||||||
// can't change a string
|
|
||||||
// a string is a read-only byte-slice
|
|
||||||
// str[0] = 'N'
|
|
||||||
// str[1] = 'o'
|
|
||||||
|
|
||||||
bytes := []byte(str)
|
|
||||||
|
|
||||||
// can change a byte slice
|
|
||||||
// bytes[0] = 'N'
|
|
||||||
// bytes[1] = 'o'
|
|
||||||
|
|
||||||
str = string(bytes)
|
|
||||||
|
|
||||||
fmt.Printf("%s\n", str)
|
|
||||||
fmt.Printf("\t%d bytes\n", len(str))
|
|
||||||
fmt.Printf("\t%d runes\n", utf8.RuneCountInString(str))
|
|
||||||
fmt.Printf("% x\n", bytes)
|
|
||||||
fmt.Printf("\t%d bytes\n", len(bytes))
|
|
||||||
fmt.Printf("\t%d runes\n", utf8.RuneCount(bytes))
|
|
||||||
|
|
||||||
// fmt.Println()
|
|
||||||
// for i, r := range str {
|
|
||||||
// fmt.Printf("str[%2d] = % -12x = %q\n", i, string(r), r)
|
|
||||||
// }
|
|
||||||
|
|
||||||
fmt.Println()
|
|
||||||
fmt.Printf("1st byte : %c\n", str[0]) // ok
|
|
||||||
fmt.Printf("2nd byte : %c\n", str[1]) // not ok
|
|
||||||
fmt.Printf("2nd rune : %s\n", str[1:3]) // ok
|
|
||||||
fmt.Printf("last rune : %s\n", str[11:]) // ok
|
|
||||||
|
|
||||||
// disadvantage: each one is 4 bytes
|
|
||||||
runes := []rune(str)
|
|
||||||
|
|
||||||
fmt.Println()
|
|
||||||
fmt.Printf("%s\n", str)
|
|
||||||
fmt.Printf("\t%d bytes\n", int(unsafe.Sizeof(runes[0]))*len(runes))
|
|
||||||
fmt.Printf("\t%d runes\n", len(runes))
|
|
||||||
|
|
||||||
fmt.Printf("1st rune : %c\n", runes[0])
|
|
||||||
fmt.Printf("2nd rune : %c\n", runes[1])
|
|
||||||
fmt.Printf("first five : %c\n", runes[:5])
|
|
||||||
}
|
|
@ -1,77 +0,0 @@
|
|||||||
// For more tutorials: https://blog.learngoprogramming.com
|
|
||||||
//
|
|
||||||
// Copyright © 2018 Inanc Gumus
|
|
||||||
// Learn Go Programming Course
|
|
||||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
|
||||||
//
|
|
||||||
|
|
||||||
/*
|
|
||||||
------------------------------------------------------------
|
|
||||||
RULES
|
|
||||||
------------------------------------------------------------
|
|
||||||
|
|
||||||
* You shouldn't use a standard library function.
|
|
||||||
|
|
||||||
* You should only solve the challenge by manipulating the bytes directly.
|
|
||||||
|
|
||||||
* Manipulate the bytes of a string using indexing, slicing, appending etc.
|
|
||||||
|
|
||||||
* Be efficient: Do not use string concat (+ operator).
|
|
||||||
|
|
||||||
* Instead, create a new byte slice as a buffer from the given string argument.
|
|
||||||
|
|
||||||
* Then, manipulate it during your program.
|
|
||||||
|
|
||||||
* And, for once, print that buffer.
|
|
||||||
|
|
||||||
------------------------------------------------------------
|
|
||||||
STEPS
|
|
||||||
------------------------------------------------------------
|
|
||||||
|
|
||||||
* Mask only links starting with http://
|
|
||||||
|
|
||||||
* Don't check for uppercase/lowercase letters
|
|
||||||
* The goal is to learn manipulating bytes in strings
|
|
||||||
* It's not about creating a perfect masker
|
|
||||||
|
|
||||||
* For example: A spammer can prevent the masker like this (for now this is OK):
|
|
||||||
|
|
||||||
"Here's my spammy page: hTTp://youth-elixir.com"
|
|
||||||
|
|
||||||
* But, you should catch this:
|
|
||||||
|
|
||||||
"Here's my spammy page: http://hehefouls.netHAHAHA see you."
|
|
||||||
"Here's my spammy page: http://******************* see you."
|
|
||||||
*/
|
|
||||||
package main
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
// ---------------------------------------------------------------
|
|
||||||
// #1
|
|
||||||
// ---------------------------------------------------------------
|
|
||||||
// Check whether there's a command line argument or not
|
|
||||||
// If not, quit from the program with a message
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------
|
|
||||||
// #2
|
|
||||||
// ---------------------------------------------------------------
|
|
||||||
// Create a byte buffer as big as the argument
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------
|
|
||||||
// #3
|
|
||||||
// ---------------------------------------------------------------
|
|
||||||
// 1- Loop and detect the http:// patterns
|
|
||||||
// 2- Copy the input character by character to the buffer
|
|
||||||
// 3- If you detect http:// pattern, copy the http:// first,
|
|
||||||
// then copy the *s instead of the original link until
|
|
||||||
// you see a whitespace character.
|
|
||||||
//
|
|
||||||
// Here: http://www.mylink.com Click!
|
|
||||||
// -> Here: http://************** Click!
|
|
||||||
//
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------
|
|
||||||
// #4
|
|
||||||
// ---------------------------------------------------------------
|
|
||||||
// Print the buffer as a string
|
|
||||||
}
|
|
@ -1,7 +0,0 @@
|
|||||||
Hi guys,
|
|
||||||
|
|
||||||
Here is my new spammy webpage http://www.mysuperpage.com <-- This is my website!
|
|
||||||
|
|
||||||
Please click on the link now!!!
|
|
||||||
|
|
||||||
When you click, I will be rich, thanks!
|
|
@ -1,80 +0,0 @@
|
|||||||
// For more tutorials: https://blog.learngoprogramming.com
|
|
||||||
//
|
|
||||||
// Copyright © 2018 Inanc Gumus
|
|
||||||
// Learn Go Programming Course
|
|
||||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
|
||||||
//
|
|
||||||
|
|
||||||
/*
|
|
||||||
#1- Get and check the input
|
|
||||||
#2- Create a byte buffer and use it as the output
|
|
||||||
#3- Write input to the buffer as it is and print it
|
|
||||||
#4- Detect the link
|
|
||||||
#5- Mask the link
|
|
||||||
#6- Stop masking when a whitespace is detected
|
|
||||||
#7- Write http:// to the buffer, just before the link
|
|
||||||
*/
|
|
||||||
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
link = "http://"
|
|
||||||
nlink = len(link)
|
|
||||||
mask = '*'
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
args := os.Args[1:]
|
|
||||||
if len(args) != 1 {
|
|
||||||
fmt.Println("gimme somethin' to censor!")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
|
||||||
text = args[0]
|
|
||||||
size = len(text)
|
|
||||||
buf = make([]byte, 0, size)
|
|
||||||
|
|
||||||
in bool
|
|
||||||
)
|
|
||||||
|
|
||||||
for i := 0; i < size; i++ {
|
|
||||||
// slice the input and look for the link pattern
|
|
||||||
// do not slice it when it goes beyond the input text's capacity
|
|
||||||
if len(text[i:]) >= nlink && text[i:i+nlink] == link {
|
|
||||||
// set the flag: we're in a link! -> "http://....."
|
|
||||||
in = true
|
|
||||||
|
|
||||||
// add the "http://" manually
|
|
||||||
buf = append(buf, link...)
|
|
||||||
|
|
||||||
// jump to the next character after "http://"
|
|
||||||
i += nlink
|
|
||||||
}
|
|
||||||
|
|
||||||
// get the current byte from the input
|
|
||||||
c := text[i]
|
|
||||||
|
|
||||||
// disable the link detection flag
|
|
||||||
// this will prevent masking the rest of the bytes
|
|
||||||
switch c {
|
|
||||||
case ' ', '\t', '\n': // try -> unicode.IsSpace
|
|
||||||
in = false
|
|
||||||
}
|
|
||||||
|
|
||||||
// if we're in the link detection mode (inside the link bytes)
|
|
||||||
// then, mask the current character
|
|
||||||
if in {
|
|
||||||
c = mask
|
|
||||||
}
|
|
||||||
buf = append(buf, c)
|
|
||||||
}
|
|
||||||
|
|
||||||
// print out the buffer as text (string)
|
|
||||||
fmt.Println(string(buf))
|
|
||||||
}
|
|
@ -1,7 +0,0 @@
|
|||||||
Hi guys,
|
|
||||||
|
|
||||||
Here is my new spammy webpage http://www.mysuperpage.com <-- This is my website!
|
|
||||||
|
|
||||||
Please click on the link now!!!
|
|
||||||
|
|
||||||
When you click, I will be rich, thanks!
|
|
@ -1,38 +0,0 @@
|
|||||||
// For more tutorials: https://blog.learngoprogramming.com
|
|
||||||
//
|
|
||||||
// Copyright © 2018 Inanc Gumus
|
|
||||||
// Learn Go Programming Course
|
|
||||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
|
||||||
//
|
|
||||||
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"unicode"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
text := `Galaksinin Batı Sarmal Kolu'nun bir ucunda, haritası bile çıkarılmamış ücra bir köşede, gözlerden uzak, küçük ve sarı bir güneş vardır.
|
|
||||||
|
|
||||||
Bu güneşin yörüngesinde, kabaca yüz kırksekiz milyon kilometre uzağında, tamamıyla önemsiz ve mavi-yeşil renkli, küçük bir gezegen döner.
|
|
||||||
|
|
||||||
Gezegenin maymun soyundan gelen canlıları öyle ilkeldir ki dijital kol saatinin hâlâ çok etkileyici bir buluş olduğunu düşünürler.`
|
|
||||||
|
|
||||||
const maxWidth = 40
|
|
||||||
|
|
||||||
var lw int // line width
|
|
||||||
|
|
||||||
for _, r := range text {
|
|
||||||
fmt.Printf("%c", r)
|
|
||||||
|
|
||||||
switch lw++; {
|
|
||||||
case lw > maxWidth && r != '\n' && unicode.IsSpace(r):
|
|
||||||
fmt.Println()
|
|
||||||
fallthrough
|
|
||||||
case r == '\n':
|
|
||||||
lw = 0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fmt.Println()
|
|
||||||
}
|
|
@ -1,5 +0,0 @@
|
|||||||
Galaksinin Batı Sarmal Kolu'nun bir ucunda, haritası bile çıkarılmamış ücra bir köşede, gözlerden uzak, küçük ve sarı bir güneş vardır.
|
|
||||||
|
|
||||||
Bu güneşin yörüngesinde, kabaca yüz kırksekiz milyon kilometre uzağında, tamamıyla önemsiz ve mavi-yeşil renkli, küçük bir gezegen döner.
|
|
||||||
|
|
||||||
Gezegenin maymun soyundan gelen canlıları öyle ilkeldir ki dijital kol saatinin hâlâ çok etkileyici bir buluş olduğunu düşünürler.
|
|
@ -1,9 +0,0 @@
|
|||||||
TODO
|
|
||||||
|
|
||||||
* Write a program that dumps the bytes of the given argument
|
|
||||||
* Get the first unicode char ([]rune)
|
|
||||||
* Get the last unicode char ([]rune)
|
|
||||||
|
|
||||||
* In the masker program:
|
|
||||||
* Use copy instead of append when appendin the "http://" manually
|
|
||||||
*
|
|
@ -1,36 +0,0 @@
|
|||||||
// For more tutorials: https://blog.learngoprogramming.com
|
|
||||||
//
|
|
||||||
// Copyright © 2018 Inanc Gumus
|
|
||||||
// Learn Go Programming Course
|
|
||||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
|
||||||
//
|
|
||||||
|
|
||||||
// why maps instead of keyed arrays etc?
|
|
||||||
// 120k elements!
|
|
||||||
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"unicode/utf8"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
args := os.Args[1:]
|
|
||||||
if len(args) != 1 {
|
|
||||||
fmt.Println("[emoji]")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
moods := [...]string{
|
|
||||||
'😊': "a happy",
|
|
||||||
'😟': "a worried",
|
|
||||||
'😎': "an awesome",
|
|
||||||
'😞': "a sad",
|
|
||||||
'😩': "a crying",
|
|
||||||
}
|
|
||||||
|
|
||||||
emoji, _ := utf8.DecodeRune([]byte(args[0]))
|
|
||||||
fmt.Printf("%s is %s emoji\n", args[0], moods[emoji])
|
|
||||||
}
|
|
@ -1,18 +0,0 @@
|
|||||||
// For more tutorials: https://blog.learngoprogramming.com
|
|
||||||
//
|
|
||||||
// Copyright © 2018 Inanc Gumus
|
|
||||||
// Learn Go Programming Course
|
|
||||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
|
||||||
//
|
|
||||||
|
|
||||||
// + Masker : Use copy instead of append when appending the "http://" manually
|
|
||||||
// + Wrapper: Accept the width from the cmdline
|
|
||||||
// args, maxWidth := os.Args[1:], 40
|
|
||||||
// if len(args) == 1 {
|
|
||||||
// maxWidth, _ = strconv.Atoi(args[0])
|
|
||||||
// }
|
|
||||||
|
|
||||||
package main
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
}
|
|
Reference in New Issue
Block a user