I have an example of what I need decoded:
#"für"
How can I get this decoded to an NSString so it looks like
fūr
I tried a number of things including
[#"für" stringByDecodingHTMLEntities] > für
[#"für" gtm_stringByUnescapingFromHTML] > für
but no luck.
Thanks!
UPDATE based on vikingosegundo's solution
NSLog(#"möchte decoded to : > %#", [#"möchte" stringByEncodingHTMLEntities] );
NSLog(#"möchte decoded to: > %#", [#"möchte" stringByDecodingHTMLEntities] );
NSLog(#"für decoded to : > %#", [#"für" stringByEncodingHTMLEntities] );
NSLog(#"für decoded to: > %#", [#"für" stringByDecodingHTMLEntities] );
NSLog(#"möchte decoded:%#", [#"möchte" stringByDecodingHTMLEntitiesComma] );
NSLog(#"für decoded:%#", [#"für" stringByDecodingHTMLEntitiesComma] );
möchte decoded to : > möchte
möchte decoded to: > möchte
für decoded to : > für
für decoded to: > für
möchte decoded:möchte
für decoded:für
Note :
stringByEncodingHTMLEntities is from https://github.com/mwaterfall/MWFeedParser/tree/master/Classes
stringByDecodingHTMLEntitiesComma is from vikingosegundo's category.
try
[#"für" stringByDecodingHTMLEntities];
or
[#"für" gtm_stringByUnescapingFromHTML];
(note the ;)
This category also works with out the missing ;.
Related
I have this string in a code.txt file.
"class Solution {\u000Apublic:\u000A vector\u003Cvector\u003Cint\u003E\u003E insert(vector\u003Cvector\u003Cint\u003E\u003E\u0026 intervals, vector\u003Cint\u003E\u0026 newInterval) {\u000A int len \u003D intervals.size()\u003B\u000A int index \u003D 0\u003B\u000A vector\u003Cvector\u003Cint\u003E \u003E ans\u003B\u000A \u000A\u000A while(index \u003C len \u0026\u0026 intervals[index][1] \u003C newInterval[0]) ans.push_back(intervals[index++])\u003B\u000A \u000A while(index \u003C len \u0026\u0026 intervals[index][0] \u003C\u003D newInterval[1]) {\u000A newInterval[0] \u003D min(intervals[index][0], newInterval[0])\u003B\u000A newInterval[1] \u003D max(intervals[index][1], newInterval[1])\u003B\u000A index++\u003B\u000A }\u000A \u000A ans.push_back(newInterval)\u003B\u000A \u000A while(index \u003C len) ans.push_back(intervals[index++])\u003B\u000A\u000A return ans\u003B \u000A }\u000A}\u003B "
I would like to convert this string to C++ syntex and write to solution.cpp file.
The content in solution.cpp will look like..
class Solution {
public:
vector<vector<int>> insert(vector<vector<int>>& intervals, vector<int>& newInterval) {
int len = intervals.size();
int index = 0;
vector<vector<int> > ans;
while(index < len && intervals[index][1] < newInterval[0]) ans.push_back(intervals[index++]);
while(index < len && intervals[index][0] <= newInterval[1]) {
newInterval[0] = min(intervals[index][0], newInterval[0]);
newInterval[1] = max(intervals[index][1], newInterval[1]);
index++;
}
ans.push_back(newInterval);
while(index < len) ans.push_back(intervals[index++]);
return ans;
}
};
I have tried enforcing/converting encoding to UTF-8 but the string stays the same.
code = File.read('code.txt')
code = code.encode('UTF-8')
file = File.open('solution.cpp', "w:UTF-8")
file.write(code)
How can I do this? Thank you.
So, I have tried to reproduce your problem and got the same result as described by using your solution.
I have noticed that \u003B (for example) is a unicode code for semicolon character. So, I analyzed the string for each "U+" notation using regex /\\u(.{4})/, as it marks "hexadecimal digits" as being Unicode code points. Then used gsub! and Array#pack to convert and substitute each of the Unicode chars.
[$1.to_i(16)].pack('U') # => "\n", "\n", "<", "&", "\n", "=" ...etc.
And finally wrote the result to a file. So, my final approach looks like this:
code = File.read('code.txt')
code.gsub!(/\\u(.{4})/) do |match|
[$1.to_i(16)].pack('U')
end
File.open('solution.cpp', 'w') { |f| f.puts code.gsub!(/\A"|"\Z/, '') }
Also note, I have used gsub again at the end, to search for the leading or trailing quote and replace it with an empty string when writing to a file.
I have csv file containing some data like:
374,Test Comment multiplelines \n Here's the 2nd line,Other_Data
Where 374 is the object ID from doors, then some commentary and then some other data.
I have a piece of code that reads the data from the CSV file, stores it in the appropriate variables and then writes it to the doors Object.
Module Openend_module = edit("path_to_mod", true,true,true)
Object o ;
Column c;
string attrib;
string oneLine ;
string OBJECT_ID = "";
string Comment = "";
String Other_data = "";
int offset;
string split_text(string s)
{
if (findPlainText(s, sub, offset, len, false))
{
return s[0 : offset -1]
}
else
{
return ""
}
}
Stream input = read("Path_to_Input.txt");
input >> oneLine
OBJECT_ID = split_text(oneLine)
oneLine = oneLine[offset+1:]
Comment = split_text(oneLine)
Other_data = oneLine[offset+1:]
When using print Comment the output in the DXL console is : Test Comment multiplelines \n Here's the 2nd line
for o in Opened_Module do
{
if (o."Absolute Number"""==OBJECT_ID ){
attrib = "Result_Comment " 2
o.attrib = Comment
}
}
But after writing to the doors object, the \n is not taken into consideration and the result is as follows:
I've tried putting the string inside a Buffer and using stringOf() but the escape character just disappeared.
I've also tried adding \r\n and \\n to the input csv text but still no luck
This isn't the most efficient way of handling this, but I have a relatively straightforward fix.
I would suggest adding the following:
Module Openend_module = edit("path_to_mod", true,true,true)
Object o ;
Column c;
string attrib;
string oneLine ;
string OBJECT_ID = "";
string Comment = "";
String Other_data = "";
int offset;
string split_text(string s)
{
if (findPlainText(s, sub, offset, len, false))
{
return s[0 : offset -1]
}
else
{
return ""
}
}
Stream input = read("Path_to_Input.txt");
input >> oneLine
OBJECT_ID = split_text(oneLine)
oneLine = oneLine[offset+1:]
Comment = split_text(oneLine)
Other_data = oneLine[offset+1:]
//Modification to comment string
int x
int y
while ( findPlainText ( Comment , "\\n" , x , y , false ) ) {
Comment = ( Comment [ 0 : x - 1 ] ) "\n" ( Comment [ x + 2 : ] )
}
This will run the comment string through a parser, replacing string "\n" with the char '\n'. Be aware- this will ignore any trailing spaces at the end of a line.
Let me know if that helps.
I am trying to design a parser using Ragel and C++ as host langauge.
There is a particular case where a parameter can be defined in two formats :
a. Integer : eg. SignalValue = 24
b. Hexadecimal : eg. SignalValue = 0x18
I have the below code to parse such a parameter :
INT = ((digit+)$incr_Count) %get_int >!(int_error); #[0-9]
HEX = (([0].'x'.[0-9A-F]+)$incr_Count) %get_hex >!(hex_error); #[hexadecimal]
SIGNAL_VAL = ( INT | HEX ) %/getSignalValue;
However in the above defined parser command, only the integer values(as defined in section a) gets recognized and parsed correctly.
If an hexadecimal number(eg. 0x24) is provided, then the number gets stored as ´0´ . There is no error called in case of hexadecimal number. The parser recognizes the hexadecimal, but the value stored is '0'.
I seem to be missing out some minor details with Ragel. Has anyone faced a similar situation?
The remaning part of the code :
//Global
int lInt = -1;
action incr_Count {
iGenrlCount++;
}
action get_int {
int channel = 0xFF;
std::stringstream str;
while(iGenrlCount > 0)
{
str << *(p - iGenrlCount);
iGenrlCount--;
}
str >> lInt; //push the values
str.clear();
}
action get_hex {
std::stringstream str;
while(iGenrlCount > 0)
{
str << std::hex << *(p - iGenrlCount);
iGenrlCount--;
}
str >> lInt; //push the values
}
action getSignalValue {
cout << "lInt = " << lInt << endl;
}
It's not a problem with your FSM (which looks fine for the task you have), it's more of a C++ coding issue. Try this implementation of get_hex():
action get_hex {
std::stringstream str;
cout << "get_hex()" << endl;
while(iGenrlCount > 0)
{
str << *(p - iGenrlCount);
iGenrlCount--;
}
str >> std::hex >> lInt; //push the values
}
Notice that it uses str just as a string buffer and applies std::hex to >> from std::stringstream to int. So in the end you get:
$ ./a.out 245
lInt = 245
$ ./a.out 0x245
lInt = 581
Which probably is what you want.
I have a file called log_file with following text:
....some text....
line wire (1)
mode : 2pair , annex : a
coding : abcd
rate : 1024
status : up
....some text....
line wire (2)
mode : 4pair , annex : b
coding : xyz
rate : 1024
status : down
....some text....
The values may differ but the attributes are always the same. Is there a way to find each line wire and display their attributes? The number of line wires also may differ.
EDIT: File doesn't have any blank lines. There are more attributes but only these are needed. Can I get like the first "n" lines, instead of searching for every line? i.e if there is line wire (1), copy that line plus the next 4 lines.
And I am copying the searched lines to a output file $fout, which I have used earlier in the script with the same $line.
Given your sample:
set fh [open log_file r]
while {[gets $fh line] != -1} {
switch -glob -- $line {
{line wire*} {puts $line}
{mode : *} -
{coding : *} -
{rate : *} -
{status : *} {puts " $line"}
}
}
close $fh
outputs
line wire (1)
mode : 2pair , annex : a
coding : abcd
rate : 1024
status : up
line wire (2)
mode : 4pair , annex : b
coding : xyz
rate : 1024
status : down
Edit: print the next "n" lines following the "line wire" line to a file
set in [open log_file r]
set out [open log_file_filtered w]
set n 4
while {[gets $in line] != -1} {
if {[string match {line wire*} $line]} {
puts $line
for {set i 1} {$i <= $n} {incr i} {
if {[gets $in line] != -1} {
puts $out " $line"
}
}
}
}
close $fh
I have a file with the following format:
/Users/devplayerx/Sandbox/pics/images/001012DG-161.JPG
pixelWidth: 1600
pixelHeight: 1050
filename: 001012DG-161.JPG
/Users/devplayerx/Sandbox/pics/images/001019DG-151 COPY.JPG
pixelWidth: 1600
pixelHeight: 1050
filename: 001019DG-151 COPY.JPG
and would like to, ultimately, have an iOS dictionary with the filename as key, and either a dictionary or array with the pixelWidth and pixelHeight as value. I was considering converting my text file into a JSON file, and then parse it using NSJSONSerialization, but I'm not sure how to convert my text file into JSON. Also, I'd like to remove the full path from the text file, since it's not needed.
Here is a perl script that seems to do the job:
#!/usr/bin/perl
use strict;
use warnings;
open FILE,"< yourfile.txt" or die "I/O error : $!\n";
my $w = 0;
my $h = 0;
my $f = "";
print "{\n";
while (my $line = <FILE>)
{
if ($f)
{
print ",\n";
$f = "";
}
if ($line =~ /pixelWidth: ([0-9]+)/)
{
$w = $1;
}
if ($line =~ /pixelHeight: ([0-9]+)/)
{
$h = $1;
}
if ($line =~ /filename: (.*)$/)
{
$f = $1;
print "\t\"$f\" : [ $w, $h ]"
}
}
print "\n}\n";
close FILE;
Note that I'm not an expert in perl so maybe it can be improved, but using it on your input file seems to produce your expected JSON as below:
prompt$ perl scr.pl
{
"001012DG-161.JPG" : [ 1600, 1050 ],
"001019DG-151 COPY.JPG" : [ 1600, 1050 ]
}
Note that once you have your JSON file, you may optionally convert it into a PLIST file using the plutil tool. For example perl scr.pl | plutil -convert binary1 -o yourfile.plist - will create yourfile.plist from the JSON produced by perl scr.pl (my script above). You can then easily read this file in your code using [NSDictionary dictionaryWithContentsOfFile:pathToYourFilePlist] and directly have access to your data as an NSDictionary in one line.
The json object could look like this:
{ picture: { path : "thePath", pixelWidth: 1600, pixelHeight: 1050, filename : "name" }}
I would then convert it by taking the rows and putting them in a list, looping through the list and eventually spitting everything out in another text file using the above object notation.
What language do you need to write the tooling to convert it to json in?
You can use NSInputStream to read the text file, on each iteration you can build your dictionary the way you want.
After that just use NSJSONSerialization.