Deno: how to take care of accents when reading from windows terminal prompt? - unicode

I'm trying to use Deno to read an user input, but I'm having trouble with the encoding.
Here is my simple code from a Deno shell when I enter "áé":
> const a = prompt('Insert your character:');
Insert your character: áé
undefined
> a
"��"
My Windows command prompt has the encoding in CP-1252, that is very similar to ISO-8859-1.
How do I tell Deno the encoding of the string I inputed? Or how do I decode it from cp-1252?

It's possible this behavior will change in future versions of Deno (see New prompt method gets confused when receiving special characters as input [ã] · Issue #8239 · denoland/deno) but for now if you need to use an encoding other than the one used by prompt (which currently appears to always be UTF-8) then you can directly use Deno.stdin which implements Deno.Reader.
e.g. Using readLines you can specify the encoding:
import { readLines } from "https://deno.land/std#0.105.0/io/bufio.ts";
async function prompt(
message = "Prompt",
{ encoding }: { encoding?: string } = {}
) {
await Deno.stdout.write(new TextEncoder().encode(`${message} `));
const { value } = await readLines(Deno.stdin, { encoding }).next();
return <string>value || null;
}
const a = await prompt("Insert your character:", { encoding: "windows-1252" });
console.log(a);

Related

MultipartFormDataContent encode single quote in .NET Standard

I have file which name contains single quote like Test'Quote.html
in order to upload file to server, I'm using HttpClient and MultipartFormDataContent like:
using (var formContent = new MultipartFormDataContent($"{new string('-', 10)}-{DateTime.Now.Ticks}"))
{
foreach (var file in files)
{
var fileContent = new StreamContent(file.Stream);
fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse(file.ContentType);
formContent.Add(fileContent, file.Name, file.Filename);
}
await client.PostAsync(address, formContent)
}
I check formContent and I see:
And I cannot upload file to server because it says that filename is invalid, seems that last filename is taken in consideration instead of first one.
How to disable or avoid encoding such name ?

How can I convert to a command in discord.py

def get_last_trade_price(TICKER):
Asset = json.dumps(getQuotes(TICKER))
raw = (json.loads(json.dumps(getQuotes(TICKER)))[0]["LastTradePrice"])
raw = re.sub(",", "", raw)
return float(raw)
How can I convert this to a discord.py command like prefix and then FRA for the trade price etc
Assuming you already have the discord.ext.commands.Bot instance set up properly, you could just use this:
#commands.command(name="last_trading_price")
async def get_last_trading_prive(ctx, ticker):
raw = (json.loads(json.dumps(getQuotes(ticker)))[0]["LastTradePrice"])
raw = re.sub(",", "", raw)
await ctx.send(float(raw))
For the sake of example, assume the command prefix is "!".
If you were to use the command !last_trading_price AAPL, this would send float(raw) to the channel which the command was invoked in.

Spotify PKCE in Dart/Flutter: "code_verifier was incorrect"

