ColdFusion 128-bit unsigned int to IPv6 - coldfusion

I've posted a function that will convert an IPv6 address to a 128-bit unsigned int value here: ColdFusion IPv6 to 128-bit unsigned int
I need a function that will go in the other direction now.
This function turned out to be more complicated and I'll explain the complications in the answer.

Here is the function that will convert a 128-bit unsigned int to an IPv6 address with the correct (concise) IPv6 formatting.
Explanations:
Part of the problem with a function like this is that the number passed into the function (nUInt128) is not guaranteed to be a 128-bit unsigned int. It might be 8-bits (::1) or even weird stuff like a signed 136-bit number (ColdFusion/Java seems to prefer signed ints). Not having exactly a 128-bit number that gets converted to a Java Byte array with 16 values will cause java.net.Inet6Address.getAddress() to throw an error. My solution was to create a ColdFusion array with 16 zeros and back-fill it and then use that with java.net.Inet6Address.getAddress(). I was surprised that this worked as I have no idea how large the numbers are in that array. ColdFusion/Java somehow did some magic and turned the array into a Byte[]. The back-fill also strips off the numbers that are larger and fixes the 136-bit signed int problem.
<cffunction name="UInt128ToIPv6" returntype="string" output="no" access="public" hint="returns IPv6 address for uint128 number">
<cfargument name="nUInt128" type="numeric" required="yes" hint="uint128 to convert to ipv6 address">
<cfif arguments.nUInt128 EQ 0>
<cfreturn "">
</cfif>
<cftry>
<cfset local['javaMathBigInteger'] = CreateObject("java", "java.math.BigInteger").init(arguments.nUInt128)>
<cfset local['JavaNetInet6Address'] = CreateObject("java", "java.net.Inet6Address")>
<cfset local['arrBytes'] = local.javaMathBigInteger.toByteArray()>
<!--- correct the array length if !=16 bytes --->
<cfset local['arrFixedBytes'] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]>
<cfif arrayLen(local.arrBytes) NEQ 16>
<cfset local['nFixedIndex'] = 16>
<cfset local['nBytesIndex'] = arrayLen(local.arrBytes)>
<cfloop condition="local.nFixedIndex NEQ 0 && local.nBytesIndex NEQ 0">
<cfset local.arrFixedBytes[local.nFixedIndex] = local.arrBytes[local.nBytesIndex]>
<cfset local.nFixedIndex-->
<cfset local.nBytesIndex-->
</cfloop>
</cfif>
<!--- /correct the array length if !=16 bytes --->
<cfif arrayLen(local.arrBytes) NEQ 16>
<cfset local['vcIPv6'] = local.JavaNetInet6Address.getByAddress(local.arrFixedBytes).getHostAddress()>
<cfelse>
<cfset local['vcIPv6'] = local.JavaNetInet6Address.getByAddress(local.javaMathBigInteger.toByteArray()).getHostAddress()>
</cfif>
<cfcatch type="any">
<cfset local['vcIPv6'] = "">
</cfcatch>
</cftry>
<cfreturn formatIPv6(vcIPv6 = local.vcIPv6)>
</cffunction>
Here is the formatIPv6() utility function that is called at the end of the previous function.
<cffunction name="formatIPv6" returntype="string" output="yes" access="public" hint="returns a compressed ipv6 address">
<cfargument name="vcIPv6" type="string" required="yes" hint="IPv6 address">
<!--- inside reReplace removes leading zeros, outside reReplace removes repeating ":" and "0:" --->
<cfreturn reReplace(reReplace(LCase(arguments.vcIPv6), "(:|^)(0{0,3})([1-9a-f]*)", "\1\3", "all"), "(^|:)[0|:]+:", "::", "all")>
</cffunction>
If you have any suggestions/questions, please leave comments.

Related

ColdFusion locale specific currency inputs in forms using commas (ie french Canadian 2 222,22$)

