export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/[\1]/'
}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule StringTrimmer do | |
def trim_utf8(string, len), do: trim(string, len, &utf8_counter/1) | |
def trim_utf16(string, len), do: trim(string, len, &utf16_counter/1) | |
def trim(string, len, counter) do | |
string | |
|> String.graphemes() | |
|> Enum.reduce_while({0, ""}, fn grapheme, {count, acc} -> | |
count = count + counter.(grapheme) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def counter(string) do | |
string | |
|> :unicode.characters_to_binary(:utf8, :utf16) | |
|> byte_size() | |
|> div(2) | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iex> name = "समुद्र के भीतर अचानक जब बड़ी तेज़ हलचल होने लगती है तो उसमें तूफान उठता है जिससे ऐसी लंबी और बहुत ऊंची लहरों का रेला उठना शुरू हो जाता"\ | |
|> Naive.trim_name | |
"समुद्र के भीतर अचानक जब बड़ी तेज़ हलचल होने लगती है तो उसमें तूफान उठता है जिससे ऐसी लंबी और बहुत ऊंची लहरों का" | |
iex> name |> String.length | |
80 | |
iex> Repo.insert(%Company{name: name}) | |
** (Postgrex.Error) ERROR 22001 (string_data_right_truncation) value | |
too long for type character varying(80) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% node | |
Welcome to Node.js v14.5.0. | |
Type ".help" for more information. | |
> "Sunny! 𐍈𐍈 Yesterday my life was filled with rain, \ | |
... Oh Sunny 𐍈𐍈You smiled at me re".length | |
84 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iex> trimmed = "Sunny! 𐍈𐍈 Yesterday my life was filled with rain, \ | |
Oh Sunny 𐍈𐍈You smiled at me really eased the pain" \ | |
|> LessNaive.trim_string | |
"Sunny! 𐍈𐍈 Yesterday my life was filled with rain, Oh Sunny 𐍈𐍈You smiled at me re" | |
iex> trimmed |> String.length | |
80 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule LessNaive do | |
def trim_string(string) do | |
string | |
|> String.graphemes() | |
|> Enum.reduce_while({0, ""}, fn grapheme, {count, acc} -> | |
count = count + counter(grapheme) | |
if count <= 80, | |
do: {:cont, {count, acc <> grapheme}}, | |
else: {:halt, acc} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule LessNaive do | |
def trim_string(string) do | |
string | |
|> String.graphemes() | |
|> Enum.reduce_while("", fn grapheme, acc -> | |
if counter(acc <> grapheme) <= 80, | |
do: {:cont, acc <> grapheme}, | |
else: {:halt, acc} | |
end) | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Naive do | |
def trim_name do | |
name | |
|> String.codepoints | |
|> Enum.take(80) | |
|> to_string | |
end | |
end | |
iex> Repo.insert(%Company{name: name |> Naive.trim_name}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Naive do | |
def trim_name(name) do | |
name |> String.slice(0, 80) | |
end | |
end |
NewerOlder