Jump to content

String formatting question


Recommended Posts

I have a bunch of functions, like this:

CODE
Func _constant_p (object)

;~Return T iff the argument is a CycL constant

;~Single value returned satisfies BOOLEANP.

EndFunc

Func _constant_external_id (constant)

;~Return the external id of CONSTANT.

;~CONSTANT must satisfy CONSTANT-P.

;~Single value returned satisfies CONSTANT-EXTERNAL-ID-P or is NIL.

EndFunc

Func _constant_internal_id (constant)

;~Return the internal id of CONSTANT.

;~CONSTANT must satisfy CONSTANT-P.

;~Single value returned satisfies CONSTANT-INTERNAL-ID-P or is NIL.

EndFunc

Func _constant_name (constant)

;~Return the name of CONSTANT or :unnamed.

;~CONSTANT must satisfy CONSTANT-P.

EndFunc

Func _find_constant (name)

;~Return the constant with NAME, or NIL if not present.

;~NAME must satisfy STRINGP.

;~Single value returned satisfies CONSTANT-P or is NIL.

EndFunc

Func _find_constant_by_external_id (external-id)

;~Return the constant with EXTERNAL-ID, or NIL if not present.

;~EXTERNAL-ID must satisfy CONSTANT-EXTERNAL-ID-P.

;~Single value returned satisfies CONSTANT-P or is NIL.

EndFunc

I'm trying to write a script that will take the "_some_function_name" text and convert it to "_someFunctionName"

The rest is within my grasp, but I can't figure out the easy way to do this. I think some stringRegExp magic might help, but I'm a RE newb... can anyone help?

Link to comment
Share on other sites

I have a bunch of functions, like this:

CODE
Func _constant_p (object)

;~Return T iff the argument is a CycL constant

;~Single value returned satisfies BOOLEANP.

EndFunc

Func _constant_external_id (constant)

;~Return the external id of CONSTANT.

;~CONSTANT must satisfy CONSTANT-P.

;~Single value returned satisfies CONSTANT-EXTERNAL-ID-P or is NIL.

EndFunc

Func _constant_internal_id (constant)

;~Return the internal id of CONSTANT.

;~CONSTANT must satisfy CONSTANT-P.

;~Single value returned satisfies CONSTANT-INTERNAL-ID-P or is NIL.

EndFunc

Func _constant_name (constant)

;~Return the name of CONSTANT or :unnamed.

;~CONSTANT must satisfy CONSTANT-P.

EndFunc

Func _find_constant (name)

;~Return the constant with NAME, or NIL if not present.

;~NAME must satisfy STRINGP.

;~Single value returned satisfies CONSTANT-P or is NIL.

EndFunc

Func _find_constant_by_external_id (external-id)

;~Return the constant with EXTERNAL-ID, or NIL if not present.

;~EXTERNAL-ID must satisfy CONSTANT-EXTERNAL-ID-P.

;~Single value returned satisfies CONSTANT-P or is NIL.

EndFunc

I'm trying to write a script that will take the "_some_function_name" text and convert it to "_someFunctionName"

The rest is within my grasp, but I can't figure out the easy way to do this. I think some stringRegExp magic might help, but I'm a RE newb... can anyone help?

Not sure how you want to use the function name afterwards, but the easiest way to convert "_some_function_name" text to "_someFunctionName" is

$newName = "_" & StringReplace($OldName,"_","")

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Actually, I just used the search/replace function in Scite, searched for _a and replaced it with _A, and then _b and _B, and so on, til they were all formatted correctly.

After that I replaced all "_" with "", and then replaced "Func " with "Func _". I now have neatly camelcased functions, and can fill them in after I build my wrapper template.

Thanks, though! the original script I've been using to generate the wrapper used StringReplace alot.

Link to comment
Share on other sites

And crap, it didn't do me any good. I forgot that I have to return the original function name with the parameter as a string in order for this to be of any use.

><

Much thanks, adding your suggestion into the original script.

Link to comment
Share on other sites

And crap, it didn't do me any good. I forgot that I have to return the original function name with the parameter as a string in order for this to be of any use.

><

Much thanks, adding your suggestion into the original script.

I notice I ignored the fact that you wanted to capitalise the characters after the second underscore.

I can't see how to do it with stringregexpreplace, so here's one way

$splitup = Stringsplit($oldname,"_")
$name = "_" & $splitup[2]
For $n = 3 To $splitup[0]
    $First = StringUpper(StringLeft($splitup[$n],1) )
    $name &= $First & StringRight($splitup[$n],StringLen($splitup[$n] - 1))