Is there an easier/shorter way to convert the final comma of a currency input to a decimal.
Inputs look like 2 000,99 OR 2,000,99
MySQL wants them to look like 2000.99
<cfform action="commatest.cfm?gotime" onsubmit="commatest.cfm" method="post" name="waiv">
<cfinput type="text" name="commer">
<input type="submit" style="width:180px;" value="convertme" class="hide button">
</cfform>
<cfif isdefined("gotime")>
<!--- START SCRIPT --->
<cfset formentry = FORM.commer>
<cfset howlong = #len(formentry)#>
<cfif howlong GT 3>
<cfset leftlen = howlong - 3>
<cfset rside = #right(formentry, 3)#>
<cfset rside = ReReplace(rside,"[,]",".", "ALL")>
<cfset lside = #left(FORM.commer, leftlen)#>
<cfset lside = ReReplaceNoCase(lside,"[-$A-Z,]","", "ALL")>
<cfset lside = reReplace(lside, "[[:space:]]", "", "ALL") />
<cfset newb = #lside# & #rside#>
<!--- OUTPUT TO DATABASE (or webpage in this case) --->
<cfoutput>
<h1>#newb# (number? #IsNumeric(newb)#)</h1>
</cfoutput>
<cfelse>
<cfoutput>
<h1>#formentry# (number? #IsNumeric(formentry)#)</h1>
</cfoutput>
</cfif>
</cfif>
As usual there are several different ways to do this. I would probably do something like this:
<cfscript>
source1 = '2 000,99';
source2 = '2,000,99';
// remove ALL commas and spaces
example1 = REReplace(source1,"[\s,]","","all");
example2 = REReplace(source2,"[\s,]","","all");
// insert a decimal before the last two digits
example1 = Insert(".",example1,(Len(example1)-2));
example2 = Insert(".",example2,(Len(example2)-2));
writeOutput(source1 & " = " & example1);
writeOutput("<br>");
writeOutput(source2 & " = " & example2);
</cfscript>
That code gives the following output:
2 000,99 = 2000.99
2,000,99 = 2000.99
Of course this assumes that the last two digits are always going to be after the decimal point.
Here is a gist of the code above.

Any better way to loop through a collection (struct) in CFML?

Please have a look at the code block below:
<cfset index = 0 />
<cfloop collection="#anotherPerson#" item="key" >
<cfset index = index+1 />
<cfoutput>
#key# : #anotherPerson[key]#
<cfif index lt ArrayLen(structKeyArray(anotherPerson))> , </cfif>
</cfoutput>
</cfloop>
<!--- Result
age : 24 , haar : Blondes haar , sex : female , ort : Hanau
---->
Now can you please tell me how could I achieve the same result without setting an index outside and incrementing it inside the loop? If you notice carefully, I had to write two more cfset tag and one cfif tag with expensive code just to avoid a comma (,) at the end of the collection!
Ok, I'm showing you two answers. The first will run on ColdFusion 9. Since other people might find this thread and be using Lucee Server or a newer version of Adobe ColdFusion, I'm including a one-liner that uses higher order functions and runs on ACF 2016. There's a lot of syntactic sugar (like member functions) and functional programming you're missing by being on CF9. These answers use script, because manipulating data is not something for a view (where tags/templating are used).
Set up the data
myStruct = { 'age'=24, 'haar'='Blondes haar', 'sex'='female', 'ort'='Hanau' };
CF9 compat, convert data to array and use delimiter to add commas
myArray = [];
for( key in myStruct ) {
arrayAppend( myArray, key & ' : ' & myStruct[ key ] );
}
writeOutput( arrayToList( myArray, ', ' ) );
Modern CFML. Use struct reduction closure to convert each key into an aggregated array which is then turned into a list.
writeOutput( myStruct.reduce( function(r,k,v,s){ return r.append( k & ' : ' & s[ k ] ); }, [] ).toList( ', ' ) );
http://cfdocs.org/structreduce
Some friends provided two different solutions. Both are efficient and elegant!
Solution 1
<cfset isFirst = true />
<cfloop collection="#anotherPerson#" item="key" >
<cfif isFirst>
<cfset isFirst = false />
<cfelse>
,
</cfif>
<cfoutput>
#key# : #anotherPerson[key]#
</cfoutput>
</cfloop>
Solution 2
<cfset resultList = "" />
<cfloop collection="#anotherPerson#" item="key" >
<cfset resultList = ListAppend(resultList, "#key# : #anotherPerson[key]#" ) />
</cfloop>
Cheers!
Just trim the comma when you are done, no skip logic required.
<cfset html = '' />
<cfloop collection="#anotherPerson#" item="key" >
<cfset html &= "#key# : #anotherPerson[key]# , " />
</cfloop>
<cfset html = left(html,len(html)-3) />
<cfoutput>#html#</cfoutput>
Readable, simple, works.

CFSpreadsheet Formatting Assistance

