uncrustify: newline before opening brace for function definitions - uncrustify

Using uncrustify 0.60
I set nl_fcall_brace=force expecting the following result: newline between function arguments and the opening brace of function body
string toNsdName(const string& sid)
{
if (sid.empty())
{
return "";
}
string temp(sid);
replace_all(temp, PERIOD_MARK, DASH_MARK);
return temp;
}
--- Instead code remains ---
string toNsdName(const string& sid) {
if (sid.empty())
{
return "";
}
string temp(sid);
replace_all(temp, PERIOD_MARK, DASH_MARK);
return temp;
}

Apparently what was needed is "nl_fdef_brace=add"

Related

How to replace query parameters into intercalated url?

Hello i am want to know given a an url saved as a string with placeholders.Is there anyway to just replace the spaceholders with the desired values?
public string Constant= #"/main/url/[id]/something/[value]";
public string Replace(int id,string value)
{
string url=Replace(id,value,Constant); // "/main/url/3/something/abc"
}
As you can see the url is intercalated with variables.Is there any class provided by the framework that i could use like:
public class Replacer
{
public string FillUrl(List<object> variables,string url)
{
var fullUrl=Replace(variables,url);
return fullUrl;
}
}
You can use the String.Replace (docs):
Returns a new string in which all occurrences of a specified Unicode
character or String in the current string are replaced with another
specified Unicode character or String.
public string Replace (string oldValue, string newValue);
url = url.Replace("[id]", id.ToString()).Replace('[value]', value);

How to check if a text contains an element of an ArrayList

I have a text and an array list. I would like to check if my text contains any element in the ArrayList, and if he does find it, to replace the space between two words with "%".
Here's my code :
public static boolean stringContainsItemFromList(String text, ArrayList<String> list)
{
for (String i: list)
{
if (text.contains(list[i]))
{
text.replaceAll(" ", "%");
System.out.println("done");
return true;
}
}
return false;
}
It gives me this error: string cannot be converted to int for the line if (text.contains(list[i])). Any idea how else can I proceed?
i is a String. In that case list[i] does not make sense, since i inside of list[i] should be an index, which is an integer (e.g. list[0], list[1], but can not be list["hello"]).
So if you want to check that your text contains a String from list, you can use
if(text.contains(i))
Also, be aware that your result of
text.replaceAll(" ", "%");
is being ignored. If you want to keep the altered text, you can do that by storing the result of the replace into a new, separate variable.
Your error is because i is a string and you're using it as a int
public static boolean stringContainsItemFromList(String text, ArrayList<String> list)
{
for (String i: list) // HERE: i is an element of the list, not the index!
{
if(text.contains(list[i])) // HERE: list[string] -> error!
{
text.replaceAll(" ", "%");
System.out.println("done");
return true;
}
}
return false;
}
replace text.contains(list[i]) for text.contains(i) and try to use more descriptive var names :)
Something makes me believe, you actually need this !
public static boolean stringContainsItemFromList(String text, ArrayList<String> list)
{
boolean flag = false;
for (String i: list) {
if(text.contains(i)) {
text = text.replaceAll(i, "%");
flag = true;
}
}
System.out.println(text);
System.out.println("done");
return flag;
}
ArrayList<String> list is not an array, it is a list. So you can get the elements of list by calling get() method.
e.g.
list.get(0) // returns the first element of the list
Also
for (String i: list) {
// ...
}
is a foreach loop, a different version of
for(int x=0; x<list.size(); x++) {
// ...
}
i corresponds to list.get(x)
You should change text.contains(list[i]) to text.contains(i) because i stands for list.get(x)
Q: How to check if a text contains an element of an ArrayList
After fixing the error you'll see that the variable text has not changed. Because in Java strings are immutable and replaceAll() method returns a new string. So you should assign the new value to the text variable:
text = text.replaceAll("\\s", "%");
Here is the corrected version of the stringContainsItemFromList() method:
public static boolean stringContainsItemFromList(String text, List<String> list)
{
for (String i: list) {
if(text.contains(i)) {
text = text.replaceAll("\\s", "%");
System.out.println("done");
System.out.println(text);
return true;
}
}
return false;
}

ServiceStack Redis client Get<T>(key) removes quotes from string data