Next

ConsoleWrite($name & @CRLF)
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

This is how I've solved it so far. I end up with a nicely formatted set of functions.

CODE
#include <String.au3>

global $InFunction = 0

$APIFile = FileOpen("openCycAPI.au3", 1)

$file = FileOpen("openCyc.au3", 0)

If $file = -1 Then

MsgBox(0, "Error", "Unable to open file.")

Exit

EndIf

While 1

$line = FileReadLine($file)

If @error = -1 Then ExitLoop

Select

Case $line = ""

$newLine = "EndFunc"

FileWrite($APIFile, $newLine & @CRLF & @CRLF)

$InFunction = 0

Case $line <> ""

$InFunction = 1

EndSelect

If $InFunction = 1 Then

If StringLeft($line, 8) = "function" Then

;between 9 and wherever func name Ends

$line = StringTrimLeft($line, 9)

$FunctionNameEnd = StringInStr($line, ":") - 1

$FuncName = StringLeft($line, $FunctionNameEnd)

$FuncName = StringLower(StringReplace($FuncName, "-", "_"))

$FuncName = StringRegExpReplace($FuncName, "(_a)", "_A")

$FuncName = StringRegExpReplace($FuncName, "(_:)", "_B")

$FuncName = StringRegExpReplace($FuncName, "(_c)", "_C")

$FuncName = StringRegExpReplace($FuncName, "(_d)", "_D")

$FuncName = StringRegExpReplace($FuncName, "(_e)", "_E")

$FuncName = StringRegExpReplace($FuncName, "(_f)", "_F")

$FuncName = StringRegExpReplace($FuncName, "(_g)", "_G")

$FuncName = StringRegExpReplace($FuncName, "(_h)", "_H")

$FuncName = StringRegExpReplace($FuncName, "(_i)", "_I")

$FuncName = StringRegExpReplace($FuncName, "(_j)", "_J")

$FuncName = StringRegExpReplace($FuncName, "(_k)", "_K")

$FuncName = StringRegExpReplace($FuncName, "(_l)", "_L")

$FuncName = StringRegExpReplace($FuncName, "(_m)", "_M")

$FuncName = StringRegExpReplace($FuncName, "(_n)", "_N")

$FuncName = StringRegExpReplace($FuncName, "(_o)", "_O")

$FuncName = StringRegExpReplace($FuncName, "(_p)", "_P")

$FuncName = StringRegExpReplace($FuncName, "(_q)", "_Q")

$FuncName = StringRegExpReplace($FuncName, "(_r)", "_R")

$FuncName = StringRegExpReplace($FuncName, "(_s)", "_S")

$FuncName = StringRegExpReplace($FuncName, "(_t)", "_T")

$FuncName = StringRegExpReplace($FuncName, "(_u)", "_U")

$FuncName = StringRegExpReplace($FuncName, "(_v)", "_V")

$FuncName = StringRegExpReplace($FuncName, "(_w)", "_W")

$FuncName = StringRegExpReplace($FuncName, "(_x)", "_X")

$FuncName = StringRegExpReplace($FuncName, "(_y)", "_Y")

$FuncName = StringRegExpReplace($FuncName, "(_z)", "_Z")

$parameterList = StringTrimLeft($line, StringLen($FuncName) + 2)

$FuncFirstLine = "Func _"& StringReplace($FuncName,"_","" ) & $parameterList

FileWrite($APIFile, $FuncFirstLine & @CRLF )

Else

$newLine = ";~" & $line

FileWrite($APIFile, $newLine & @CRLF )

EndIf

EndIf

WEnd

FileClose($APIFile)

FileClose($file)

Original Functions:

CODE
function CONSTANT-P : (object)

Return T iff the argument is a CycL constant

Single value returned satisfies BOOLEANP.

function CONSTANT-EXTERNAL-ID : (constant)

Return the external id of CONSTANT.

CONSTANT must satisfy CONSTANT-P.

Single value returned satisfies CONSTANT-EXTERNAL-ID-P or is NIL.

function CONSTANT-INTERNAL-ID : (constant)

Return the internal id of CONSTANT.

CONSTANT must satisfy CONSTANT-P.

Single value returned satisfies CONSTANT-INTERNAL-ID-P or is NIL.

function CONSTANT-NAME : (constant)

Return the name of CONSTANT or :unnamed.

CONSTANT must satisfy CONSTANT-P.

function FIND-CONSTANT : (name)

Return the constant with NAME, or NIL if not present.

NAME must satisfy STRINGP.

Single value returned satisfies CONSTANT-P or is NIL.

