A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/richardschneider/net-ipfs-core/commit/7baa55b below:

ToString "G" and "L" · richardschneider/net-ipfs-core@7baa55b · GitHub

File tree Expand file treeCollapse file tree 2 files changed

+97

-20

lines changed

Filter options

Expand file treeCollapse file tree 2 files changed

+97

-20

lines changed Original file line number Diff line number Diff line change

@@ -184,28 +184,78 @@ public MultiHash Hash

184 184

}

185 185 186 186

/// <summary>

187 -

/// A CID that is readable by a human.

187 +

/// Returns the string representation of the CID in the

188 +

/// general format.

188 189

/// </summary>

189 190

/// <returns>

190 -

/// e.g. "base58btc cidv0 dag-pb sha2-256 Qm..."

191 +

/// e.g. "QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39V"

191 192

/// </returns>

192 193

public override string ToString()

193 194

{

194 -

var sb = new StringBuilder();

195 -

sb.Append(Encoding);

196 -

sb.Append(' ');

197 -

sb.Append("cidv");

198 -

sb.Append(Version);

199 -

sb.Append(' ');

200 -

sb.Append(ContentType);

201 -

if (Hash != null)

202 -

{

203 -

sb.Append(' ');

204 -

sb.Append(Hash.Algorithm.Name);

205 -

sb.Append(' ');

206 -

sb.Append(MultiBase.Encode(Hash.ToArray(), Encoding).Substring(1));

207 -

}

208 -

return sb.ToString();

195 +

return ToString("G");

196 +

}

197 + 198 +

/// <summary>

199 +

/// Returns a string representation of the CID

200 +

/// according to the provided format specifier.

201 +

/// </summary>

202 +

/// <param name="format">

203 +

/// A single format specifier that indicates how to format the value of the

204 +

/// CID. Can be "G" or "L".

205 +

/// </param>

206 +

/// <returns>

207 +

/// The CID in the specified <paramref name="format"/>.

208 +

/// </returns>

209 +

/// <exception cref="FormatException">

210 +

/// <paramref name="format"/> is not valid.

211 +

/// </exception>

212 +

/// <remarks>

213 +

/// <para>

214 +

/// The "G" format specifier is the same as calling <see cref="Encode"/>.

215 +

/// </para>

216 +

/// <list type="table">

217 +

/// <listheader>

218 +

/// <term>Specifier</term>

219 +

/// <description>return value</description>

220 +

/// </listheader>

221 +

/// <item>

222 +

/// <term>G</term>

223 +

/// <description>QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39V</description>

224 +

/// </item>

225 +

/// <item>

226 +

/// <term>L</term>

227 +

/// <description>base58btc cidv0 dag-pb sha2-256 Qm...</description>

228 +

/// </item>

229 +

/// </list>

230 +

/// </remarks>

231 +

public string ToString(string format)

232 +

{

233 +

switch (format)

234 +

{

235 +

case "G":

236 +

return Encode();

237 + 238 +

case "L":

239 +

var sb = new StringBuilder();

240 +

sb.Append(Encoding);

241 +

sb.Append(' ');

242 +

sb.Append("cidv");

243 +

sb.Append(Version);

244 +

sb.Append(' ');

245 +

sb.Append(ContentType);

246 +

if (Hash != null)

247 +

{

248 +

sb.Append(' ');

249 +

sb.Append(Hash.Algorithm.Name);

250 +

sb.Append(' ');

251 +

sb.Append(MultiBase.Encode(Hash.ToArray(), Encoding).Substring(1));

252 +

}

253 +

return sb.ToString();

254 + 255 +

default:

256 +

throw new FormatException($"Invalid CID format specifier '{format}'.");

257 +

}

258 + 209 259

}

210 260 211 261

/// <summary>

Original file line number Diff line number Diff line change

@@ -13,13 +13,40 @@ namespace Ipfs

13 13

public class CidTest

14 14

{

15 15

[TestMethod]

16 -

public void HumanReadable()

16 +

public void ToString_Default()

17 17

{

18 18

var cid = new Cid { Hash = new MultiHash("QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39V") };

19 -

Assert.AreEqual("base58btc cidv0 dag-pb sha2-256 QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39V", cid.ToString());

19 +

Assert.AreEqual("QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39V", cid.ToString());

20 20 21 21

cid = "zBunRGrmCGokA1oMESGGTfrtcMFsVA8aEtcNzM54akPWXF97uXCqTjF3GZ9v8YzxHrG66J8QhtPFWwZebRZ2zeUEELu67";

22 -

Assert.AreEqual("base58btc cidv1 dag-pb sha2-512 8Vx9QNCcSt39anEamkkSaNw5rDHQ7yuadq7ihZed477qQNXxYr3HReMamd1Q2EnUeL4oNtVAmNw1frEhEN1aoqFuKD", cid.ToString());

22 +

Assert.AreEqual("zBunRGrmCGokA1oMESGGTfrtcMFsVA8aEtcNzM54akPWXF97uXCqTjF3GZ9v8YzxHrG66J8QhtPFWwZebRZ2zeUEELu67", cid.ToString());

23 +

}

24 + 25 +

[TestMethod]

26 +

public void ToString_L()

27 +

{

28 +

var cid = new Cid { Hash = new MultiHash("QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39V") };

29 +

Assert.AreEqual("base58btc cidv0 dag-pb sha2-256 QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39V", cid.ToString("L"));

30 + 31 +

cid = "zBunRGrmCGokA1oMESGGTfrtcMFsVA8aEtcNzM54akPWXF97uXCqTjF3GZ9v8YzxHrG66J8QhtPFWwZebRZ2zeUEELu67";

32 +

Assert.AreEqual("base58btc cidv1 dag-pb sha2-512 8Vx9QNCcSt39anEamkkSaNw5rDHQ7yuadq7ihZed477qQNXxYr3HReMamd1Q2EnUeL4oNtVAmNw1frEhEN1aoqFuKD", cid.ToString("L"));

33 +

}

34 + 35 +

[TestMethod]

36 +

public void ToString_G()

37 +

{

38 +

var cid = new Cid { Hash = new MultiHash("QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39V") };

39 +

Assert.AreEqual("QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39V", cid.ToString("G"));

40 + 41 +

cid = "zBunRGrmCGokA1oMESGGTfrtcMFsVA8aEtcNzM54akPWXF97uXCqTjF3GZ9v8YzxHrG66J8QhtPFWwZebRZ2zeUEELu67";

42 +

Assert.AreEqual("zBunRGrmCGokA1oMESGGTfrtcMFsVA8aEtcNzM54akPWXF97uXCqTjF3GZ9v8YzxHrG66J8QhtPFWwZebRZ2zeUEELu67", cid.ToString("G"));

43 +

}

44 + 45 +

[TestMethod]

46 +

public void ToString_InvalidFormat()

47 +

{

48 +

var cid = new Cid { Hash = new MultiHash("QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39V") };

49 +

ExceptionAssert.Throws<FormatException>(() => cid.ToString("?"));

23 50

}

24 51 25 52

[TestMethod]

You can’t perform that action at this time.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4