In Name constructor, allow the argument to be another Name.
In Name.add, return this to allow chaining, and allow the argument to be another Name.
Added Name.addSegment.
diff --git a/js/Name.js b/js/Name.js
index 4d8a64c..833f383 100644
--- a/js/Name.js
+++ b/js/Name.js
@@ -6,8 +6,10 @@
/*
* Create a new Name from _components.
- * If _components is a string, parse it as a URI. Otherwise it is an array of components
- * where each is a string, byte array, ArrayBuffer or Uint8Array.
+ * If _components is a string, parse it as a URI.
+ * If _components is a Name, add a deep copy of its components.
+ * Otherwise it is an array of components where each is a string, byte array, ArrayBuffer, Uint8Array
+ * or Name.
* Convert and store as an array of Uint8Array.
* If a component is a string, encode as utf8.
*/
@@ -17,10 +19,13 @@
this.components = Name.createNameArray(_components);
}
else if(typeof _components === 'object'){
- if(LOG>4)console.log('Content Name Array '+_components);
this.components = [];
- for (var i = 0; i < _components.length; ++i)
- this.add(_components[i]);
+ if (_components instanceof Name)
+ this.add(_components);
+ else {
+ for (var i = 0; i < _components.length; ++i)
+ this.add(_components[i]);
+ }
}
else if(_components==null)
this.components =[];
@@ -114,10 +119,10 @@
};
/*
- * component is a string, byte array, ArrayBuffer or Uint8Array.
+ * component is a string, byte array, ArrayBuffer, Uint8Array or Name.
* Convert to Uint8Array and add to this Name.
* If a component is a string, encode as utf8.
- * Return the converted value.
+ * Return this Name object to allow chaining calls to add.
*/
Name.prototype.add = function(component){
var result;
@@ -130,6 +135,18 @@
result = new Uint8Array(new ArrayBuffer(component.byteLength));
result.set(new Uint8Array(component));
}
+ else if (typeof component == 'object' && component instanceof Name) {
+ var components;
+ if (component == this)
+ // special case, when we need to create a copy
+ components = this.components.slice(0, this.components.length);
+ else
+ components = component.components;
+
+ for (var i = 0; i < components.length; ++i)
+ this.components.push(new Uint8Array(components[i]));
+ return this;
+ }
else if(typeof component == 'object')
// Assume component is a byte array. We can't check instanceof Array because
// this doesn't work in JavaScript if the array comes from a different module.
@@ -138,7 +155,8 @@
throw new Error("Cannot add Name element at index " + this.components.length +
": Invalid type");
- return this.components.push(result);
+ this.components.push(result);
+ return this;
};
// Return the escaped name string according to "CCNx URI Scheme".
@@ -154,6 +172,26 @@
return result;
};
+/**
+* @brief Add component that represents a segment number
+*
+* @param number Segment number (integer is expected)
+*
+* This component has a special format handling:
+* - if number is zero, then %00 is added
+* - if number is between 1 and 255, %00%01 .. %00%FF is added
+* - ...
+*/
+Name.prototype.addSegment = function(number) {
+ var segmentNumberBigEndian = DataUtils.nonNegativeIntToBigEndian(number);
+ // Put a 0 byte in front.
+ var segmentNumberComponent = new Uint8Array(segmentNumberBigEndian.length + 1);
+ segmentNumberComponent.set(segmentNumberBigEndian, 1);
+
+ this.components.push(segmentNumberComponent);
+ return this;
+};
+
/*
* Return a new Name with the first nComponents components of this Name.
*/
diff --git a/js/tools/build/ndn-js-uncomp.js b/js/tools/build/ndn-js-uncomp.js
index b947a5c..274879c 100644
--- a/js/tools/build/ndn-js-uncomp.js
+++ b/js/tools/build/ndn-js-uncomp.js
@@ -633,6 +633,26 @@
return result;
};
+/**
+* @brief Add component that represents a segment number
+*
+* @param number Segment number (integer is expected)
+*
+* This component has a special format handling:
+* - if number is zero, then %00 is added
+* - if number is between 1 and 255, %00%01 .. %00%FF is added
+* - ...
+*/
+Name.prototype.addSegment = function(number) {
+ var segmentNumberBigEndian = DataUtils.nonNegativeIntToBigEndian(number);
+ // Put a 0 byte in front.
+ var segmentNumberComponent = new Uint8Array(segmentNumberBigEndian.length + 1);
+ segmentNumberComponent.set(segmentNumberBigEndian, 1);
+
+ this.components.push(segmentNumberComponent);
+ return this;
+};
+
/*
* Return a new Name with the first nComponents components of this Name.
*/
diff --git a/js/tools/build/ndn-js.js b/js/tools/build/ndn-js.js
index fb403f1..fb07e3a 100644
--- a/js/tools/build/ndn-js.js
+++ b/js/tools/build/ndn-js.js
@@ -18,9 +18,9 @@
Name.prototype.from_ccnb=function(a){a.readStartElement(this.getElementLabel());for(this.components=[];a.peekStartElement(CCNProtocolDTags.Component);)this.add(a.readBinaryElement(CCNProtocolDTags.Component));a.readEndElement()};Name.prototype.to_ccnb=function(a){if(null==this.components)throw Error("CANNOT ENCODE EMPTY CONTENT NAME");a.writeStartElement(this.getElementLabel());for(var b=this.components.length,c=0;c<b;c++)a.writeElement(CCNProtocolDTags.Component,this.components[c]);a.writeEndElement()};
Name.prototype.getElementLabel=function(){return CCNProtocolDTags.Name};
Name.prototype.add=function(a){var b;if("string"==typeof a)b=DataUtils.stringToUtf8Array(a);else if("object"==typeof a&&a instanceof Uint8Array)b=new Uint8Array(a);else if("object"==typeof a&&a instanceof ArrayBuffer)b=new Uint8Array(new ArrayBuffer(a.byteLength)),b.set(new Uint8Array(a));else if("object"==typeof a)b=new Uint8Array(a);else throw Error("Cannot add Name element at index "+this.components.length+": Invalid type");return this.components.push(b)};
-Name.prototype.to_uri=function(){if(0==this.components.length)return"/";for(var a="",b=0;b<this.components.length;++b)a+="/"+Name.toEscapedString(this.components[b]);return a};Name.prototype.getPrefix=function(a){return new Name(this.components.slice(0,a))};Name.prototype.getComponent=function(a){var b=new ArrayBuffer(this.components[a].length);(new Uint8Array(b)).set(this.components[a]);return b};
-Name.prototype.indexOfFileName=function(){for(var a=this.components.length-1;0<=a;--a){var b=this.components[a];if(!(0>=b.length)&&!(0==b[0]||192==b[0]||193==b[0]||245<=b[0]&&255>=b[0]))return a}return-1};Name.prototype.equalsName=function(a){if(this.components.length!=a.components.length)return!1;for(var b=this.components.length-1;0<=b;--b)if(!DataUtils.arraysEqual(this.components[b],a.components[b]))return!1;return!0};
-Name.prototype.getContentDigestValue=function(){for(var a=this.components.length-1;0<=a;--a){var b=Name.getComponentContentDigestValue(this.components[a]);if(null!=b)return b}return null};
+Name.prototype.to_uri=function(){if(0==this.components.length)return"/";for(var a="",b=0;b<this.components.length;++b)a+="/"+Name.toEscapedString(this.components[b]);return a};Name.prototype.addSegment=function(a){var a=DataUtils.nonNegativeIntToBigEndian(a),b=new Uint8Array(a.length+1);b.set(a,1);this.components.push(b);return this};Name.prototype.getPrefix=function(a){return new Name(this.components.slice(0,a))};
+Name.prototype.getComponent=function(a){var b=new ArrayBuffer(this.components[a].length);(new Uint8Array(b)).set(this.components[a]);return b};Name.prototype.indexOfFileName=function(){for(var a=this.components.length-1;0<=a;--a){var b=this.components[a];if(!(0>=b.length)&&!(0==b[0]||192==b[0]||193==b[0]||245<=b[0]&&255>=b[0]))return a}return-1};
+Name.prototype.equalsName=function(a){if(this.components.length!=a.components.length)return!1;for(var b=this.components.length-1;0<=b;--b)if(!DataUtils.arraysEqual(this.components[b],a.components[b]))return!1;return!0};Name.prototype.getContentDigestValue=function(){for(var a=this.components.length-1;0<=a;--a){var b=Name.getComponentContentDigestValue(this.components[a]);if(null!=b)return b}return null};
Name.getComponentContentDigestValue=function(a){return a.length==Name.ContentDigestPrefix.length+32+Name.ContentDigestSuffix.length&&DataUtils.arraysEqual(a.subarray(0,Name.ContentDigestPrefix.length),Name.ContentDigestPrefix)&&DataUtils.arraysEqual(a.subarray(a.length-Name.ContentDigestSuffix.length,a.length),Name.ContentDigestSuffix)?a.subarray(Name.ContentDigestPrefix.length,Name.ContentDigestPrefix.length+32):null};Name.ContentDigestPrefix=new Uint8Array([193,46,77,46,71,193,1,170,2,133]);
Name.ContentDigestSuffix=new Uint8Array([0]);Name.toEscapedString=function(a){for(var b="",c=!1,d=0;d<a.length;++d)if(46!=a[d]){c=!0;break}if(c)for(d=0;d<a.length;++d)c=a[d],b=48<=c&&57>=c||65<=c&&90>=c||97<=c&&122>=c||43==c||45==c||46==c||95==c?b+String.fromCharCode(c):b+("%"+(16>c?"0":"")+c.toString(16).toUpperCase());else{b="...";for(d=0;d<a.length;++d)b+="."}return b};
Name.fromEscapedString=function(a){a=unescape(a.trim());return null==a.match(/[^.]/)?2>=a.length?null:DataUtils.toNumbersFromString(a.substr(3,a.length-3)):DataUtils.toNumbersFromString(a)};Name.prototype.match=function(a){var b=this.components,a=a.components;if(b.length>a.length)return!1;for(var c=0;c<b.length;++c)if(!DataUtils.arraysEqual(b[c],a[c]))return!1;return!0};