function FIND-CONSTANT-BY-EXTERNAL-ID : (external-id)

Return the constant with EXTERNAL-ID, or NIL if not present.

EXTERNAL-ID must satisfy CONSTANT-EXTERNAL-ID-P.

Single value returned satisfies CONSTANT-P or is NIL.

function FIND-CONSTANT-BY-INTERNAL-ID : (id)

Return the constant with internal ID, or NIL if not present.

ID must satisfy CONSTANT-INTERNAL-ID-P.

Single value returned satisfies CONSTANT-P or is NIL.

function CREATE-CONSTANT : (name &optional external-id)

Return a new constant named NAME. Use EXTERNAL-ID if non-null, else create a new ID.

Single value returned satisfies CONSTANT-P.

function FIND-OR-CREATE-CONSTANT : (name &optional external-id)

Return the constant with NAME if it exists, otherwise create it with EXTERNAL-ID.

Also, if it exists but has a null id, install EXTERNAL-ID on the constant.

NAME must satisfy STRINGP.

Single value returned satisfies CONSTANT-P.

function RENAME-CONSTANT : (constant new-name)

Rename CONSTANT to have NEW-NAME as its name. The constant is returned.

CONSTANT must satisfy CONSTANT-P.

NEW-NAME must satisfy STRINGP.

Single value returned satisfies CONSTANT-P.

function UNIQUIFY-CONSTANT-NAME : (name)

Return a unique, currently unused constant name based on NAME, which must be a valid constant name.

NAME must satisfy STRINGP.

Single value returned satisfies STRINGP.

Formatted:

CODE
Func _constantP (object)

;~Return T iff the argument is a CycL constant

;~Single value returned satisfies BOOLEANP.

EndFunc

Func _constantExternalId (constant)

;~Return the external id of CONSTANT.

;~CONSTANT must satisfy CONSTANT-P.

;~Single value returned satisfies CONSTANT-EXTERNAL-ID-P or is NIL.

EndFunc

Func _constantInternalId (constant)

;~Return the internal id of CONSTANT.

;~CONSTANT must satisfy CONSTANT-P.

;~Single value returned satisfies CONSTANT-INTERNAL-ID-P or is NIL.

EndFunc

Func _constantName (constant)

;~Return the name of CONSTANT or :unnamed.

;~CONSTANT must satisfy CONSTANT-P.

EndFunc

Func _findConstant (name)

;~Return the constant with NAME, or NIL if not present.

;~NAME must satisfy STRINGP.

;~Single value returned satisfies CONSTANT-P or is NIL.

EndFunc

Func _findConstantByExternalId (external-id)

;~Return the constant with EXTERNAL-ID, or NIL if not present.

;~EXTERNAL-ID must satisfy CONSTANT-EXTERNAL-ID-P.

;~Single value returned satisfies CONSTANT-P or is NIL.

EndFunc

Func _findConstantByInternalId (id)

;~Return the constant with internal ID, or NIL if not present.

;~ID must satisfy CONSTANT-INTERNAL-ID-P.

;~Single value returned satisfies CONSTANT-P or is NIL.

EndFunc

Func _createConstant (name &optional external-id)

;~Return a new constant named NAME. Use EXTERNAL-ID if non-null, else create a new ID.

;~Single value returned satisfies CONSTANT-P.

EndFunc

Func _findOrCreateConstant (name &optional external-id)

;~Return the constant with NAME if it exists, otherwise create it with EXTERNAL-ID.

;~Also, if it exists but has a null id, install EXTERNAL-ID on the constant.

;~NAME must satisfy STRINGP.

;~Single value returned satisfies CONSTANT-P.

EndFunc

Func _renameConstant (constant new-name)

;~Rename CONSTANT to have NEW-NAME as its name. The constant is returned.

;~CONSTANT must satisfy CONSTANT-P.

;~NEW-NAME must satisfy STRINGP.

;~Single value returned satisfies CONSTANT-P.

EndFunc

Func _uniquifyConstantName (name)

;~Return a unique, currently unused constant name based on NAME, which must be a valid constant name.

;~NAME must satisfy STRINGP.

;~Single value returned satisfies STRINGP.

EndFunc

I'm creating an openCyc UDF, so the things like (constant) will be replaced with ($constant), and the functions themselves will simply be firing off commands to the server, and then returns a response, so that instead of this:

TCPSend($Socket,'(ISA "$#Person" "$#Thing")]') ;

You can just write this:

_Isa("Person", "Thing")

The openCyc server then does it's thing, and returns "Hey, it's true! a Person is a Thing!"

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...