Attachment "dict-getwithdefault.c" to
ticket [2370575fff]
added by
dkf
2019-04-01 09:10:28.
/*
* Table of dict subcommand names and implementations.
*/
static const EnsembleImplMap implementationMap[] = {
{"append", DictAppendCmd, TclCompileDictAppendCmd },
{"create", DictCreateCmd },
{"exists", DictExistsCmd },
{"filter", DictFilterCmd },
{"for", DictForCmd, TclCompileDictForCmd, DictForNRCmd },
{"get", DictGetCmd, TclCompileDictGetCmd },
{"getwithdefault", DictGetWithDefaultCmd },
{"incr", DictIncrCmd, TclCompileDictIncrCmd },
{"info", DictInfoCmd },
{"keys", DictKeysCmd },
{"lappend", DictLappendCmd, TclCompileDictLappendCmd },
{"merge", DictMergeCmd },
{"remove", DictRemoveCmd },
{"replace", DictReplaceCmd },
{"set", DictSetCmd, TclCompileDictSetCmd },
{"size", DictSizeCmd },
{"unset", DictUnsetCmd },
{"update", DictUpdateCmd, TclCompileDictUpdateCmd },
{"values", DictValuesCmd },
{"with", DictWithCmd },
{NULL}
};
/***********
* *
* [snip] *
* *
***********/
/*
*----------------------------------------------------------------------
*
* DictGetWithDefaultCmd --
*
* This function implements the "dict getwithdefault" Tcl command.
* See the user documentation for details on what it does, and
* TIP#3?? for the formal specification.
*
* Results:
* A standard Tcl result.
*
* Side effects:
* The same as for DictExistsCmd.
*
*----------------------------------------------------------------------
*/
static int
DictGetWithDefaultCmd(
ClientData dummy,
Tcl_Interp *interp,
int objc,
Tcl_Obj *const *objv)
{
Tcl_Obj *dictPtr, *valuePtr;
int result;
if (objc < 4) {
Tcl_WrongNumArgs(interp, 1, objv,
"dictionary key ?key ...? default");
return TCL_ERROR;
}
dictPtr = TclTraceDictPath(interp, objv[1], objc-4, objv+2,
DICT_PATH_EXISTS);
if (dictPtr == NULL) {
return TCL_ERROR;
}
if (dictPtr == DICT_PATH_NON_EXISTENT) {
Tcl_SetObjResult(interp, objv[objc-1]);
return TCL_OK;
}
result = Tcl_DictObjGet(interp, dictPtr, objv[objc-2], &valuePtr);
if (result != TCL_OK) {
return result;
}
Tcl_SetObjResult(interp, valuePtr != NULL ? valuePtr : objv[objc-1]);
return TCL_OK;
}
/*
Material for dict.test:
test dict-23.1 {dict getwithdefault command} -body {
dict getwithdefault {a b} a c
} -result b
test dict-23.2 {dict getwithdefault command} -body {
dict getwithdefault {a b} b c
} -result c
test dict-23.3 {dict getwithdefault command} -body {
dict getwithdefault {a {b c}} a b d
} -result c
test dict-23.4 {dict getwithdefault command} -body {
dict getwithdefault {a {b c}} a c d
} -result d
test dict-23.5 {dict getwithdefault command} -body {
dict getwithdefault {a {b c}} b c d
} -result d
test dict-23.6 {dict getwithdefault command} -returnCodes error -body {
dict getwithdefault {a {b c d}} a b d
} -result {missing value to go with key}
test dict-23.7 {dict getwithdefault command} -returnCodes error -body {
dict getwithdefault
} -result {wrong # args: should be\
"dict getwithdefault dictionary key ?key ...? default"}
test dict-23.8 {dict getwithdefault command} -returnCodes error -body {
dict getwithdefault {}
} -result {wrong # args: should be\
"dict getwithdefault dictionary key ?key ...? default"}
*/