convert utf codes to letters - ruby-on-rails

I'm using Ruby
I have such value in the database
25.02.2020 13:57:56:\nu:633;644;627;645; u:627;633;62a;627;62f; u:645;6cc;634;647; u:644;637;641; u:6a9;646;6cc;62f; u:622;645;648;632;634; u:6a9;627;631; u:628;627; u:627;6cc;646; u:628;631;646;627;645;647; u:627;632; u:635;641;631; u:62a;627; u:635;62f; u:631;648; u:628;641;631;645;627;6cc;6cc;62f; u:645;645;646;648;646; u:627;632; u:634;645;627;\n\n\nСервис
is it possible to convert it to normal persian letters with a ruby?
it must be like this
13:57:56:
سلام استاد میشه لطف کنید آموزش کار با این برنامه از صفر تا صد رو بفرمایید ممنون از شما
Сервис
I will be appreciative for your help

The Unicode values in your string have a specific pattern:
u:___;___;___; where each ___ is a hexadecimal value representing a codepoint.
You can match that pattern using a regexp, and replace the encoded values with their respective Unicode chars via gsub:
str = '25.02.2020 13:57:56:\nu:633;644;627;645; u:627;633;62a;627;62f; u:645;6cc;634;647; u:644;637;641; u:6a9;646;6cc;62f; u:622;645;648;632;634; u:6a9;627;631; u:628;627; u:627;6cc;646; u:628;631;646;627;645;647; u:627;632; u:635;641;631; u:62a;627; u:635;62f; u:631;648; u:628;641;631;645;627;6cc;6cc;62f; u:645;645;646;648;646; u:627;632; u:634;645;627;\n\n\nСервис'
str.gsub(/u:((?:\h+;)+)/) { Regexp.last_match(1).split(';').map(&:hex).pack('U*') }
#=> "25.02.2020 13:57:56:\\nسلام استاد میشه لطف کنید آموزش کار با این برنامه از صفر تا صد رو بفرمایید ممنون از شما\\n\\n\\nСервис"
Step by step: (for each match)
the regexp matches "u:633;644;627;645;"
Regexp.last_match(1) returns the 1st capture group "633;644;627;645;"
split(';') turns that into ["633", "644", "627", "645"]
map(&:hex) converts the elements to [1587, 1604, 1575, 1605]
pack('U*') interprets them as Unicode codepoints and returns "سلام"

Related

Replace last comma on string Lua

I have this string
Apples, Oranges, Grapes
How can i replace last comma for and?
Havent learn enough of patterns but i've tried several ways such as
str:gsub(",$", "and", 1)
Isnt supposed that magic character $ reads string from end --> begin?
My problem becomes because im concatenating an array using table.concat
Your table:
local t = {"Apples", "Oranges", "Grapes"}
Method #1:
Concatenate the last item using "and":
local s = #t > 1
and table.concat(t, ", ", 1, #t-1).." and "..t[#t]
or table.concat(t, ", ")
print(s) --> Apples, Oranges and Grapes
Method #2:
Replace the last comma:
local s = table.concat(t, ", ")
s = s:gsub("(.*),", "%1 and")
print(s) --> Apples, Oranges and Grapes
local str = "Apples, Oranges, Grapes"
print(str:gsub(",(%s+%w+)$", " and%1"))

texreg: numbers as text instead of math

I am using texreg for regression output. I would like to align on decimals in the table. However, all numbers are in math mode, between '$' characters. Is there an option to only enclose the significance stars in '$', leaving the numbers as regular text?
Used stringr to modify the output of texreg. For anyone interested:
tex <- texreg(list(model1,model2,model3,model4),
digits = 3,
stars=c(0.1,0.05,0.01,0.001),
symbol='+')
#
# convert numbers to text, grabbing stars and keeping between math symbols.
library(stringr)
newTex <- str_replace_all(tex,"\\$\\(?-?\\d+(,\\d+)*(\\.\\d+(e\\d+)?)?\\)?\\^?\\{?(\\*+)?\\+?\\}?\\$",
function(x) {
if(str_detect(x,"\\^")) {
ret <- sub("^","$^",sub("$","",x,fixed=T),fixed=T)
} else {
ret <- gsub("$","",x,fixed=T)
}
return(ret)
})

jsPDF: How to render Ä Ü Ö?

I am using jsPDF to create a pdf file from a webpage. I like to add a text but if use ä ö ü it won't be rendered correctly.
I tried to add Unicode characters to my pdf using the documentation, but the docs they have added for the library related to the Unicode part doesn't include any examples. It would be great if you could provide an example.
This is my code:
function doDocData(doc) {
var number_of_pages = doc.internal.getNumberOfPages()
var pdf_pages = doc.internal.pages
doc.setFontStyle('normal');//optional
for (var i = 1; i < pdf_pages.length; i++) {
doc.setPage(i)
var strl = 'I like to add Ä Ü Ö';
doc.setFontSize(10);// optional
doc.text(strl, 20, doc.internal.pageSize.height - 10);//key is the interal pageSize
var str = 'Seite ' + i + ' von ' + number_of_pages;
doc.setFontSize(10);// optional
doc.text(str, doc.internal.pageSize.width - 80, doc.internal.pageSize.height - 10);//key is the interal pageSize
}
}
For utf-8 encoded Characters like Ä, Ö, Ü you need to include a font, that is utf-8 encoded (like it is stated in the Documentary: Use of Unicode Characters / UTF-8) since jsPDF by default contains only ASCII font.
A font like this (ARIAL utf-8 ATTENTION: opens download of arialuni.ttf) can be converted by jsPDF own fontconverter.
You can find a code example how to include the converted font here: setFont in jsPDF

Lua pattern replace uppercase letters

I need a special Lua pattern that takes all the uppercase letters in a string, and replaces them with a space and the respective lowercase letter;
TestStringOne => test string one
this isA TestString => this is a test string
Can it be done?
Assuming only ASCII is used, this works:
function lowercase(str)
return (str:gsub("%u", function(c) return ' ' .. c:lower() end))
end
print(lowercase("TestStringOne"))
print(lowercase("this isA TestString"))
function my(s)
s = s:gsub('(%S)(%u)', '%1 %2'):lower()
return s
end
print(my('TestStringOne')) -->test string one
print(my('this isA TestString')) -->this is a test string

Get an array from a string of numbers separated with comma

How can I convert a string like s = "6.1101,17.592,3.3245\n" to numbers in Lua.
In python, I usually do
a = s.strip().split(',')
a = [float(i) for i in a]
What is the proper way to do this with Lua?
This is fairly trivial; just do a repeated match:
for match in s:gmatch("([%d%.%+%-]+),?") do
output[#output + 1] = tonumber(match)
end
This of course assumes that there are no spaces in the numbers.

Resources