Skip to content

Instantly share code, notes, and snippets.

View adworse's full-sized avatar

Dima Ermilov adworse

  • Apptopia
  • Varna, Bulgaria
View GitHub Profile
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)
def counter(string) do
string
|> :unicode.characters_to_binary(:utf8, :utf16)
|> byte_size()
|> div(2)
end
@adworse
adworse / not nice.ex
Last active September 13, 2020 01:30
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)
@adworse
adworse / wtf.js
Last active September 13, 2020 01:10
% 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
@adworse
adworse / wtf.ex
Last active September 13, 2020 13:26
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
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}
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
@adworse
adworse / naive2.ex
Last active September 13, 2020 00:21
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})
@adworse
adworse / naive.ex
Last active September 13, 2020 13:24
naïve
defmodule Naive do
def trim_name(name) do
name |> String.slice(0, 80)
end
end

Show git branch and ruby version in bash prompt

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]/'
}