add: strings and projects
This commit is contained in:
54
20-project-spam-masker/01-step-1/main.go
Normal file
54
20-project-spam-masker/01-step-1/main.go
Normal file
@@ -0,0 +1,54 @@
|
||||
// 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 whitespace is detected
|
||||
#7- Put a http:// prefix in front of the masked link
|
||||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
args := os.Args[1:]
|
||||
if len(args) != 1 {
|
||||
fmt.Println("gimme somethin' to mask!")
|
||||
return
|
||||
}
|
||||
|
||||
const (
|
||||
link = "http://"
|
||||
nlink = len(link)
|
||||
)
|
||||
|
||||
var (
|
||||
text = args[0]
|
||||
size = len(text)
|
||||
buf = make([]byte, 0, size)
|
||||
)
|
||||
|
||||
for i := 0; i < size; i++ {
|
||||
buf = append(buf, text[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 {
|
||||
}
|
||||
}
|
||||
|
||||
// print out the buffer as text (string)
|
||||
fmt.Println(string(buf))
|
||||
}
|
7
20-project-spam-masker/01-step-1/spam.txt
Normal file
7
20-project-spam-masker/01-step-1/spam.txt
Normal file
@@ -0,0 +1,7 @@
|
||||
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!
|
80
20-project-spam-masker/02-step-2/main.go
Normal file
80
20-project-spam-masker/02-step-2/main.go
Normal file
@@ -0,0 +1,80 @@
|
||||
// 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 whitespace is detected
|
||||
✅ #7- Put a http:// prefix in front of the masked 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 mask!")
|
||||
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))
|
||||
}
|
7
20-project-spam-masker/02-step-2/spam.txt
Normal file
7
20-project-spam-masker/02-step-2/spam.txt
Normal file
@@ -0,0 +1,7 @@
|
||||
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!
|
68
20-project-spam-masker/03-step-2-no-append/main.go
Normal file
68
20-project-spam-masker/03-step-2-no-append/main.go
Normal file
@@ -0,0 +1,68 @@
|
||||
// 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 whitespace is detected
|
||||
✅ #7- Put a http:// prefix in front of the masked 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 mask!")
|
||||
return
|
||||
}
|
||||
|
||||
var (
|
||||
text = args[0]
|
||||
size = len(text)
|
||||
|
||||
// create a byte buffer directly from the string (text)
|
||||
buf = []byte(text)
|
||||
|
||||
in bool
|
||||
)
|
||||
|
||||
for i := 0; i < size; i++ {
|
||||
// no need to add an artificial http:// prefix
|
||||
// it's already there
|
||||
if len(text[i:]) >= nlink && text[i:i+nlink] == link {
|
||||
in = true
|
||||
i += nlink
|
||||
}
|
||||
|
||||
switch text[i] {
|
||||
case ' ', '\t', '\n': // try -> unicode.IsSpace
|
||||
in = false
|
||||
}
|
||||
|
||||
// when censoring mode: on
|
||||
// directly manipulate the bytes on the buffer
|
||||
if in {
|
||||
buf[i] = mask
|
||||
}
|
||||
}
|
||||
fmt.Println(string(buf))
|
||||
}
|
7
20-project-spam-masker/03-step-2-no-append/spam.txt
Normal file
7
20-project-spam-masker/03-step-2-no-append/spam.txt
Normal file
@@ -0,0 +1,7 @@
|
||||
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!
|
60
20-project-spam-masker/README.md
Normal file
60
20-project-spam-masker/README.md
Normal file
@@ -0,0 +1,60 @@
|
||||
# Spam Masker Challenge Tips
|
||||
|
||||
## 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.
|
||||
|
||||
* Mask only links starting with `http://`
|
||||
|
||||
* Don't check for uppercase/lowercase letters
|
||||
|
||||
* The goal is learning how to manipulate bytes in strings, it's not about creating the 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:
|
||||
|
||||
```
|
||||
INPUT:
|
||||
Here's my spammy page: http://hehefouls.netHAHAHA see you.
|
||||
|
||||
OUTPUT:
|
||||
Here's my spammy page: http://******************* see you.
|
||||
```
|
||||
|
||||
## Steps:
|
||||
|
||||
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. Loop and detect the `http://` patterns
|
||||
|
||||
4. Copy the input character by character to the buffer
|
||||
|
||||
5. If you detect `http://` pattern, copy the `http://` first, then copy the `*`s instead of the original link until you see whitespace character.
|
||||
|
||||
For example:
|
||||
```
|
||||
INPUT:
|
||||
Here: http://www.mylink.com Click!
|
||||
|
||||
OUTPUT:
|
||||
Here: http://************** Click!
|
||||
```
|
||||
|
||||
6. Print the buffer as a string
|
Reference in New Issue
Block a user