I am using ServiceStack.Redis library to work with Redis. To start with, I have implemented this solution. The get/set methods work fine for plain text/string.
Now when I save a string with quotes (with escape char), it saves properly (I verify the same in redis-cli). But the Get method returns string having all the double quotes removed.
For example saving this string - "TestSample" is saved and get as expected. Also,
saving "TestSample \"with\" \"quotes\"" is fine and shows same in redis-cli. But the output of Get method becomes "TestSample with quotes"
public bool SetDataInCache<T>(string cacheKey, T cacheData)
{
try
{
using (_redisClient = new RedisClient(_cacheConfigs.RedisHost))
{
_redisClient.As<T>().SetValue(cacheKey, cacheData, new TimeSpan(0,0,300));
}
return true;
}
catch (Exception ex)
{
return false;
}
}
public T GetDataFromCacheByType<T>(string cacheKey)
{
T retVal = default(T);
try
{
using (_redisClient = new RedisClient(_cacheConfigs.RedisHost))
{
if (_redisClient.ContainsKey(cacheKey))
{
var wrapper = _redisClient.As<T>();
retVal = wrapper.GetValue(cacheKey);
}
return retVal;
}
}
catch (Exception ex)
{
return retVal;
}
}
Usage:
cacheObj.SetDataInCache("MyKey1","TestSample");
cacheObj.SetDataInCache("MyKey2","TestSample \"with\" \"quotes\"");
string result1 = Convert.ToString(cacheObj.GetDataFromCacheByType<string>("MyKey1"));
string result2 = Convert.ToString(cacheObj.GetDataFromCacheByType<string>("MyKey2"));
Actual : "TestSample with quotes"
Expected : "TestSample \"with\" \"quotes\""
The Typed Generic API is only meant for creating a generic Redis Client for serializing complex types. If you're implementing a generic cache you should use the IRedisClient APIs instead, e.g:
_redisClient.Set(cacheKey, cacheData, new TimeSpan(0,0,300));
Then retrieve back with:
var retVal = _redisClient.Get<T>(cacheKey);
Alternatively for saving strings or if you want to serialize the POCOs yourself you can use the IRedisClient SetValue/GetValue string APIs, e.g:
_redisClient.SetValue(cacheKey, cacheData.ToJson());
var retVal = _redisClient.GetValue(cacheKey).FromJson<T>();
Note: calling IRedisClient.ContainsKey() performs an additional unnecessary Redis I/O operation, since you're returning default(T) anyway you should just call _redisClient.Get<T>() which returns the default value for non-existing keys.

Custom format functions for StringTemplate4

I would like to know how to create a custom format function for string template. Let say I have the following code:
render(attributes) :: <<
<html>
$atributes: {
<div> $customformat(atribute.name)$</div>
}
</html>
>>
customformat(name) ::= <<
$name; format="upper"$
>>
Currently the behaviour of the function customformat is:
Input: "hello world" -> Output: "HELLO WORLD"
And I would like to modify the customformat function so the output is something like the following:
Input: "hello world" -> Output: "HELLO_WORLD"
As far as I'm aware this isn't possible, since StringTemplate is all about strict model-view separation.
Instead, I think you'd be better off having a getter in the controller that returned the formatted string.
You might find this question useful: embed java code inside a template
Actually, I found a simple way of doing this which avoids the need for the formatted string getters:
You need to create a new StringRenderer which can format the string in the way you want.
public class MyStringRenderer extends StringRenderer
{
#Override
public String toString(Object o, String formatString, Locale locale) {
if (!("upperAndUnder".equals(formatString)))
return super.toString(o, formatString, locale);
// we want upper case words with underscores instead of spaces
return ((String) o).replaceAll(" ", "_").toUpperCase(locale);
}
}
Then you'll need to let the template group know about the new renderer:
public static void main(String[] args) {
STGroup templates = new STGroupFile("test.stg");
templates.registerRenderer(String.class, new MyStringRenderer());
ST renderTemplate = templates.getInstanceOf("render");
renderTemplate.add("attributes", new String[]{"blahh blahh I'm a string", "I'm another string"});
System.out.println(renderTemplate.render());
}
Then you can call the format function like you did before, but pass "upperAndUnder" as the parameter:
group test;
delimiters "$","$"
render(attributes) ::= <<
<html>
$attributes:{ attribute | <div> $customFormat(attribute)$</div>}; separator="\n"$
</html>
>>
customFormat(name) ::= <<
$name; format="upperAndUnder"$
>>
which prints:
<html>
<div> BLAHH_BLAHH_I'M_A_STRING</div>
<div> I'M_ANOTHER_STRING</div>
</html>
FYI:
Here's the original StringRenderer code
More info on Renderers
Try this one
Object rendering using AttributeRenderer
public class BasicFormatRenderer implements AttributeRenderer {
public String toString(Object o) {
return o.toString();
}
public String toString(Object o, String formatName) {
if (formatName.equals("toUpper")) {
return o.toString().toUpperCase();
} else if (formatName.equals("toLower")) {
return o.toString().toLowerCase();
} else {
throw new IllegalArgumentException("Unsupported format name");
}
}}

JNA - output parameter in DLL

How can I get the output parameter, which is in a method in a C++ DLL?
public class MyClass {
public int error = 0;
public String MyMethod(){
String str = null;
error = error(str);
if (error == 0){
return str;
}
else
return null;
}
public native int error(String outputparam);
static {
Native.register("MyDLL");
}
}
See this JNA FAQ entry, which explains how to extract a "returned" string from a buffer used as a parameter.