IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Voir le flux RSS

Open source et architecture logicielle

Un objet StringBuffer pour JavaScript : l'implémentation

Noter ce billet
par , 01/04/2016 à 18h26 (1205 Affichages)
Pour implémenter la classe StringBuffer, j'utiliserai par délégation les méthodes de l'objet String. D'ailleurs pour cette première implémentation, cette classe ne contiendra qu'une unique propriété string de type String. On pourrait donc considérer ici cette classe StringBuffer comme un wrapper du type String.
Code javascript : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"use strict";
 
class StringBuffer{
  constructor(str){
		if (str)
			this.string = new String(str);
		else
			this.str = new String();
  }
 
	// methodes
	getSize(){
		return this.string.length;
	}
 
	append(str){
		this.string += str;
	}
 
	charAt(index){
		return this.string.charAt(index);
	}
 
	codePointAt(index){
		return this.string.codePointAt(index);
	}
 
	delete(start, end){
		if (end > this.string.length)
			this.string = this.string.slice(0, start);
		else {
			this.string = this.string.slice(0, start) + this.string.slice(end);
		}
	}
 
	deleteCharAt(index){
		this.string = this.string.slice(0, index) + this.string.slice(index+1);
	}
 
	insert(offset, str){
		this.string = this.string.slice(0, offset) + str + this.string.slice(offset);
	}
 
	toString(){
		return(this.string.toString());
	}
}
Dans un billet ultérieur, je ferai une implémentation de cette classe avec pour propriété un tableau de caractères au lieu d'un objet String pour mesurer la différence de vitesse d’exécution avec l'implémentation actuelle.

Envoyer le billet « Un objet StringBuffer pour JavaScript : l'implémentation » dans le blog Viadeo Envoyer le billet « Un objet StringBuffer pour JavaScript : l'implémentation » dans le blog Twitter Envoyer le billet « Un objet StringBuffer pour JavaScript : l'implémentation » dans le blog Google Envoyer le billet « Un objet StringBuffer pour JavaScript : l'implémentation » dans le blog Facebook Envoyer le billet « Un objet StringBuffer pour JavaScript : l'implémentation » dans le blog Digg Envoyer le billet « Un objet StringBuffer pour JavaScript : l'implémentation » dans le blog Delicious Envoyer le billet « Un objet StringBuffer pour JavaScript : l'implémentation » dans le blog MySpace Envoyer le billet « Un objet StringBuffer pour JavaScript : l'implémentation » dans le blog Yahoo

Commentaires