I have a spreadsheet query bringing back results. Negative numbers are formatted as ([$$123.12]) and positive numbers are formatted as ("$$123.12").
I need to format the negative number as -123.12 and the positive number as 123.12 before being inserted into a db. What type of regex would I need to use to do that? Or, could I use ColdFusion's Replace() function..and, if so, how?
We created xList as holder for the variable to simulate a looping query.
Assuming negative numbers will always contain "[" and positive numbers don't have the brackets we will loop on the list and check for "[" to format the negative numbers and then format the positive numbers for values not having brackets.
<cfset xlist = '([$$123.12]),("$$123.12")'>
<cfloop list="#xlist#" index="x">
<cfif FindNoCase("[", x )>
<cfset xVal = "-" & rereplaceNoCase(x,"[^0-9.]","","all" )>
<cfelse>
<cfset xVal = rereplaceNoCase(x,"[^0-9.]","","all" )>
</cfif>
<cfdump var="#xVal#"><br>
</cfloop>
Here's a slightly modified version of when I did something similar. Basically, I'm treating the bracket as a negative sign, and stripping out other irrelevant characters, like $. I wasn't clear on whether or not the parenthesis were part of your answer, in which case those would need to be stripped out, too.
<cffunction name="launderMoney">
<cfargument name="value">
<cfset var multiplier = 1>
<cfset arguments.value = Replace(trim(arguments.value), '"', "", "all")>
<cfif Find("[", arguments.value)>
<cfset multiplier = -1>
<cfset arguments.value = Replace(Replace(arguments.value, "[", "", "all"), "]", "", "all")>
</cfif>
<cfset var temp = Trim(Replace(Replace(trim(arguments.value), "$", "", "all"), ",", "", "all"))>
<cfif isNumeric(temp)>
<cfset temp *= multiplier>
</cfif>
<cfreturn temp>
</cffunction>
<p>#LaunderMoney('"$$123.12"')#</p>
<p>#LaunderMoney('[$$123.12]')#</p>

How to find special character from the string

I need to find the special character index in string and it as to be stored in an array variable.
The below code showing only the first special character index but i need to know all index of all the special character in the string .
Tell me how to do
<cfoutput>
<cfset start_var = ArrayNew(1)>
<cfset g_invalidUsername_Char= "!###$%^&*()_+=<>?/\,`~}{[];:.|\""0123456789">
<cfset Firstname="ab!cd!efgh">
<cfif FindOneOf(g_invalidUsername_Char,(trim(FirstName))) NEQ 0>
<cfset QAQC = 0>
<cfset AR=" Firstname can only have ' and - .<br>">
<cfset ARClass ="ARBad">
<cfset start_var= findoneof(g_invalidUsername_Char,FirstName)>
#start_var#
#RemoveChars(FirstName, start_var, 1)#
</cfif>
</cfoutput>
I tried Rereplace function too
<cfoutput>
<cfset start_var = ArrayNew(1)>
<cfset g_invalidUsername_Char= "! # ## $ % & * ( ) _ + = <> ? / , ` ~ } { [ ] ; : . | "" 0 1 2 3 4 5 6 7 8 9 \ ">
<cfset FirstName="ab!cd""efgh">
<cfset var_num=0>
<cfif FindOneOf(g_invalidUsername_Char,(trim(FirstName))) NEQ 0>
<cfset QAQC = 0>
<cfset AR=" Firstname can only have ' and - .<br>">
<cfset ARClass ="ARBad">
<cfloop list="#g_invalidUsername_Char#" index="x" delimiters=" ">
#x#<br>
<cfset FirstName = ReReplace(FirstName,"[#x#]", "", "ALL")>
#FirstName#
</cfloop>
</cfif>
</cfoutput>
but in rereplace function these "\^" two special character is not accepting

ColdFusion: pick first non null value from a list

In JavaScript, you can do this:
var a = null;
var b = "I'm a value";
var c = null;
var result = a || b || c;
And 'result' will get the value of 'b' because JavaScript short-circuits the 'or' operator.
I want a one-line idiom to do this in ColdFusion and the best I can come up with is:
<cfif LEN(c) GT 0><cfset result=c></cfif>
<cfif LEN(b) GT 0><cfset result=b></cfif>
<cfif LEN(a) GT 0><cfset result=a></cfif>
Can anyone do any better than this?
ColdFusion doesn't have nulls.
Your example is basing the choice on which item is an empty string.
If that is what you're after, and all your other values are simple values, you can do this:
<cfset result = ListFirst( "#a#,#b#,#c#" )/>
(Which works because the standard list functions ignore empty elements.)
Note: other CFML engines do support nulls.
If we really are dealing with nulls (and not empty strings), here is a function that will work for Railo and OpenBlueDragon:
<cffunction name="FirstNotNull" returntype="any" output="false">
<cfset var i = 0/>
<cfloop index="i" from="1" to="#ArrayLen(Arguments)#">
<cfif NOT isNull(Arguments[i]) >
<cfreturn Arguments[i] />
</cfif>
</cfloop>
</cffunction>
Then to use the function is as simple as:
<cfset result = FirstNotNull( a , b , c ) />