Why is the source location end off by two characters here? - llvm-clang

I'm trying to write a source to source translator using libTooling.
I'm using ASTMatchers to try to find if statements that don't have curly braces and then use a rewriter to add the braces.
The matcher I'm using is:
ifStmt(unless(hasDescendant(compoundStmt())))
Then I just get the start and end locations, and rewrite the curly braces.
Here's the source code for that:
if (const IfStmt *IfS = Result.Nodes.getNodeAs<clang::IfStmt>("ifStmt")) {
const Stmt *Then = IfS->getThen();
Rewrite.InsertText(Then->getLocStart(), "{", true, true);
Rewrite.InsertText(Then->getLocEnd(),"}",true,true);
Now the problem is that for some reason the end location is always off by 2 characters. Why is this so?

the SourceLocation i was getting is off by one because it only matches the token and ";" is not part of that.
btw, if anybody's wondering how to include the ";" into the range if they want to, you could just use Lexer::MeasureTokenLength and then add that by one and get the new SourceLocaiton by offset.

Related

Regex/Gsub removing all nested double quotes instead of just the outer ones

I want to import the following CSV into rails db:
"[{""id"":""actions"",""name"":""app"",""description""}]"
After importing, I always get:
"{\"id\":\"actions\",\"name\":\"app\",\"description\"]}"
I want the import to looks like this:
[{"id":,"actions":,"name":,"description",...}]"
I tried using .gsub!(/"/,'') but this returns:
"[{id:actions,name:actions,description:}]"
The issue is that all the quote marks have been removed so its just id insted of "id"
My code is:
def import_app_version
path = Rails.root.join('db', 'csv_export', 'app_versions.csv')
counter = 0
puts "Inserts on table app_versions started..."
CSV.foreach(path, headers: true) do |row|
next if row.to_hash['modules'] == nil
row.to_hash['modules'].gsub!(/"/,'')
next if row.to_hash['deleted_at'] != nil
counter += 1
AppVersion.skip_callbacks = true
AppVersion.create!(row.to_hash)
end
AppVersion.skip_callbacks = false
puts "#{counter} inserts on table app_versions complete"
end
What is the right way to do this so that the import works correctly and the data is imported as it's meant to be?
I have been searching for half a day, and found so many answers but they all end up removing all of the double quotes as displayed above.
Or even better would be, if someone knew a way to import csv with JSON content the right way.
Do not use replace, or do not try to remove quotes manually.
"[{""id"":""actions"",""name"":""app"",""description""}]"
If you read the above data with a csv parser/reader (maybe with a parameter 'quoted fields') all should be fine.
The surrounding quotes will be removed and the inside quotes will be unescaped to one double quote.

Ruby override .index() in String to search for a character or its HTML equivalent

So... I've been working with WYSIWYG editors, and have realized, that they occasionally replace certain characters with the hex codes for that character, like the ' or the & for example.
How do I override String's index method such that it includes these hex codes?
Like, when do somestring.index("\'hello there") how do I get it to search \' and '
note: single quote is escaped for clarity against double quotes.
what is the most efficient way to do this kind of string search?
is there something like this already built in.
Also, since I'm using external tools, I don't really have a say in the format things are in.
THE SOLUTION:
search_reg_exp = Regexp.escape(str).gsub(/(both|options|or|more)/, "(both|options|or|more)")
long_str.index(search_reg_exp)
ORIGINAL ANSWER:
String#index doesn't just work for single characters, it can be used for a substring of any length, and you can give it a regular expression which would probably be best in this case:
some_string = "Russell's teapot"
another_string = "Russell's teapot"
apostrophe_expr = /'|'/
some_string.index apostrophe_expr
# => 7
another_string.index apostrophe_expr
# => 7
Another option would be to just decode the HTML entities before you start manipulating the string. There are various gems for this including html_helpers:
require 'html_helpers'
another_string = "Russell's teapot"
yet_another_string = HTML::EntityCoder.decode_entities another_string
# => "Russell's teapot"
yet_another_string.index "'"
# => 7
yet_another_string.index ?' # bonus syntax tip--Ruby 1.9.1+
# => 7

String.replace() function to parse XML string so that it can be displayed in HTML

I have a XML string which needs to be displayed within HTML. I understand the first thing needed to be done here is to convert all '<' and '>' into '& lt;' and '& gt;' (ignore the space after & sign). This is what I am doing to replace '<' -
regExp = new RegExp("/</g");
xmlString = xmlString.replace(regExp, '& lt;');
xmlString does not change.
Also, trace(regExp.test("<")); prints false.
What is wrong here?
replace returns a new string, it doesn't modify the old one. So if you want to overwrite the old you have to do the following:
xmlString = xmlString.replace(regExp, '<');
Or if you don't want to overwrite the old one, just store the result in a new variable.
var newString = xmlString.replace(regExp, '<');
The issue is the way you create your RegExp object.
Because your using the RegExp constructor, don't include the / characters:
regExp = new RegExp("<", "g");
or use / as a shortcut:
regExp = /</g;
See this page for more details: http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/RegExp.html

Detect Numbers in Octave

I am trying the following right now:
function isNum = isItANum(string)
isNum = isempty(str2num(string))
end
The problem is if I have a date '1998/34/2', I want my function to say no.
From help str2num:
*Caution:* As `str2num' uses the `eval' function to do the
conversion, `str2num' will execute any code contained in the
string S. Use `str2double' instead if you want to avoid the use
of `eval'.
See also: str2double, eval
Looks like you can replace your function with ~isnan(str2double(string))
do a loop so that you split the string in single characters, and if any char fail, then return 0.

Removing non-alphanumeric characters in an Access Field

I need to remove hyphens from a string in a large number of access fields. What's the best way to go about doing this?
Currently, the entries are follow this general format:
2010-54-1
2010-56-1
etc.
I'm trying to run append queries off of this field, but I'm always getting validation errors causing the query to fail. I think the cause of this failure is the hypens in the entries, which is why I need to remove them.
I've googled, and I see that there are a number of formatting guides using vbscript, but I'm not sure how I can integrate vb into Access. It's new to me :)
Thanks in advance,
Jacques
EDIT:
So, Ive run a test case with some values that are simply text. They don't work either, the issue isn't the hyphens.
I'm not sure that the hyphens are actually the problem without seeing sample data / query but if all you need to do is get rid of them, the Replace function should be sufficient (you can use this in the query)
example: http://www.techonthenet.com/access/functions/string/replace.php
If you need to do some more advanced string manipulation than this (or multiple calls to replace) you might want to create a VBA function you can call from your query, like this:
http://www.pcreview.co.uk/forums/thread-2596934.php
To do this you'd just need to add a module to your access project, and add the function there to be able to use it in your query.
I have a function I use when removing everything except Alphanumeric characters. Simply create a query and use the function in the query on whatever field you are trying to modify. Runs much faster than find and replace.
Public Function AlphaNumeric(inputStr As String)
Dim ascVal As Integer, originalStr As String, newStr As String, counter As Integer, trimStr As String
On Error GoTo Err_Stuff
' send to error message handler
If inputStr = "" Then Exit Function
' if nothing there quit
trimStr = Trim(inputStr)
' trim out spaces
newStr = ""
' initiate string to return
For counter = 1 To Len(trimStr)
' iterate over length of string
ascVal = Asc(Mid$(trimStr, counter, 1))
' find ascii vale of string
Select Case ascVal
Case 48 To 57, 65 To 90, 97 To 122
' if value in case then acceptable to keep
newStr = newStr & Chr(ascVal)
' add new value to existing new string
End Select
Next counter
' move to next character
AlphaNumeric = newStr
' return new completed string
Exit Function
Err_Stuff:
' handler for errors
MsgBox Err.Number & " " & Err.Description
End Function
Just noticed the link to the code, looks similar to mine. Guess this is just another option.