Using the Authorization Code Flow with PKCE of the Spotify-API I am getting the error that my code_verifier is incorrect, which has to be an encoding problem from what I know by now.
{"error":"invalid_grant","error_description":"code_verifier was incorrect"}
This is the original code I wrote:
String getAuthUrl() {
code = getRandomString(128);
// saveToPrefs("verfifier_code", code);
var hash = sha256.convert(ascii.encode(code));
String code_challenge = base64Url.encode(hash.bytes);
return Uri.parse(
"https://accounts.spotify.com/authorize?response_type=code&client_id=${widget.client_id}&redirect_uri=http%3A%2F%2Flocalhost%2Fauth&scope=user-top-read&code_challenge=$code_challenge&code_challenge_method=S256")
.toString();
}
This is how I understand the Spotify-Authorisation-Guide (https://developer.spotify.com/documentation/general/guides/authorization-guide/).
After finding this post (https://stackoverflow.com/a/63174909/14266484) I tried porting the fix to Dart but failed. As far as I understand it the code hashes the in ascii encoded code_verifier and then uses btoa() to convert it into ascii again. This function also seems to do base64 (but not base64Url, maybe this is why certain parts have to be replaced manually?).
String getAuthUrl() {
// also tried static verifier_codes for debugging, so the getRandomString() function is working properly
code = getRandomString(128);
// saveToPrefs("verfifier_code", code);
var hash = sha256.convert(ascii.encode(code));
// this does not work with either base64 or base64Url
String code_challenge = base64.encode(hash.bytes).replaceAll(RegExp(r"/\+/g"), '-').replaceAll(RegExp(r"/\//g"), '_').replaceAll(RegExp(r"/=+$/"), '');
return Uri.parse(
"https://accounts.spotify.com/authorize?response_type=code&client_id=${widget.client_id}&redirect_uri=http%3A%2F%2Flocalhost%2Fauth&scope=user-top-read&code_challenge=$code_challenge&code_challenge_method=S256")
.toString();
}
I also tried different ways of encoding:
-Using String.codeUnits (But this is using UTF-16)
-Getting a String for the sha256-function aswell the base64(-Url)-function with String.fromCharCodes() (which should be using ASCII?)
-Switching between ASCII and UTF-8 (Which should not make a difference in that case as my verifier_code is made up of ASCII-Characters only)
EDIT:
To make the requests I use:
var res = await http.post(endpoint, body: {"client_id":widget.client_id, "grant_type":"authorization_code", "code":value, "redirect_uri":"http://localhost/auth", "code_verifier":code});
After some more research I found out that the important thing that is happening is that the "=" at the end of the challenge has to be removed (Shouldn't base64Url do that?). Anyways, that is the working code:
EDITED CODE:
String getAuthUrl() {
code = getRandomString(128);
var hash = sha256.convert(ascii.encode(code));
String code_challenge = base64Url.encode(hash.bytes).replaceAll("=", "").replaceAll("+", "-").replaceAll("/", "_");
return Uri.parse(
"https://accounts.spotify.com/authorize?response_type=code&client_id=${widget.client_id}&redirect_uri=http%3A%2F%2Flocalhost%2Fauth&scope=user-top-read&code_challenge=$code_challenge&code_challenge_method=S256")
.toString();
}
EDIT:
Further "+" has to be replaced with "-" and "/" with "_"!

Flutter - Character encoding is not behaving as expected

I am parsing a local JSON file that has words containing rarely used special characters in Icelandic.
When displaying the characters I get mixed up symbols but not the characters, for some others I just get a square instead of a symbol.
I am using this type of encoding "\u00c3"
Update: Example of the characters I am using: þ, æ, ý, ð
Q: What is the best way to display those kind of characters and avoid any chance of display failures?
Update #2:
How I am parsing:
Future<Null> getAll() async{
var response = await
DefaultAssetBundle.of(context).loadString('assets/json/dictionary.json');
var decodedData = json.decode(response);
setState(() {
for(Map word in decodedData){
mWordsList.add(Words.fromJson(word));
}
});
}
The class:
class Words{
final int id;
final String wordEn, wordIsl;
Words({this.id, this.wordEn, this.wordIsl});
factory Words.fromJson(Map<String, dynamic> json){
return new Words(
id: json['wordId'],
wordEn: json['englishWord'],
wordIsl: json['icelandicWord']
);
}
}
JSON Model:
{
"wordId": 47,
"englishWord": "Age",
//Here's a String that has two special characters
"icelandicWord": "\u00c3\u00a6vi"
}
I had similar issues with accented characters. They were not displayed as expected.
This worked for me
final codeUnits = source.codeUnits;
return Utf8Decoder().convert(codeUnits);
The problem is that your JSON is stored locally.
Let's say you have
Map<String, String> jsonObject = {"info": "Æ æ æ Ö ö ö"};
So to show your text correctly you have to encode and decode back your JSON with utf-8.
I understand that's serialization and deserialization are costly operations, but's it's a workaround for locally stored JSON objects that contains UTF-8 texts.
import 'dart:convert';
jsonDecode(jsonEncode(jsonObject))["info"]
If you get that JSON from server, then it's much more simpler, for example in dio package you can chose contentType params that's is "application/json; charset=utf-8" by default.

How to get a range from start & end index with Office.js while developing MS Word Add-in

I want to get a sub range by character index from the parent paragraph. What's the suggest way to do so? The only method I found to shrink the range is "Paragraph.search()"
ref:
Word.Range: https://learn.microsoft.com/en-us/javascript/api/word/word.range?view=office-js
Word.Paragraph: https://learn.microsoft.com/en-us/javascript/api/word/word.paragraph?view=office-js
My use case:
I'm writing a markdown plugin for MS Word and I'm trying to parse the following paragraph.
A **bold** word
The output from markdown parser is {style:"strong",start:2,end:9}. So I want to apply bold style to the targeting range.
Found a way just now. The key is passing an empty separator to Paragraph.getTextRanges([""]) I'm not sure how bad the performance would be.
const makeBold = async (paragraph:Word.Paragraph,start:number,end:number) => {
const charRanges = paragraph.getTextRanges([""])
charRanges.load()
await charRanges.context.sync()
const targetRange = charRanges.items[start].expandTo(charRanges.items[end])
targetRange.load()
await targetRange.context.sync()
targetRange.font.bold = true
await targetRange.context.sync()
}
The existing answer didn't work for me (it seems like getTextRanges() no longer accepts empty strings).
So I have adapted the answer to use paragraph.search() instead.
async function getRangeByIndex(paragraph: Word.Paragraph, start: number, end: number) {
const charRanges = paragraph.search('?', { matchWildcards: true })
charRanges.load()
await charRanges.context.sync()
const targetRange = charRanges.items[start].expandTo(charRanges.items[end])
targetRange.load()
await targetRange.context.sync()
return targetRange
}